|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
void CaluateStack(STACK S,ElemType *answer) //STACK Sstack储存有逆波兰后续表达式
{
STACK p1; //p1储存逆波兰的数值
ElemType temp1,temp2;
int i=0;
InitStack(&p1);
while(S.stack[i] != '\0')
{
*answer=S.stack[i];
switch(*answer)
{
case '+':
pop(&p1,&temp1);
pop(&p1,&temp2);
push(&p1,(temp1-'0')+(temp2-'0'));
break;
case '-':
pop(&p1,&temp1);
pop(&p1,&temp2);
push(&p1,(temp2-'0')-(temp1-'0'));
break;
case '*':
pop(&p1,&temp1);
pop(&p1,&temp2);
push(&p1,(temp2-'0')*(temp1-'0'));
break;
case '/':
pop(&p1,&temp1);
pop(&p1,&temp2);
if(temp1 == '0')
printf("errror");
else
push(&p1,(temp2-'0')/(temp1-'0'));
break;
default:
push(&p1,*answer);
}
i++;
}
printf("%d",p1.stack[0]);
}
能算两位数加减乘除,三位以上算不了
|
|