qq_26163659 2015-04-07 12:35 采纳率: 36.4%
浏览 2032

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();
}

  • 写回答

5条回答 默认 最新

  • oyljerry 2015-04-07 13:59
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?