3 条题解
-
3
不想多思考,表达式的值代码如下,包教包会,一个赞拿走
#include <bits/stdc++.h> #define ll long long using namespace std; stack<char> s1; stack<int> s, s2; char now; int x, y; void work(string c) { for (int i = 0; i < c.length(); i++) { if (c[i] >= '0' && c[i] <= '9') { now *= 10; now += c[i] - '0'; } else if (c[i] == '.') { s.push(now); now = 0; } else if (c[i] == '+') { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(y + x); } else if (c[i] == '-') { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(y - x); } else if (c[i] == '*') { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(y * x); } else if (c[i] == '/') { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(y / x); } } } int main() { int now = 0; string c; cin >> c; for (int i = 0; i < c.length(); i++) { if (c[i] >= '0' && c[i] <= '9') { s2.push(c[i]); if (!(c[i + 1] >= '0' && c[i + 1] <= '9')) s2.push('.'); } else if (c[i] == '+' || c[i] == '-') { while (!s1.empty() && s1.top() != '(') { s2.push(s1.top()); s1.pop(); } s1.push(c[i]); } else if (c[i] == '*' || c[i] == '/') { while (!s1.empty() && (s1.top() == '*' || s1.top() == '/')) { s2.push(s1.top()); s1.pop(); } s1.push(c[i]); } else if (c[i] == '(') s1.push(c[i]); else if (c[i] == ')') { while (s1.top() != '(') { s2.push(s1.top()); s1.pop(); } s1.pop(); } } while(!s1.empty()) { s2.push(s1.top()); s1.pop(); } while(!s2.empty()) { s1.push(s2.top()); s2.pop(); } string cx = ""; while(!s1.empty()) { cx += s1.top(); s1.pop(); } work(cx); cout << s.top() << endl; return 0; }
- 1
信息
- ID
- 322
- 时间
- 1000ms
- 内存
- 16MiB
- 难度
- 1
- 标签
- 递交数
- 40
- 已通过
- 30
- 上传者