我输入的是自带测试用例运算式
(((60+40)/50*(16-4))
#include
#include
#include
#include
#include
using namespace std;
double read_and_evaluate(istream& ins);
void evaluate_stack_tops(stack& numbers,stack& operations);
int main()
{
double answer;
cout<<"Type a fully parenthesized arithmetic expression:"< answer= read_and_evaluate(cin);
cout return EXIT_SUCCESS;
}
double read_and_evaluate(istream& ins)
{
const char DECIMAL = '.';
const char RIGHT_PARENTHESIS = ')';
stack numbers;
stack operations;
double number;
char symbol;
while(ins&&ins.peek() != '\n')
{
if(isdigit(ins.peek())||(ins.peek()==DECIMAL))
{
ins>>number;
numbers.push(number);
}
else if(strchr("+-*/",ins.peek())!=NULL)
{
ins>>symbol;
operations.push(number);
}
else if(ins.peek()==RIGHT_PARENTHESIS)
{
ins.ignore();
evaluate_stack_tops(numbers,operations);
}
else
ins.ignore();
}
return numbers.top();
}
void evaluate_stack_tops(stack& numbers, stack& operations)
{
double operand1,operand2;
operand2=numbers.top();
numbers.pop();
operand1=numbers.top();
numbers.pop();
switch(operations.top())
{
case '+': numbers.push(operand1+operand2);
break;
case '-': numbers.push(operand1-operand2);
break;
case '*': numbers.push(operand1*operand2);
break;
case '/': numbers.push(operand1/operand2);
break;
}
operations.pop();
}
把stack的定义,pop,peek等方法加进去试试
通过在线编译,没有错。
http://download.csdn.net/detail/brave2002honest/8546565
仿照这个例子翻译成C++语言的,别总看书里的例子,自己摸索摸索会有很多收获。
无法执行到底是什么现象? 说明白点,是不是编译器问题
明显括号对不上,多了一个左括号吧?