By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Code modification.

P: 7
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");
}
Sep 21 '06 #1
Share this Question
Share on Google+
1 Reply


100+
P: 293
D_C
Are you using the same character for negative numbers as minus signs? For example, -5 = 0-5. That's where you are confusing it, I bet. Each time you run into '-' check to see if the last thing pushed was a number. Otherwise, push 0, then '-'. Well I'm not sure if it's infix or postfix, therefore which order to push 0 and '-'. But you get the idea, right?

See where that gets you.
Sep 21 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.