我输入的是自带测试用例运算式
(((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();
}
c++数据结构堆栈加减乘除运算器 我从教科书上copy了一段代码输入运算式直接停止工作 为什么会这样?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答
悬赏问题
- ¥15 急matlab编程仿真二阶震荡系统
- ¥20 TEC-9的数据通路实验
- ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
- ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
- ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
- ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
- ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
- ¥15 python爬取bilibili校园招聘网站
- ¥30 求解达问题(有红包)
- ¥15 请解包一个pak文件