逆波兰求结果出现错误
void CaluateStack(STACK S,ElemType *answer) //STACK Sstack储存有逆波兰后续表达式{
STACK p1; //p1储存逆波兰的数值
ElemType temp1,temp2;
int i=0;
InitStack(&p1);
while(S.stack != '\0')
{
*answer=S.stack;
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);
}
能算两位数加减乘除,三位以上算不了
http://www.so.com/link?m=a3Xbu%2B%2FxB5KbziQAUugg0NuWGHdPIFvmb8e3%2By%2FY9aHYOTFHBckY%2FwbHzSWVSFydug5gBgZarQRQbwlj7nJzylEagF%2FHaPMLj9oHVJ3ooXWBV%2BorohuQMw8hhtGJCRyM7ucfNB1KkF3X5tozHAnayQ5yjDJUY6MeFqqgi7YnPRl5%2BfWJgfHXdhA%3D%3D
恩你的代码真简洁~ 新手·ing 发表于 2017-7-10 07:44
http://www.so.com/link?m=a3Xbu%2B%2FxB5KbziQAUugg0NuWGHdPIFvmb8e3%2By%2FY9aHYOTFHBckY%2FwbHzSWVSFydu ...
还有的压栈,出栈,初始没复制过来
页:
[1]