您的位置:首页 > 其它

栈的应用:中缀表达式转化为后缀表达式(逆波兰表达式)

2016-02-04 20:06 387 查看
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "MyownStack.h"
using  namespace std;

const int Max = 1000;

int main()
{
MyOwnStack <char> s;
char c,e;

cout<<"请输入中缀表达式,以#作为结束标志:\n";
while(1){
scanf("%c",&c);
while(c >= '0'&&c <= '9'){
cout<<c;
scanf("%c",&c);
if(c<'0'||c>'9')
cout<<" ";
}
if('#' == c)
break;
if(')' == c)
while(1){
e=s.Pop();
if('(' == e)
break;
cout<<e<<" ";
}
if('+' == c || '-' == c){
while(1){
if(!s.stackLen()){
s.Push(c);
break;
}
e=s.Pop();
if('(' == e){
s.Push(e);
s.Push(c);
break;
}
cout<<e<<" ";
}
}
if( '*' == c|| '/' == c|| '(' == c)
s.Push(c);
}
while(s.stackLen()){
e=s.Pop();
cout<<e<<" ";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: