练习3.19 计算后缀表达式的值
int main() { int MaxSize = 100,i,j; char tmp[10]; PtrToStack s; s = CreateStack(MaxSize); while( scanf("%s",tmp) != EOF ) { int len,num=0,val1,val2; len = strlen(tmp); if( tmp[0] == '\n') break; if( len == 1 && tmp[0] >=48 && tmp[0] <= 57 ) Push( tmp[0] - '0', s ); else if( len > 1 ) { for( i = 0,j = len - 1; i < len; i++,j--) { num += tmp[i] * pow(10,j); } Push( num, s); } else { val1 = Top(s); Pop(s); val2 = Top(s); Pop(s); switch ( tmp[0] ) { case '+': Push( val1 + val2, s ); break; case '-': Push( val1 - val2, s ); break; case '*': Push( val1 * val2, s ); break; case '/': Push( val1 / val2, s ); break; } }//else }//while printf("%d", Top(s) ); }
while(scanf )有问题,其他地方都没问题
待优化