郁闷的C小加(三)中缀转前后缀并计算
来源:互联网 发布:新电脑软件下载 编辑:程序博客网 时间:2024/05/22 03:34
模板:各种缀的转换以及计算
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn = 1234;int T,n,m;int ranks[maxn];//优先级char pre[maxn];//存前缀表达式char suf[maxn];//存后缀表达式void init_suf(){ ranks['=']=-1; ranks['(']=0; ranks['+']=ranks['-']=1; ranks['*']=ranks['/']=2; ranks[')']=3;}void init_pre(){ ranks['=']=-1; ranks['(']=1; ranks['+']=ranks['-']=1; ranks['*']=ranks['/']=2; ranks[')']=1;}double cal(char ch,double x,double y){ if(ch=='+') return x+y; else if(ch=='-') return x-y; else if(ch=='*') return x*y; else return x/y;}void change_pre(char a[])//中缀转前缀{ init_pre(); stack<char>st; char b[maxn]; int j=0; int len=strlen(a); b[j++]='='; for(int i=len-2;i>=0;i--) { if(isdigit(a[i])||a[i]=='.')//小数情况 b[j++]=a[i]; else { b[j++]='#';//分割运算数 if(a[i]==')') st.push(a[i]); else if(a[i]=='(') { while(!st.empty()&&st.top()!=')') { b[j++]=st.top(); st.pop(); } st.pop(); } else { while(!st.empty()&&ranks[a[i]]<ranks[st.top()])//把优先级大于他的都出栈 { b[j++]=st.top(); st.pop(); } st.push(a[i]); } } } while(!st.empty())//最后清空栈 { b[j++]=st.top(); st.pop(); } b[j]='\0'; int k=0; for(int i=j-1;i>=0;i--) pre[k++]=b[i]; pre[k]='\0';}double calculate_pre()//前缀表达式表达式的计算{ stack<double>q; int len=strlen(pre); for(int i=len-2;i>=0;i--) { if(pre[i]==' '||pre[i]=='#') continue; else if(isdigit(pre[i])) { while(isdigit(pre[i])||pre[i]=='.')//小数处理 i--; i++; double t=atof(&pre[i]);//atof:将字串转换成浮点型数 q.push(t); } else { double a=q.top(); q.pop(); double b=q.top(); q.pop(); q.push(cal(pre[i],a,b)); } } return q.top();}void change_suf(char a[])//中缀转后缀{ init_suf(); stack<char>st; int j=0; int len=strlen(a); st.push('=');//现将=压进栈为了之后的运算符好比较优先级 for(int i=0;a[i]!='=';i++) { if(isdigit(a[i])||a[i]=='.')//小数情况 suf[j++]=a[i]; else { suf[j++]='#';//分割运算数 if(a[i]=='(') st.push(a[i]); else if(a[i]==')') { while(st.top()!='(') { suf[j++]=st.top(); st.pop(); } st.pop(); } else { while(ranks[a[i]]<=ranks[st.top()])//把优先级大于他的都出栈 { suf[j++]=st.top(); st.pop(); } st.push(a[i]); } } } while(!st.empty())//最后清空栈 { suf[j++]=st.top(); st.pop(); } suf[j]='\0';}double calculate_suf()//后缀表达式的计算{ char c[maxn]; int j=0; stack<double>st; for(int i=0;suf[i]!='=';i++) { if(isdigit(suf[i])||suf[i]=='.') c[j++]=suf[i]; else { if(j!=0) { st.push(atof(c));//字符串转数字函数的运用 memset(c,'\0',sizeof(c)); j=0; } if(suf[i]!='#') { double n1,n2; n1=st.top(); st.pop(); n2=st.top(); st.pop(); st.push(cal(suf[i],n2,n1));//运算并入栈 } } } return st.top();}void print(char pr[])//输出{ int i=0; while(pr[i]=='#') i++; printf("%c",pr[i++]); for(i=i;pr[i];i++) { if(pr[i]=='.'||(isdigit(pr[i])&&(isdigit(pr[i-1])||pr[i-1]=='.'))) printf("%c",pr[i]); else if(pr[i]!='#') printf(" %c",pr[i]); } printf("\n");}int main(){ scanf("%d",&T); char a[maxn]; while(T--) { scanf("%s",a); change_pre(a); change_suf(a); print(pre); print(suf); //printf("%.2f\n",calculate_suf()); printf("%.2f\n",calculate_pre()); } return 0;}
阅读全文
1 0
- 郁闷的C小加(三)中缀转前后缀并计算
- 中缀式转后缀表达式 -NYOJ 题目267郁闷的C小加(二)
- NYOJ 257 郁闷的C小加(一) (栈 、中缀转后缀)
- NYOJ257郁闷的C小加(一)_中缀转后缀
- nyoj-409 郁闷的C小加(三) (表达式求值,中缀式转前缀式,中缀式转后缀式)
- 中缀式与后缀式的转化 郁闷的C小加(一)
- nyoj267郁闷的c小加 中缀表达式转后缀求值
- NYOJ 257 郁闷的C小加(一)(中缀式变后缀式)
- nyoj 467 中缀式变后缀式 nyoj 257 郁闷的C小加(一)
- nyoj-257-郁闷的C小加(一 )中缀式变后缀式
- 郁闷的C小加(三)
- 郁闷的C小加(三)
- 郁闷的C小加(三)
- 中缀转后缀并计算
- 郁闷的C小加(三)(nyoj 409)
- NYOJ--郁闷的C小加(三)
- NYOJ 409 郁闷的C小加(三)
- 郁闷的C小加(三) 409
- 一份快速实用的 tcpdump 命令参考手册
- LeetCode Sum of Left Leaves
- 基于Redis的分布式锁的简单应用
- Docker简单教程
- 推荐一个github上前端大神的作品,有兴趣大家去看看。https://github.com/bailicangdu
- 郁闷的C小加(三)中缀转前后缀并计算
- JMeter-配置元件-计数器
- 02 java里的创建类对象
- 你不知道的 React Router 4
- FreeCodeCamp基础算法题答案解析
- 浏览器渲染过程工作原理
- tensorflow-入门-01-Ubuntu安装tensorflow以及常见错误libcudnn.Version cannot open shared object file No such
- tensorflow安装所遇问题:libcudnn.so.6:cannot open sharedobject file: No such file or directory
- 51nod 1603 限高二叉排列树 计数dp