Code:
#include <iostream>
#include <stack>
void calculation(int &ope1, int &ope2, char &input);
using namespace std;
stack <int> a;
void main(void)
{
bool check = false;
int number;
char ch;
char oper;
while (!check)
{
cout << endl;
cout << "Press # for numbers: " << endl;
cout << "Press your sign (+, -, /, * ) for calculation." << endl ;
cout << "press '=' to print the calculation." ;
cin >> ch;
if (ch == '#')
{
cout << "Enter number; ";
cin >> number;
a.push(number);
}
else if (ch != '#')
{
calculation(number, number, ch);
if (ch == '=')
{
check = true;
}
else if (ch != '=')
{
check = false;
}
}
}
cout << "Your value is " << a.top() ;
system ("pause");
}
void calculation(int &oper1, int &oper2, char &chr)
{
switch (chr)
{
case '+':
a.push(oper1 + oper2);
break;
case '-':
a.push(oper1 - oper2);
break;
case '*':
a.push(oper1 * oper2);
break;
case '/':
a.push(oper1 / oper2);
break;
}
}
Another version. Able to input numbers, operators. (no error checking. ) But the calculation completely wrong. Where is the wrong part.