资源简介
描述
编写一个程序可以完成基本的带括号的四则运算。其中除法(/)是整除,并且在负数除法时向0取整。(C/C++/Java默认的除法就是向0取整,python默认的是向负无穷取整。)
例如计算 100 * ( 2 + 12 ) - (20 / 3) * 2, 结果是1388。
输入
一个长度不超过100的字符串,代表要计算的算式。包含数字0-9以及+-*/()。
输入保证计算过程不会超过32位有符号整数,并且其中的'-'都是减号没有负号。

代码片段和文件信息
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
#include
#include
#include
using namespace std;
void step(stack& num stack& 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& num stack& 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 num;
stack 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;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2290 2017-09-29 16:30 Caculator.cpp
----------- --------- ---------- ----- ----
2290 1
相关资源
- 去掉代码注释的工具java \\ c\\c++、ph
- 算法设计、分析与实现从入门到精通
- 全世界最经典的数据结构教材,Horo
- C++程序设计原理与实践.pdf
- C++ 程序设计语言:第4部分 标准库原
- android与c++通过socket通信
- socket编程实验报告
- C++Qt5实现雷达及余晖扫描,探测发现
- parasoft c++Test9.0破解
- 白话 C++ 完整版
- PDFlib-6.0.2-Windows库以及java c++开发
- 聊天室系统源码C++ 服务器,PC客户端
- Visual C++6.0助手破解版
- C++ Tutorial for Rational Rhapsody
- C++程序设计语言特别版 Bjarne Stroustr
- C++最强版中国象棋
- Java版计算器源代码带括号
- Android视频监控系统客户端及服务器端
- Android opencv c++配置工程
- 《C++ for Java Programmers》高清完整英文
- C++ 条码,二维码生成程序Qt 界面
- VISSIM二次开发案例与框架VBA,C++,M
- java JGL标准程序库类似C++的STL
- C++程序设计(谭浩强完整版)
- 算法与数据结构之LeetCode题目详解PD
- java与c++通过socket通信
- 重构源代码C1-c++版本
- 使用JNA的jar包
- Java客户端上传图片文件到c++服务器
- Absolute C++ 6th Edition
评论
共有 条评论