I wrote below code to do calculation. But it donesn't work for negative numbers like if input 4 + ( - 5 ), the program will crash. Any one can help me to modify the code to make it can calculate negative int also. Like if input 4 + ( - 5 ) should get -1, if input - 5 + 4 should get -1, ect..
Code:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int checkPrecedence(char c)
// return the precedence of the operator, a larger value means a higher precedence
{
if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
}
int main(void)
{
string input; // Enter expression here
getline(cin,input);
string::const_iterator iterInput;
// Converting Infix to Postfix
string output;
stack<char> stk;
for (iterInput=input.begin(); iterInput!=input.end(); iterInput++)
{
if ((*iterInput >= '0' && *iterInput <= '9') || (*iterInput == ' '))
{
if (!(output.empty() && *iterInput == ' ')) // ignore first ' ' in string
output.push_back(*iterInput); // add operand to postfix string
}
else if (*iterInput == '(')
{
stk.push(*iterInput); // add '(' to stack
iterInput++; // skip the next ' '
}
else if (*iterInput == ')')
{
while(stk.top() != '(') // remove all operator until '('
{
output.push_back(stk.top()); // add operator to postfix string
stk.pop(); // remove operator from stack
output.push_back(' '); // add ' ' to postfix string
}
stk.pop(); // remove the '('
iterInput++; // skip the next ' '
}
else // handle the operator
{
while (!stk.empty() && stk.top() != '(' && checkPrecedence(stk.top()) >= checkPrecedence(*iterInput))
{
output.push_back (stk.top()); // add operator to postfix string
stk.pop(); // remove operator from stack
}
stk.push(*iterInput); // add operator to stack
iterInput++; // skip the next ' '
}
}
while (!stk.empty()) // add all remaining operator from stack to postfix string
{
output.push_back(' '); // add ' ' to postfix string
output.push_back(stk.top()); // add operator to postfix string
stk.pop(); // remove operator from stack
}
cout << "Postfix expression: " << output << std::endl;
// Evaluate Postfix expression
stack<int> intStack;
string::const_iterator iterOutput;
string numString;
for (iterOutput= output.begin(); iterOutput!=output.end(); iterOutput++)
{
if (*iterOutput >= '0' && *iterOutput <= '9')
numString.push_back(*iterOutput);
else if (*iterOutput == ' ')
{
intStack.push(atoi(numString.c_str ()));
numString.clear();
}
else // operator
{
int rightOperand = intStack.top();
intStack.pop();
int leftOperand = intStack.top();
intStack.pop();
int result;
if (*iterOutput == '*')
result = leftOperand * rightOperand;
else if (*iterOutput == '/')
result = leftOperand / rightOperand;
else if (*iterOutput == '+')
result = leftOperand + rightOperand;
else if (*iterOutput == '-')
result = leftOperand - rightOperand;
intStack.push(result);
iterOutput++; // skip the next ' '
if (iterOutput== output.end()) // handle end of string condition
break;
}
}
cout << "Result: " << intStack.top() << endl;
system("pause");
}