#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;
}
栈的应用:中缀表达式转化为后缀表达式(逆波兰表达式)
最新推荐文章于 2022-10-11 17:49:10 发布