100*(2+12)-(20/3)*2
100*(2-12)+(20/3)*(1-2)
10*(3+5*(5-5+1)*100+(5-9*(20-2)/2)*2*20/10)+1000
2/3-10
2+(2*4)
50/(50-20*2+1-9)*(10+12*12-12*10)
50*(50-20*2+1-9)*(10+12*12-12*10)/(1*2*3*4*5*6)
(1)+(1+2)+2*(1+3)
100*(2+12)-20/3*2
7*(8+2-(8/2+5*4))
3+((12+7*9)-15)
((5*(8+7)-5)+30*(7-6/3))*7
8/4*2
8/2*4
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
void step(stack<int>& num, stack<char>& op) {
char top = op.top();
op.pop();
int a, b;
b = num.top();
num.pop();
a = num.top();
num.pop();
//cout << a << top << b << "=";//
if (top == '+')
a += b;
else if (top == '-')
a -= b;
else if (top == '*')
a *= b;
else a /= b;
//cout << a << endl;//
num.push(a);
}
bool cmp(char top, char c) {
if (top == '(') return false;
if (top == '+' || top == '-') {
if (c == '+' || c == '-')
return true;
return false;
}
return true;
}
void pushOp(stack<int>& num, stack<char>& op, char c) {
if (c == ')') {
for (; op.top() != '(';)
step(num, op);
op.pop();
return;
}
if (op.empty() || op.top() == '(') {
//cout << "New: " << c << endl;//
op.push(c);
return;
}
//cout << "Top: " << op.top() << " >> " << "New: " << c << endl;//
for (; cmp(op.top(), c);) { //For the order of calculating
step(num, op);
if (op.empty()) break;
}
op.push(c);
}
int getNum(char* line, int& i, int len) {
int a;
sscanf(line + i, "%d", &a);
//cout << a << endl;//
for (; i < len && line[i] >= '0' && line[i] <= '9'; i++);
return a;
}
int main() {
stack<int> num;
stack<char> op;
char line[101];
int i, len;
for (; scanf("%s", line) != EOF;) {
i = 0;
len = strlen(line);
for (; line[i] == '('; i++) //I's like to get many '(' continously
op.push('(');
num.push(getNum(line, i, len));
for (; i < len;) {
for (; line[i] == ')'; i++) //It's the same with '('
pushOp(num, op, line[i]);
if (i >= len) break;
pushOp(num, op, line[i++]);
for (; line[i] == '('; i++) //...
op.push('(');
num.push(getNum(line, i, len));
}
for (; !op.empty();)
step(num, op);
i = num.top();
num.pop();
//cout << "Empty: " << num.size() << endl;
printf("%d\n", i);
}
return 0;
}

yinizhizhu
- 粉丝: 21
最新资源
- 5种ceemdan组合时间序列预测模型Python代码(包括ceemdan-lstm、ceemdan-cnn-lstm等)
- 江苏移动通信有限责任公司员工绩效考核实施细则精.doc
- 最新国家开放大学电大《优秀广告作品评析答案》网络核心课形考网考作业.docx
- 工程项目管理计划书.doc
- 基于PLC双轴位置控制.docx
- 基于复矢量PI控制器的模型参考自适应三相永磁同步电机高速低载波比无速度传感器控制仿真研究 - MATLAB 宝典
- 第8章-网络营销的策略组合.ppt
- (源码)基于NodeMCU的可视化通知提醒系统.zip
- 系统集成测试(SIT)报告.docx
- 基于MATLAB的GMSK系统的设计仿真.doc
- 离心风机辐射噪声仿真分析:从结构模态到声源辐射噪声的全流程解析 · 辐射噪声 深度版
- 专题讲座资料(2021-2022年)大工秋Java程序设计在线作业.docx
- (源码)基于Arduino的EDeliveryRobot.zip
- Comsol光子晶体仿真技术:拓扑荷、偏振态、三维能带及Q因子计算
- 基于非支配排序的多目标鱼鹰优化算法求解柔性作业车间调度问题的MATLAB实现
- (源码)基于多种编程语言和框架的物联网服务器与客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


