vendredi 29 janvier 2021

Multi-operator Calculator With C

I want to make a calculator which takes 3+4/4*6 kinda input and I need to write it in C programming. I take numbers and operations separately from user, then I put numbers and operators to separate arrays. Then I wanted to loop through arrays respectively to do operations -I know operator priority is not considered- but It is not giving me correct result at all. To determine opeators, first I used switch case then tried If statement but both of them gave wrong results. I am gonna share both of them. How I can re-arrange this code to get correct result ?

By correct result I mean e.g 3+4/1*8 = 56

CODE WITH IF STATEMENT

#include <stdio.h>
int main(void){
    printf("You will enter your operations in number - operator order.\n");
    printf("How many numbers do you have to calculate?");
    int how_many; //how many numbers the user has in his/her whole operation
    scanf("%d",&how_many);
    int number_entry = how_many; //for example 1+2*9/5 has 4 numbers
    int operator_entry = how_many - 1; //and 3 operators
    int *number_in; //for every number as 1, 2 ,9 ,5 according to example
    //int *last;
    char *operator_in; // and for every operator as +, * , / according to example
    int numbers[number_entry];
    int ops[operator_entry];
    int j;
    int result;
    //int k;
    printf("\n");
    for(j=0;j<operator_entry;j++){
        printf("Enter the number : ");
        scanf("%d",&number_in);
        numbers[j] = number_in;
        printf("Enter the operation as + for add, - for substract, * for multiply and / for division: ");
        scanf(" %c", &operator_in);
        ops[j] = operator_in;
    };
    printf("Enter the number : ");
    scanf("%d" , &number_in);   
    numbers[number_entry-1] = number_in;
        
    
    int loop;

//wrote these loops to see if arrays works correctly or not

    for(loop = 0; loop < number_entry; loop++)
        printf("%d ", numbers[loop]);
    
    for(loop = 0; loop < operator_entry; loop++)
        printf(" %c", ops[loop]);
        
    for(loop = 0; loop<number_entry;loop++){
        
        if(ops[loop] == "+"){
            
            result = numbers[2*loop] + numbers[(2*loop)-1];
            
        }else if(ops[loop] == "-"){
            
            result = numbers[2*loop] - numbers[(2*loop)-1];
            
        }else if(ops[loop] == "*"){
            
            result = numbers[2*loop] * numbers[(2*loop)-1];
        }
        else if(ops[loop] == "/"){
            
            result = numbers[2*loop] / numbers[(2*loop)-1];
        }
        
    };
    
    printf("\n");
    
    printf("Result: ");
    
    printf("%d", result);
    
    return 0;
}

WITH SWITCH CASE

#include <stdio.h>
int main(void){
    printf("You will enter your operations in number - operator order.\n");
    printf("How many numbers do you have to calculate?");
    int how_many; //how many numbers the user has in his/her whole operation
    scanf("%d",&how_many);
    int number_entry = how_many; //for example 1+2*9/5 has 4 numbers
    int operator_entry = how_many - 1; //and 3 operators
    int *number_in; //for every number as 1, 2 ,9 ,5 according to example
    //int *last;
    char *operator_in; // and for every operator as +, * , / according to example
    int numbers[number_entry];
    int ops[operator_entry];
    int j;
    int *result = 0;
    //int k;
    printf("\n");
    for(j=0;j<operator_entry;j++){
        printf("Enter the number : ");
        scanf("%d",&number_in);
        numbers[j] = number_in;
        printf("Enter the operation as + for add, - for substract, * for multiply and / for division: ");
        scanf(" %c", &operator_in);
        ops[j] = operator_in;
    };
    printf("Enter the number : ");
    scanf("%d" , &number_in);   
    numbers[number_entry-1] = number_in;
        
    
    int loop;

//wrote these loops to see if arrays works correctly or not

    for(loop = 0; loop < number_entry; loop++)
        printf("%d ", numbers[loop]);
    
    for(loop = 0; loop < operator_entry; loop++)
        printf(" %c", ops[loop]);
        
    for(loop = 0; loop<number_entry;loop++){
        switch(ops[loop]){
            case '+':
                result = numbers[2*loop] + numbers[(2*loop)-1];
                break;
            case '-':
                result = numbers[2*loop] - numbers[(2*loop)-1];
                break;
            case '*':
                result = numbers[2*loop] * numbers[(2*loop)-1];
                break;
            case '/':
                result = numbers[2*loop] / numbers[(2*loop)-1];
                break;      
        };

        resulu = result + result;
    };
    
    printf("\n");
    
    printf("Result: ");
    
    printf("%d", result);
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire