2016 UESTC Training for Data Structures N - 秋实大哥搞算数 CDOJ 1074 栈 表达式求值
2016-05-01 14:38
633 查看
N -
秋实大哥搞算数
给一个表达式,无括号,保证合法,long long以内,整数运算,求值
栈 表达式求值的经典问题,
首先设置一个开始和结束符号#,他们的优先级是最低的,然后开两个栈,运算符栈和运算数栈,如果当前要放的运算符优先级高的话,就放进去,然后继续模拟,如果当前要放的运算符优先级低的话,就取出栈顶的运算符,和运算数栈顶的两个数,进行运算,然后继续检查,直到栈空或者栈顶运算符优先级低了为止,因为栈最先时放了一个#进去,所以保证合法,只有读入到结束符#时,运算符栈才会被算空
当然, 如果栈顶运算符和要加入的运算符相同,也是优先栈顶运算符计算的
end
代码:
秋实大哥搞算数
给一个表达式,无括号,保证合法,long long以内,整数运算,求值
栈 表达式求值的经典问题,
首先设置一个开始和结束符号#,他们的优先级是最低的,然后开两个栈,运算符栈和运算数栈,如果当前要放的运算符优先级高的话,就放进去,然后继续模拟,如果当前要放的运算符优先级低的话,就取出栈顶的运算符,和运算数栈顶的两个数,进行运算,然后继续检查,直到栈空或者栈顶运算符优先级低了为止,因为栈最先时放了一个#进去,所以保证合法,只有读入到结束符#时,运算符栈才会被算空
当然, 如果栈顶运算符和要加入的运算符相同,也是优先栈顶运算符计算的
end
代码:
#include <iostream> #include <vector> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <vector> #include <stack> using namespace std; #define ll long long #define maxn 1000005 char str[maxn]; stack<char> oper; stack<ll> num; bool prior[5][5] = { { 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0 }, { 1, 1, 1, 0, 0 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 } }; int idx(char c) { int x; switch (c) { case '#': x = 0; break; case '+': x = 1; break; case '-': x = 2; break; case '*': x = 3; break; case '/': x = 4; break; default: x = -1; break; } return x; } ll calcluate(ll a, ll b, char c) { ll ans; switch (c) { case '#': ans = b; break; case '+': ans = a + b; break; case '-': ans = a - b; break; case '*': ans = a*b; break; case '/': ans = a / b; break; } return ans; } int main(){ //freopen("input.txt", "r", stdin); int T; scanf("%d", &T); while (T--) { ll nn = 0; while (!num.empty()) num.pop(); while (!oper.empty()) oper.pop(); memset(str, 0, sizeof(char)*maxn); scanf("%s", str); oper.push('#'); int n = strlen(str); str[n++] = '#'; for (int i = 0; i < n; ++i) { if (str[i] >= '0'&&str[i] <= '9') { nn = nn * 10 + str[i] - '0'; } else { num.push(nn); nn = 0; int x = idx(str[i]); while (!oper.empty()) { char c = oper.top(); int y = idx(c); if (prior[y][x]) { oper.pop(); ll a = 0, b = 0; if (!num.empty()) { a = num.top(); num.pop(); } if (!num.empty()) { b = num.top(); num.pop(); } //printf("%d %lld %lld %c\n", i, b, a, c); num.push(calcluate(b, a, c)); } else { oper.push(str[i]); break; } } } } printf("%lld\n", num.top()); } //while (1); return 0; }
相关文章推荐
- 2016 UESTC Training for Data Structures M - 卿学姐失恋了Ⅱ CDOJ 1350 汉诺塔 模拟
- 2016 UESTC Training for Data Structures L - 郭大侠与苦恼 CDOJ 1284 map+启发式合并
- link quiz
- UIImageVIew
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 CDOJ 1342 离线树状数组
- Analog linked list quiz
- 2016 UESTC Training for Data Structures J - 郭大侠与Rabi-Ribi CDOJ 1334 优先队列
- 2016 UESTC Training for Data Structures I - 郭大侠与线上游戏 CDOJ 1339 pb_ds黑科技
- Vawio Sequence
- Android开发:Handler异步通信机制全面解析(包含Looper、Message Queue
- tractor quiz
- 2016 UESTC Training for Data Structures H - 郭大侠与英雄学院 CDOJ 1338 并查集
- Palindrome-detection quiz
- 2016 UESTC Training for Data Structures G - 郭大侠与阴阳家 CDOJ 1337 强行map
- Quick-sort quiz
- 2016 UESTC Training for Data Structures F - 郭大侠与“有何贵干?” CDOJ 1335 线段树 扫描线 离散化
- 2016 UESTC Training for Data Structures E - 卿学姐与城堡的墙 CDOJ 1341 树状数组 逆序对 离散化
- 2016 UESTC Training for Data Structures D - 卿学姐与魔法 CDOJ 1329 堆
- 334. Increasing Triplet Subsequence
- 2016 UESTC Training for Data Structures C - 卿学姐与诡异村庄 CDOJ 1328 并查集