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");
    }

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.

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.

Looks decent i ll give it a look thank you JamesCherrill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.