郁闷的C小加(三)中缀转前后缀并计算
2017-08-25 23:07
190 查看
模板:各种缀的转换以及计算
代码:
#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; }
相关文章推荐
- NYOJ 257 郁闷的C小加(一) (栈 、中缀转后缀)
- 中缀算术转后缀算术表达式并计算的函数
- 栈应用之中缀转后缀表达式计算(C++、JAVA)
- java实现中缀转后缀,后缀计算值
- 中缀转后缀表达式||后缀表达书计算
- 中缀转后缀并计算
- 中缀后缀表达式转换与计算
- 【转】算术表达式中缀形式转后缀形式,并基于后缀式和栈进行计算
- c++ 表达式计算--中缀转后缀再计算
- 栈的应用--算术表达式的求值(中缀转后缀然后计算后缀表达式的值)
- 蓝桥杯 算法训练 表达式的计算(中缀转后缀表达式求值)
- 【叶子函数分享十七】中缀算术转后缀算术表达式并计算的函数
- 【叶子函数分享十七】中缀算术转后缀算术表达式并计算的函数
- 数据结构-栈应用(中缀转后缀并计算结果)
- 中缀式转后缀表达式 -NYOJ 题目267郁闷的C小加(二)
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 中缀表达式转化为后缀表达式,并计算结果
- #C++实现先中缀转后缀的算术表达式计算
- qt 计算器实现(中缀转后缀,后缀计算)
- 逆波兰表达式计算(后缀表达式,中缀转后缀)