2 条题解
-
0
死也不用getline系列(代码质量很低)
闲得慌
using namespace std; stack s; int num,sym; int tmpi; char symin[250]; int main() { for(int i=0;i<250;i++) { char tmp; tmp=getchar(); if(tmp>='0'&&tmp<='9') { tmpi*=10; tmpi+=(tmp-'0'); continue; } if(tmp==' ') { s.push(tmpi); tmpi=0; continue; } if(tmp=='+'||tmp=='-'||tmp=='*'||tmp=='/') { if(num==0) num=i-1; symin[i-num-1]=tmp; continue; } if(tmp=='@') { sym=i-num; break; } } for(int i=0;i
-
0
- 后缀表达式?
运算数1 运算数2 运算符
格式的数学表达式就叫做后缀表达式
- 那正常的数学的
运算数1 运算符 运算数2
表达式是?- 这就是中缀表达式
- 后缀表达式与中缀表达式相比有什么好处?
- 没有运算符优先级的概念,每次遇到运算符都把最新的两个数进行运算得到结果。
#include <bits/stdc++.h> using namespace std; string s; stack<int> sta; int main() { getline(cin, s); int now = 0; //当前处理的整数 for (int i = 0; i < s.length(); i++) { if (s[i] == '@') break; else if ('0' <= s[i] && s[i] <= '9') now = now * 10 + s[i] - '0'; else if (s[i] == ' ') { sta.push(now); now = 0; } else if (s[i] == '+') { int b = sta.top(); sta.pop(); int a = sta.top(); sta.pop(); sta.push(a + b); } else if (s[i] == '-') { int b = sta.top(); sta.pop(); int a = sta.top(); sta.pop(); sta.push(a - b); } else if (s[i] == '*') { int b = sta.top(); sta.pop(); int a = sta.top(); sta.pop(); sta.push(a * b); } else if (s[i] == '/') { int b = sta.top(); sta.pop(); int a = sta.top(); sta.pop(); sta.push(a / b); } } cout << sta.top(); return 0; }
- 后缀表达式?
- 1
信息
- ID
- 550
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 2
- 标签
- (无)
- 递交数
- 61
- 已通过
- 38
- 上传者