Files
python/TangDou/LuoGuBook/P1449_2.cpp

64 lines
1.9 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
stack<int> n;
int s, x, y;
/**
测试用例 :
3.5.2.-*7.+@ 3*(5–2)+7 16
3.5.2.-*#:7 2 3#.+@ 3*(5–2)+(7+2)*(2+3) 54
*/
//快读
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int main() {
char ch;
do { //(1)上来就读入,读完了再看是不是结束符,就是干了再说~
ch = getchar(); //(2)读入字符的办法
//对增加的条件进行判断
if (ch == '#') {
//读入一个:
char x = getchar();//放弃掉
int a = read();
int b = read();
int c = read();
n.push((a + b) * (b + c));
//read会吃掉最后的#结束符
x = getchar(); //再吃一个.
//cout<<endl;
} else if (ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; //(3)按字符读入转为数字的办法
else if (ch == '.') //表示数字结束
n.push(s), s = 0; //入栈,重新计数
else if (ch != '@') { //操作符
x = n.top();
n.pop();
y = n.top();
n.pop();
switch (ch) {
case '+':
n.push(y + x);
break;
case '-':
n.push(y - x); //注意顺序
break;
case '*':
n.push(y * x);
break;
case '/':
n.push(y / x); //注意顺序
break;
}
}
} while (ch != '@');
printf("%d\n", n.top());
return 0;
}