WEISS的书看到栈,顺手把中缀转后缀写了出来。。照着书的思路写的。。只能支持加号乘号左右括号。。
乱写一气,功能实现了就好。。栈的实现就不写了。。太简单了。。
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include "Stack.h"
- void
- DispPostFix(char res[],int len)
- {
- Stack s;
- s=CreateStack();
- for(int i=0;i<len;i++)
- {
- char c=res[i];
- switch(c)
- {
- case '+':
- while(true)
- {
- if(IsEmpty(s)|| Top(s)=='(' )
- {
- Push(c,s);
- break;
- }
- if(Top(s)=='*' || Top(s)=='+')
- {
- printf("%c",Top(s));
- Pop(s);
- }
- }
- break;
- case '*':
- while(true)
- {
- if(IsEmpty(s)||Top(s)=='+' || Top(s)=='(' )
- {
- Push(c,s);
- break;
- }
- else
- {
- printf("%c",Top(s));
- Pop(s);
- }
- }
- break;
- case '(':
- Push(c,s);
- break;
- case ')':
- while(Top(s)!='(')
- {
- printf("%c",Top(s));
- Pop(s);
- }
- Pop(s);
- break;
- default:
- printf("%c",c);
- break;
- }
- }
- while(!IsEmpty(s))
- {
- printf("%c",Top(s));
- Pop(s);
- }
- DisposeStack(s);
- }
- int main()
- {
- char inFix[]="a+b*c+(d*e+f)*g";
- DispPostFix(inFix,sizeof(inFix)/sizeof(inFix[0])-1);
- return 0;
- }