此表达式求值程序利用Stack实现 /**/ /******************************Date: 2006 - 10 -24Description: This programe is aiming to carry out operator counting by using of sequence stack*******************************/ #include " stdio.h " #define MAXSIZE 128 typedef char DataType; int pIFX = 0 ; int pPFX = 0 ; int pTP = 0 ; /**/ /***Define a Stack Struct***/ typedef struct ... { DataType stack[MAXSIZE]; } SeqStack; /**/ /**********初始化PFX IFX************/ SeqStack PFX;SeqStack IFX;SeqStack TP; /**/ /***************************Function: Stack pushParam: SeqStack ss, char datReturn: SeqStack****************************/ SeqStack Push(SeqStack s, char dat) ... { s.stack[pPFX] = dat; return s; } /**/ /***************************Function: Stack popParam: SeqStack ss, char datReturn: SeqStack****************************/ SeqStack Pop(SeqStack s, char dat) ... { } int CtoD( char c) ... { return(c - '0');} char DtoC( int x) ... { return(x + 48);} char InputPression( void ) ... { int i = 0; char ch; scanf("%c",&ch); while(ch!= '=') ...{ IFX.stack[i ++] = ch; scanf("%c",&ch); } IFX.stack[i] = '=';} void PrintStack(SeqStack s) ... { int i = 0; while(s.stack[i] != '=') printf("%c",s.stack[i++]); getchar(); getchar(); } SeqStack toSufix() ... { int pIFX = 0; char ch; TP.stack[pTP++] = '='; while(IFX.stack[pIFX]!= '=') ...{ ch = IFX.stack[pIFX]; switch(ch) ...{ case '0':case '1':case '2':case '3': case '4':case '5':case '6':case '7': case '8': case'9': PFX.stack[pPFX++] = ch; break; case '(': TP.stack[++pTP] = '('; break; case ')': while(TP.stack[pTP] != '(') ...{ PFX.stack[pPFX++] = TP.stack[pTP]; pTP--; } pTP--; break; case '+': case '-': while( (pTP > 1 && TP.stack[pTP] != '(') && (TP.stack[pTP]== '+' || TP.stack[pTP]== '-') ) PFX.stack[pPFX++] = TP.stack[pTP--]; pTP ++; TP.stack[pTP]= ch; break; case '*': case '/': while( pTP > 1 && (TP.stack[pTP]== '*' || TP.stack[pTP]== '/') ) PFX.stack[pPFX++] = TP.stack[pTP--]; pTP++; TP.stack[pTP] = ch; break; }/**//*end switch*/ pIFX++; }/**//*end while*/ while(pTP > 1) PFX.stack[pPFX++] = TP.stack[pTP--]; PFX.stack[pPFX] = '=';} int Computing() ... { char ch; char tmp1; char tmp2; int tmp; pPFX = 0; pIFX = 0; pTP = 0; while(PFX.stack[pPFX]!= '=') ...{ ch = PFX.stack[pPFX]; switch(ch) ...{ case '0':case '1':case '2':case '3': case '4':case '5':case '6':case '7': case '8': case'9': pTP++; TP.stack[pTP] = ch; break; case '+': tmp1 = TP.stack[pTP--]; tmp2 = TP.stack[pTP--]; tmp = CtoD(tmp1) + CtoD(tmp2); TP.stack[++pTP] = DtoC(tmp); break; case '-': tmp1 = TP.stack[pTP--]; tmp2 = TP.stack[pTP--]; tmp = CtoD(tmp1) - CtoD(tmp2); TP.stack[++pTP] = DtoC(tmp); break; case '*': tmp1 = TP.stack[pTP--]; tmp2 = TP.stack[pTP--]; tmp = CtoD(tmp1) * CtoD(tmp2); TP.stack[++pTP] = DtoC(tmp); break; case '/': tmp1 = TP.stack[pTP--]; tmp2 = TP.stack[pTP--]; tmp = CtoD(tmp1) / CtoD(tmp2); TP.stack[++pTP] = DtoC(tmp); break; }/**//*end switch*/ pPFX ++; }/**//*end while*/ return(CtoD(TP.stack[1]));} /**/ /****************************** Main Function*******************************/ int main() ... { printf("Input count pression: "); InputPression(); PrintStack(IFX); PFX = toSufix(); PrintStack(PFX); printf("%d",Computing()); getchar();} 未完成...