C project calculator with any number of operands

In school and college a calculator program or project in c consists of only 2 operands. How can we make it calculate any number of operands according to user input. In this article we try to make one by taking the same function to the next level.

printf(“%s”,” c project calculator “);

Here is our very old function that calculates with 2 operands

double calc2(double a,char b,double c){
  double res;
  switch(b){
case '+':
    res=a+c;
    break;
case '-':
    res=a-c;
    break;
case '*':
    res=a*c;
    break;
case '/':
    res=a/c;
    break;
  }
  return res;
}

Lets continue with the main function

First we declare a struct where we store the user input

int main()
{
    struct cal{
      double num;
      char op;
    } a[100];
    printf("Thangus calculatorn");
}

We store the operands in double num and operators in char opAlso give your calculator a nice name inside the the printf() statement. I like to call mine “Thangus calculator” for no reason. Declare a variable int n in which we store the length of the user input and also int i for the loop. Now we take user input inside the a loop because we don’t know how much the user is going into input.

int main()
{
    struct cal{
      double num;
      char op;
    } a[100];
    printf("Thangus calculator");
    static int n=0;
    int i;
    for(i=0;i<100;i++){
        if(i%2==0){
            scanf("%lf",&a[i].num);
        }
        else{
            scanf("%c",&a[i].op);

            if(a[i].op==42||a[i].op==43||a[i].op==45||a[i].op==47){
                n++;
            }
            else{
                break;
            }
        }
    }
}

We loop till limit of the struct we declared and exit when the user input ends. By this the user can input whatever he wants in only one line.Consider an example user input 2+2+2.Here is a step by step explanation of what happens

when i=0

  • Matches the first if condition because 0 is completely divisible by 2 (Condition to check if i is even or not or just 0)
  • scans 2 and stops scanning because ‘+’ does not match the format specified and leaves +2+2 in the buffer.

when i=1

  • Now we now that next character is ‘+’ we save it in a[i].op
  • We scan ‘+’ into a[1].op.
  • We check to see if the character is ‘+’ , ‘-‘ , ‘*’ , ‘/’ . If it is we increment n so at last we can get the total number of operators in the user input.

This continues till i=4 but

when i=5

  • The character doesn’t match our specified character list(because user input has ended and the last input is enter key), so we break the loop and exit.

We now have the total number of operators  in the user input stored in n. We have stored the user input in sequence and have the length of the user input, its time to do the calculations.

In our example 2+2+2 the way it should work is

  • First calculate 2+2
  • Then taking the calculated value of 2+2 that is 4 and doing 4+2.

Now that we have the idea lets do it

    n=2*n;
    double current=a[0].num;
    for(i=0;i<n;i+=2){
        current=calc2(current,a[i+1].op,a[i+2].num);
    }
    printf("%lf",current);

Notice here that we are not incrementing the value of i in the loop by i++ but instead i+=2 or i=i+2. We are calculating 2 numbers at a time as mentioned. We named the function calc2Number of calculations we have to perform = number of operators, but because we are incrementing i by 2 (in the loop), i must be less than 2n.We store the currently calculated value in current, starting with a[0].num.

Taking our 2+2+2 example again

  • first current is 2, then current+2 is calculated and current becomes 4
  • Now current+2 is calculated which is equal to 6 and the result is printed out.

Now that our simple c project – A calculator with any number of operands is complete, here is the final code

double calc2(double a,char b,double c);
int main()
{
    struct cal{
      double num;
      char op;
    } a[100];
    printf("Thangus calculatorn");
    static int n=0;
    int i;
    for(i=0;i<100;i++){
        if(i%2==0){
            scanf("%lf",&a[i].num);
        }
        else{
            scanf("%c",&a[i].op);

            if(a[i].op==42||a[i].op==43||a[i].op==45||a[i].op==47){
                n++;
            }
            else{
                break;
            }
        }
    }
    n=2*n;
    double current=a[0].num;
    for(i=0;i<n;i+=2){
        current=calc2(current,a[i+1].op,a[i+2].num);
    }
    printf("%lf",current);

}
double calc2(double a,char b,double c){
  double res;
  switch(b){
case '+':
    res=a+c;
    break;
case '-':
    res=a-c;
    break;
case '*':
    res=a*c;
    break;
case '/':
    res=a/c;
    break;
  }
  return res;
}

We will be happy to hear your thoughts

      Leave a reply