中缀表达式转后缀表达式C++代码
- //MyStack.h
- #include<iostream>
- usingnamespacestd;
- template<classElemType>classMyStack
- {
- public:
- conststaticintMAXSIZE=100;
- ElemTypedata[MAXSIZE];
- inttop;
- public:
- voidinit();//初始化栈
- boolempty();//判断栈是否为空
- ElemTypegettop();//读取栈顶元素(不出栈)
- voidpush(ElemTypex);//进栈
- ElemTypepop();//出栈
- };
- template<classT>voidMyStack<T>::init()
- {
- this->top=0;
- }
- template<classT>boolMyStack<T>::empty()
- {
- returnthis->top==0?true:false;
- }
- template<classT>TMyStack<T>::gettop()
- {
- if(empty())
- {
- cout<<"栈为空!\n";
- exit(1);
- }
- returnthis->data[this->top-1];
- }
- template<classT>voidMyStack<T>::push(Tx)
- {
- if(this->top==MAXSIZE)
- {
- cout<<"栈已满!\n";
- exit(1);
- }
- this->data[this->top]=x;
- this->top++;
- }
- template<classT>TMyStack<T>::pop()
- {
- if(this->empty())
- {
- cout<<"栈为空!\n";
- exit(1);
- }
- Te=this->data[this->top-1];
- this->top--;
- returne;
- }
- //PrefixToPostfix.h
- #include<vector>
- usingnamespacestd;
- boolisoperator(charop);//判断是否为运算符
- intpriority(charop);//求运算符优先级
- voidpostfix(charpre[],charpost[],int&n);//把中缀表达式转换为后缀表达式
- doubleread_number(charstr[],int*i);//将数字字符串转变成相应的数字
- doublepostfix_value(charpost[]);//由后缀表达式字符串计算相应的中值表达式的值
- //PrefixToPostfix.cpp
- #include"MyStack.h"
- #include"PrefixToPostfix.h"
- #include<iostream>
- usingnamespacestd;
- voidmain()
- {
- MyStack<int>stack;
- stack.init();
- //charpre[]="22/(5*2+1)#";
- charexp[100];
- cout<<"输入表达式(中缀,以#结束):";
- cin>>exp;
- charpost[100];
- //cout<<"中缀表达式为:"<<pre<<endl;
- intn=0;//返回后缀表达式的长度
- postfix(exp,post,n);
- cout<<"后缀表达式为:";
- for(inti=0;i<n;i++)
- cout<<post[i];
- cout<<"\n由后缀表达式计算出的数值结果:";
- cout<<postfix_value(post)<<endl;
- system("pause");
- }
- boolisoperator(charop)
- {
- switch(op)
- {
- case'+':
- case'-':
- case'*':
- case'/':
- return1;
- default:
- return0;
- }
- }
- intpriority(charop)
- {
- switch(op)
- {
- case'#':
- return-1;
- case'(':
- return0;
- case'+':
- case'-':
- return1;
- case'*':
- case'/':
- return2;
- default:
- return-1;
- }
- }
- //把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)
- voidpostfix(charpre[],charpost[],int&n)
- {
- inti=0,j=0;
- MyStack<char>stack;
- stack.init();//初始化存储操作符的栈
- stack.push('#');//首先把结束标志‘#’放入栈底
- while(pre[i]!='#')
- {
- if((pre[i]>='0'&&pre[i]<='9')||pre[i]=='.')//遇到数字和小数点直接写入后缀表达式
- {
- post[j++]=pre[i];
- n++;
- }
- elseif(pre[i]=='(')//遇到“(”不用比较直接入栈
- stack.push(pre[i]);
- elseif(pre[i]==')')//遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式
- {
- while(stack.gettop()!='(')
- {
- post[j++]=stack.pop();
- n++;
- }
- stack.pop();//将“(”出栈,后缀表达式中不含小括号
- }
- elseif(isoperator(pre[i]))
- {
- post[j++]='';//用空格分开操作数(
- n++;
- while(priority(pre[i])<=priority(stack.gettop()))
- {
- //当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程
- post[j++]=stack.pop();
- n++;
- }
- stack.push(pre[i]);//当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈
- }
- i++;
- }
- while(stack.top)//将所有的操作符加入后缀表达式
- {
- post[j++]=stack.pop();
- n++;
- }
- }
- doubleread_number(charstr[],int*i)
- {
- doublex=0.0;
- intk=0;
- while(str[*i]>='0'&&str[*i]<='9')//处理整数部分
- {
- x=x*10+(str[*i]-'0');
- (*i)++;
- }
- if(str[*i]=='.')//处理小数部分
- {
- (*i)++;
- while(str[*i]>='0'&&str[*i]<='9')
- {
- x=x*10+(str[*i]-'0');
- (*i)++;
- k++;
- }
- }
- while(k!=0)
- {
- x/=10.0;
- k--;
- }
- returnx;
- }
- doublepostfix_value(charpost[])
- {
- MyStack<double>stack;//操作数栈
- stack.init();
- inti=0;
- doublex1,x2;
- while(post[i]!='#')
- {
- if(post[i]>='0'&&post[i]<='9')
- stack.push(read_number(post,&i));
- elseif(post[i]=='')
- i++;
- elseif(post[i]=='+')
- {
- x2=stack.pop();
- x1=stack.pop();
- stack.push(x1+x2);
- i++;
- }
- elseif(post[i]=='-')
- {
- x2=stack.pop();
- x1=stack.pop();
- stack.push(x1-x2);
- i++;
- }
- elseif(post[i]=='*')
- {
- x2=stack.pop();
- x1=stack.pop();
- stack.push(x1*x2);
- i++;
- }
- elseif(post[i]=='/')
- {
- x2=stack.pop();
- x1=stack.pop();
- stack.push(x1/x2);
- i++;
- }
- }
- returnstack.gettop();
- }
运行结果: