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