struct word //定义word结构体类型,其中包含两个变量 { };
struct STACK{ //定义栈的结构体类型 };
void MakeNull(STACK &S)//初始化栈 { }
bool Empty(STACK S)//判栈空 { }
word Top(STACK S)//弹出栈顶元素 {
word empty={'0',0}; if(Empty(S))
if(S.top>maxlength-1)
return 1; S.top=maxlength; int top;
word elements[maxlength]; char type; double num;
else
return 0;
}
return empty;
else
return(S.elements[S.top]);
word Pop(STACK &S)//删除栈顶元素 {
if(Empty(S)) { }
return S.elements[S.top++]; }
std::cerr<<\栈空不能出栈\
void Push(word x,STACK &S)//将元素压入栈中 { }
word Display(STACK S) {
word empty={'0',0}; cout<<\栈中元素有\ while(!Empty(S)) {
if (S.top==0) { }
S.top=S.top-1; S.elements[S.top]=x;
std::cerr<<\栈满不能入栈\
empty=Pop(S); cout< cout< word iDisplay(STACK S) { word empty={'0',0}; cout<<\栈中元素有\ while(!Empty(S)) { empty=Pop(S); cout< cout< void Exchange(word a[100],word b[100],int m,int &l) { //将中缀表达式转换为后缀表达式 STACK O; //定义空栈O MakeNull(O); int i; for(i=0;i if(a[i].type=='0')//如果为数字时 { b[l]=a[i];//直接存入后缀表达式数组 l++; } if(a[i].type=='(') { Push(a[i],O); Display(O); b[l++]=Pop(O); if (a[i].type=='+'||a[i].type=='-') { while(!Empty(O)&&Top(O).type!='(') { } Display(O); Push(a[i],O); } Display(O); while(!Empty(O)&&Top(O).type!='-'&&Top(O).type!='+'&&Top(O).type! } if(a[i].type=='/'||a[i].type=='*') { ='(') b[l++]=Pop(O); Display(O); Push(a[i],O); Display(O); while(Top(O).type!='(') { b[l++]=Pop(O); } if(a[i].type==')') { Display(O); } Pop(O); Display(O); } while(!Empty(O))//当栈非空时,弹出栈顶元素存入后缀表达式数组 { b[l++]=Pop(O); } Display(O); } int j; for(j=0;j if (b[j].type=='0') { } else cout<