【发布时间】:2011-08-03 15:11:07
【问题描述】:
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
int main()
{
string input;
cout << "Enter a postfix expression: " << endl;
getline(cin, input);
int operand1, operand2, result,number;
stack<char>operation;
stringstream temp;
int i=0;
while (i < input.length())
{
if (isdigit(input[i]))
{
operation.push(input[i]);
}
else
{
operand2 = operation.top();
temp << operation.top();
operation.pop();
operand1 = operation.top();
temp << operation.top();
operation.pop();
switch(operand1,operand2)
{
case '+': result=operand1 + operand2;
break;
case '-': result=operand1 - operand2;
break;
case '*': result=operand1 * operand2;
break;
case '/': result=operand1 / operand2;
break;
}
operation.push(result);
}
i++;
}
cout << "The result is: "<<temp.str()<<endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
我修改了代码,成功获取了“pop”值,但是操作没有成功。
【问题讨论】:
-
你用的是什么输入法?它在崩溃之前能走多远?如果你的操作栈是空的怎么办?
-
我们知道您更改了代码。只需对问题发表评论即可。我回应了我的答案的变化:)
标签: c++ stack evaluation postfix-notation