表达式求值以及中缀式转后缀式代码
发布日期:2021-06-29 11:14:09
浏览次数:3
分类:技术文章
本文共 2890 字,大约阅读时间需要 9 分钟。
#include//数据结构表达式计算 #include #include #include #include #include #include using namespace std;//列子 //((1+2)*5+1)/4//1+2*(7-4)/3 stack op;//放操作符 stack od;//放操作数 (正整数)void deal(char str[]);void count();int priorityJudge(char ope1, char ope2);int main() { int times; char str[1005]; scanf("%d", ×); while (times--) { scanf("%s", str); deal(str); } return 0;}void deal(char str[]) { while (!od.empty()) od.pop(); while (!op.empty()) op.pop(); int i = 0, length = strlen(str); op.push('='); while (i < length) { if (isdigit(str[i])) { char s[1010]; int count = 0; while (isdigit(str[i]) || str[i] == '.') { printf("%c", str[i]); s[count++] = str[i++]; } s[count] = '\0'; od.push(atof(s)); }else { if (priorityJudge(op.top(), str[i]) < 0) { op.push(str[i]); i++; }else if(priorityJudge(op.top(), str[i]) == 0) { op.pop(); i++; }else { printf("%c", op.top()); count(); } } } printf("=\n"); printf("%.2lf\n", od.top());}void count() { double a = od.top(); od.pop(); double b = od.top(); od.pop(); char c = op.top(); op.pop(); if (c == '+') od.push(b + a); else if (c == '-') od.push(b - a); else if (c == '*') od.push(b * a); else od.push(b / a);}int priorityJudge(char op1, char op2) { if(op1 == '+' || op1 == '-') { if (op2 == '*' || op2 == '/' || op2 == '(') return -1; else return 1; } if (op1 == '*' || op1 == '/') { if (op2 == '(') return -1; else return 1; } if (op1 == '(' && op2 == ')') return 0; if (op1 == '=' && op2 == '=') return 0; return -1;}
#include#include #include #include #include #include #include using namespace std;stack s;void stringChange(string & temp, string & str);int priorityJudge(char c);//1.000+2/4=//((1+2)*5+1)/4=int main() { int times; string str, tempString = ""; cin >> times; while (times--) { cin >> str; stringChange(tempString, str); cout << tempString << endl; tempString.clear(); str.clear(); } return 0;}void stringChange(string & temp, string & str) { //数据结构中缀式转后缀式 while (!s.empty()) s.pop(); int i = 0, length = str.size(); s.push('#'); while (i < length) { if (str[i] == '(') s.push(str[i++]); else if (str[i] == ')') { while (s.top() != '(') { temp += s.top(); temp += ' '; s.pop(); } s.pop(); i++; }else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') { while (priorityJudge(s.top()) >= priorityJudge(str[i])) { temp += s.top(); temp += ' '; s.pop(); } s.push(str[i++]); }else if ((str[i] <= '9' && str[i] >= '0') || str[i] == '.') { while ((str[i] <= '9' && str[i] >= '0') || str[i] == '.') { temp += str[i++]; } temp += ' '; }else if (str[i] == ',' || str[i] == '=') i++; } while (s.top() != '#') { temp += s.top(); s.pop(); temp += ' '; } temp += '=';}int priorityJudge(char c) { if (c == '+' || c == '-') return 1; else if (c == '*' || c == '/') return 2; return 0;}
转载地址:https://blog.csdn.net/ZWHSOUL/article/details/80018155 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月18日 21时14分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
80后,天才程序员, Facebook 第一任 CTO,看看开挂的人生到底有多变态?
2019-04-29
「第四篇」电赛控制题可以准备一些什么?
2019-04-29
「第五篇」全国电子设计竞赛-电源题设计方案总结
2019-04-29
「第六篇」对于电赛,我们应该看重什么?
2019-04-29
树莓派翻车了
2019-04-29
垃圾分类背后的数据和真相
2019-04-29
PID算法搞不懂?看这篇文章就够了。
2019-04-29
这位电子工程师,你不能错过。
2019-04-29
十八般武艺教你如何解决问题
2019-04-29
「权威发布」2019年大学生电子设计竞赛,仪器设备和主要元器件清单
2019-04-29
「重磅猜题之第二篇」2019年大学生电子设计竞赛
2019-04-29
知乎:硬件和软件哪个吃香?
2019-04-29
中国深圳,600架无人机的盛典!
2019-04-29
干货分享 JVM 之第 3 篇 —— Java 内存结构相关
2019-04-29
干货分享 JVM 之第 5 篇 —— 类加载器
2019-04-29
干货分享 JVM 之第 6 篇 —— SpringBoot2.0 框架性能调优
2019-04-29
基于 Hystrix 高并发服务限流第 1 篇 —— 必须了解的相关概念
2019-04-29
基于 Hystrix 高并发服务限流第 2 篇 —— 服务隔离(线程池隔离、信号量隔离)
2019-04-29
基于 Hystrix 高并发服务限流第 3 篇 —— 服务熔断、服务降级
2019-04-29