c++数据结构堆栈加减乘除运算器 我从教科书上copy了一段代码但是输入后直接停止工作求解

我输入的是自带测试用例运算式
(((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();
}

设置断点单步调试一下。应该是某个地方指针操作不正确。

大神运行一下试试呗 我试了很久并没试出来

你之前发过程序了,程序没有错。
你的输入有问题,你应该输入半角的括号。

而且上次你的程序还正确,这次直接连linclude都丢字符了,估计你是直接抄的http://ask.csdn.net/questions/173139

 (((60+40)/50*(16-4))

直接复制代码的话可能有全半角的问题,建议自己重新敲

#include
#include
using namespace std;
double read_and_evaluate(istream& ins);
void evaluate_stack_tops(stack& numbers, stack& operations);
int main()
{
double answer;
answer = read_and_evaluate(cin);
cout << "Type a fully parenthesized arithmetic expression:"<< answer;
cin >> answer;
return 0;
}
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);

    }
    //查找字符串s中首次出现字符c的位置strchr(char *s,char c)
    else if(strchr("+-*/",ins.peek())!=NULL)
    {
        ins>>symbol;
        //这里它写错了
        operations.push(symbol);

    }
    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)
{

//输入检测,numbers里边至少要有两个值,operation中至少一个值
if (numbers.size() > 1 && operations.size() >0)
{
    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;
    }
    cout << operand1 << ":" << operand2 <<":" << numbers.top() <<endl;
    operations.pop();
}

}



输入首先就有问题,应该改成(((60+40)/50)*(16-4))因为每次运算都是以')'作为开始符
代码错误主要就是stack是模版类,所以要给它参数。另外就是它入栈operand的时候写错了