表达式计算(中缀转后缀,然后求值)
来源:互联网 发布:淘宝怎么好友代付 编辑:程序博客网 时间:2024/05/01 19:30
呕心沥血。。。写了一天,终于把这个恶心的东西写完了。。
下面的代码可以实现给出一个表达式,然后直接求值,应该算是表达式求值的裸题了,蓝桥杯有一道表达式计算就是这个题
题目链接:http://lx.lanqiao.cn/problem.page?gpid=T419
参考博客:http://blog.csdn.net/reidsc/article/details/54669433用后缀表达式求值的时候是照着写的。。
下面是我的代码:
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cctype>#include <map>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 100+20#define M 1000000+10#define ll long longusing namespace std;char a[100];//存放转好的后缀表达式stack<char>hpc;stack<int>s3;//计算栈int num[21];//暂时存放数字map<char,int> mp;//定义运算符优先级char s[100];//原来的中缀表达式int len=0,sum;int calc(int x,int y,char op)//两个数的运算{ if(op=='+')return x+y; if(op=='-')return x-y; if(op=='/'&&y!=0)return x/y; if(op=='*')return x*y;}void init()//把中缀表达式转换成后缀表达式{ mp['+']=1; mp['-']=1; mp['*']=2; mp['/']=2; mp['(']=0; scanf("%s",s); int length=strlen(s); for(int i=0; i<length; i++) { if(isdigit(s[i]))//当前这一位为数字 { while(isdigit(s[i]))// { a[len++]=s[i]; i++; } i--; a[len++]='#';//在数字后面加分隔符 } else//是操作符 { if(hpc.empty()||s[i]=='(')//栈为空且是左括号 { hpc.push(s[i]); } else { if(s[i]==')')//遇到右括号时把前面的直到遇到左括号全部出栈并加入后缀表达式中 { while(hpc.top()!='(') { a[len++]=hpc.top(); hpc.pop(); } hpc.pop();//把左括号出栈 } else { if(mp[s[i]]>mp[hpc.top()])//当当前符号的优先级高时,直接入栈 { hpc.push(s[i]); } else { while(!hpc.empty()&&mp[hpc.top()]>=mp[s[i]])//当当前符号的优先级比较低时,出栈栈中优先级<=它的 { a[len++]=hpc.top(); hpc.pop(); } hpc.push(s[i]);//把当前的入栈 } } } } } while(!hpc.empty())//把栈中剩下的元素加入后缀表达式中 { a[len++]=hpc.top(); hpc.pop(); }}int solve()//后缀表达式计算{ int x,y,tmp=0,k=0; for(int i=0; i<len; i++) { if(a[i]=='#') continue; else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/') { x=s3.top(); s3.pop(); y=s3.top(); s3.pop(); x=calc(y,x,a[i]); s3.push(x); } else//是数字 { if(a[i+1]=='#') { num[k++]=a[i]-'0'; for(int i=0; i<k; i++) tmp+=(num[i]*(int)pow(10,k-i-1)); s3.push(tmp); tmp=0; k=0; } else { num[k++]=a[i]-'0'; } } } return s3.top();}int main(){ init();//转换成后缀表达式 printf("%d\n",solve()); return 0;}//9+(3-1)*3+10/2//1+((23+34)*5)-6
0 0
- 表达式计算(中缀转后缀,然后求值)
- 栈的应用--算术表达式的求值(中缀转后缀然后计算后缀表达式的值)
- 表达式求值(中缀转后缀及后缀表达式求值)
- 中缀表达式求值、后缀表达式求值、中缀转后缀、前缀
- 蓝桥杯 算法训练 表达式的计算(中缀转后缀表达式求值)
- 计算四则表达式(中缀式转后缀式,然后计算结果)
- 中缀转后缀 和 后缀表达式求值
- 中缀表达式转后缀表达式 (中缀表达式的计算)
- 关于中缀表达式转后缀表达式 然后求值问题的实现
- 中缀 后缀表达式求值
- NYOJ 35 表达式求值 【中缀转后缀+后缀表达式求值】
- 中缀表达式转后缀表达式求值(栈的应用)
- 中缀表达式转后缀表达式求值(模板)
- 中缀表达式转后缀表达式并求值
- 中缀表达式转后缀表达式求值
- 中缀表达式转后缀表达式并求值
- 中缀表达式转后缀表达式求值
- 中缀表达式转后缀表达式并求值
- 可伸缩架构简短系列
- 杭电oj 1175连连看
- 紫书例题6-3 UVa442 (栈
- nginx 常见错误及优化
- 单例模式
- 表达式计算(中缀转后缀,然后求值)
- 《java学习笔记》MD5加密算法
- mysql-按天或者按月统计数据
- SSM框架整合(三)
- ORA-12519: TNS:no appropriate service handler found 解决
- ubuntu下安装lua的时候出现 lua.c:80:31: fatal error: readline/readline.h: 没有那个文件或目录 这样的报错
- [POI2010]TES-Intelligence Test 洛谷3500 二分
- 安卓目录res/menu中showAsAction用法
- gradle下载更新依赖库失败的解决办法