0

Hello,

I am building a calculator in c++ windows application form, i want the calculator to handle multiple operator precedence (for example,
2+4*5/3). but i am not sure how to write the following code on my windows form project.

Any suggestions would appreciated .

    #include <iostream>
    #include <conio.h>

    using namespace std;

    int main()
    {

    int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;
    char op1, op2, op3, op4;
    int total1 = 0, total2 = 0, total3 = 0, total4 = 0;

    cout << "Enter five values with operator to calculate=>";
    cin >> num1 >> op1 >> num2 >> op2 >> num3 >> op3 >> num4 >> op4 >> num5;

    //first operator
    if (op1 == '*')
    {
        total1 = num1*num2;
    }
    else if (op1 == '/')
    {
        total1 = num1 / num2;
    }
    else if (op1 == '+')
    {
        total1 = num1 / num2;
    }
    else if (op1 == '-')
    {
        total1 = num1 - num2;
    }
    else
    {
        cout << "Invalid operator";
    }

    //second operator
    if (op2 == '*')
    {
        total2 = total1*num3;
    }
    else if (op2 == '/')
    {
        total2 = total1 / num3;
    }
    else if (op2 == '+')
    {
        total2 = total1 / num3;
    }
    else if (op2 == '-')
    {
        total2 = total1 - num3;
    }
    else
    {
        cout << "Invalid operator";
    }

    // third operator
    if (op3 == '*')
    {
        total3 = total2*num4;
    }
    else if (op3 == '/')
    {
        total3 = total2 / num4;
    }
    else if (op3 == '+')
    {
        total3 = total2 / num4;
    }
    else if (op3 == '-')
    {
        total3 = total2 - num4;
    }
    else
    {
        cout << "Invalid operator";
    }

    //fourth operator
    if (op4 == '*')
    {
        total4 = total3*num5;
    }
    else if (op4 == '/')
    {
        total4 = total3 / num5;
    }
    else if (op4 == '+')
    {
        total4 = total3 / num5;
    }
    else if (op4 == '-')
    {
        total4 = total3 - num5;
    }
    else
    {
        cout << "Invalid operator";
    }

    cout << "Total =>" << total4 << endl;
    system("pause");
    }
3
Contributors
6
Replies
23
Views
4 Weeks
Discussion Span
Last Post by rproffitt
0

Hi rproffitt,

I have already seen multiple tutorials and videos however, non of them can perform multiple operator precedence such as 3+2/513 or (3+2)6/2 etc. They only allow 1 operation at a time.

Thanks for your replay.

0

Hi rproffitt,

I have already seen multiple tutorials and videos however, non of them can perform multiple operator precedence such as 3+2/513 or (3+2)6/2 etc. They only allow 1 operation at a time.

Thanks for your replay.

0

Looks decent i ll give it a look thank you JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.