标签:
//将中缀表达式转换为后缀表达式 int main() { int MaxSize = 10; int str[8]={3,3,2,1,0,1,0,2}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); while(1) { tmp = getchar(); if(tmp == ‘\n‘) break; if(tmp == ‘ ‘) continue; else if(tmp == ‘+‘ || tmp == ‘-‘ || tmp == ‘*‘ || tmp == ‘/‘ || tmp == ‘(‘ || tmp == ‘)‘) { if( IsEmpty( s ) ) Push( tmp, s ); else { if( tmp == ‘)‘ ) { while( Top(s) != ‘(‘ ) { printf("%c ",Top(s)); Pop( s ); } Pop( s ); } else { while( !IsEmpty(s) && Top(s) != ‘(‘ && str[ tmp - ‘(‘ ] <= str[ Top(s) - ‘(‘ ] ) { printf("%c ",Top( s )); Pop( s ); } Push( tmp, s ); } } } else printf("%c ",tmp); } while( !IsEmpty( s ) ) { printf("%c ",Top( s ) ); Pop( s ); } return 0; }
该程序只可以a+b*c等任何式子都可以workout
123+123*321 由于是用getchar()来做的,所以没能做这种式子
标签:
原文地址:http://www.cnblogs.com/gabygoole/p/4643513.html