【发布时间】:2018-09-28 17:02:12
【问题描述】:
代码是使用 ADT 通过包含“stack.h”用户定义的头文件来实现的。
头文件有堆栈操作的代码。 由于不正确的 ASCII 转换,我将输出显示为奇怪的符号。
我应该对代码进行哪些更改才能获得正确的输出。
#include<iostream>
#include<string.h>
#include "stack.h"
using namespace std;
void posteva(char postfix[])
{
stack s;
int i=0;
while(postfix[i]!='\0')
{
char x=postfix[i];
if(isdigit(x))
{
s.push(x);
}
else
{
int op1=s.pop();
int op2=s.pop();
int res;
switch(x)
{
case '+':
res=op1+op2;
break;
case '-':
res=op1-op2;
break;
case '*':
res=op1*op2;
break;
case '/':
res=op1/op2;
break;
case '%':
res=op1%op2;
break;
case '^':
res=op1^op2;
break;
}
s.push(res);
}
i++;
}
cout<<"\n\nRESULT :"<<s.pop();
}
int main()
{
char postfix[20];
cout<<"\n\nEnter the postfix : ";
cin>>postfix;
posteva(postfix);
}
【问题讨论】:
-
例如。我将后缀表达式输入为“23+”,结果是“e”。
-
这是您提供的 only 输入吗?
stack存储什么?int值?如果一个操作产生的值大于9,会发生什么? -
ASCII 的
'2'和'3'值分别为50和51。50 + 51是101,恰好是'e'的 ASCII 值。
标签: c++ stack postfix-notation