后缀评估
Postfix evaluation
我正在编写一个计算给定后缀表达式的代码。每个操作数和运算符由空格分隔,最后一个运算符后跟一个空格和一个"x"。
例:
中缀表达式:(2*3+4)*(4*3+2)
后缀表达式:2 3 * 4 + 4 3 * 2 + * x
"x"表示表达式的结束。
输入(后缀表达式)由另一个函数以字符串形式给出,该函数将中缀表达式转换为后缀表达式。
后缀评估的功能是:
int pfeval(string input)
{
int answer, operand1, operand2, i=0;
char const* ch = input.c_str();
node *utility, *top;
utility = new node;
utility -> number = 0;
utility -> next = NULL;
top = new node;
top -> number = 0;
top -> next = utility;
while((ch[i] != ' ')&&(ch[i+1] != 'x'))
{
int operand = 0;
if(ch[i] == ' ') //to skip a blank space
i++;
if((ch[i] >= '0')&&(ch[i] <= '9')) //to gather all digits of a number
{
while(ch[i] != ' ')
{
operand = operand*10 + (ch[i]-48);
i++;
}
top = push(top, operand);
}
else
{
top = pop(top, operand1);
top = pop(top, operand2);
switch(ch[i])
{
case '+': answer = operand2 + operand1;
break;
case '-': answer = operand2 - operand1;
break;
case '*': answer = operand2 * operand1;
break;
case '/': answer = operand2 / operand1;
break;
case '^': answer = pow(operand2, operand1);
break;
}
top = push(top, answer);
}
i++;
}
pop(top, answer);
cout << "nAnswer: " << answer << endl;
return 0;
}
我给出的示例的输出应该是"140",但我得到的是"6"。请帮我找到错误。
推送和弹出方法如下(以防有人想要审查):
class node
{
public:
int number;
node *next;
};
node* push(node *stack, int data)
{
node *utility;
utility = new node;
utility -> number = data;
utility -> next = stack;
return utility;
}
node* pop(node *stack, int &data)
{
node *temp;
if (stack != NULL)
{
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "nERROR: Empty stack.n";
return stack;
}
while((ch[i] != ' ')&&(ch[i+1] != 'x'))
一旦 a) 当前字符是空格,或 b) 下一个字符是"x",您就可以退出此循环。当前角色在这个过程的早期就变成了一个空间;只处理字符串的一小部分。
尝试与以下代码进行比较。
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
#include<math.h>
class A
{
char p[30],ch;
int i,top,s[30],y1,y2,x,y,r;
public:
A()
{
top=-1;
i=0;
}
void input();
char getsymbol();
void push(int);
int pop();
void evaluation();
};
void A :: input()
{
cout<<"enter postfix expressionn";
gets(p);
}
char A :: getsymbol()
{
return p[i++];
}
void A :: push(int ch)
{
if(top==29)
cout<<"stack overflown";
else
s[++top]=ch;
}
int A :: pop()
{
if(top==-1)
{
cout<<"stack underflown";
return 0;
}
else
return s[top--];
}
void A :: evaluation()
{
ch=getsymbol();
while(ch!=' ')
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
cout<<"enter the value for "<<ch;
cin>>x;
push(x);
}
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
{
y2=pop();
y1=pop();
if(ch=='+')
{
y=y1+y2;
push(y);
}
else if(ch=='-')
{
y=y1-y2;
push(y);
}
else if(ch=='^')
{
y=y1^y2;
push(y);
}
else if(ch=='*')
{
y=y1*y2;
push(y);
}
else if(ch=='/')
{
y=y1/y2;
push(y);
}
else
{
cout<<"entered operator has no valuen";
}
}
ch=getsymbol();
}
if(ch==' ')
{
r=pop();
cout<<"the result is "<<r;
}
}
int main()
{
A a;
int m=0;
while(m==0)
{
a.input();
a.evaluation();
cout<<"enter 0 to continue 1 to exitn";
cin>>m;
}
getch();
return 0;
}
相关文章:
- std::condition_variable::wait()如何评估给定的谓词
- 增量运算符与后缀混淆
- c++11评估顺序(未定义的行为)
- 如何在OpenSSL库的名称中添加后缀'd'?
- 如何使用"equal to"以外的评估编写开关语句
- 嵌套 if 中没有返回评估
- 懒惰的参数评估try_emplace?
- C++17 十六进制浮点文字单精度后缀冲突?
- C++ 后缀评估问题
- 堆栈后缀表示法评估等于中缀计算得到的不同
- 使用C 中的STL评估后缀表达
- 为什么在后缀评估中尝试减法时不起作用?
- 后缀评估算法
- 使用堆栈评估后缀表达式时出现分段错误
- 中缀到后缀转换器 + 评估程序正确打印到控制台,但不正确地打印到文本文件
- 如何从C++中的文件读取到队列中进行后缀评估
- 原子 CAS 中的后缀评估
- 后缀评估
- 使用堆栈进行后缀评估
- 以下代码片段中 push(po[i]-'0') 对评估后缀表达式的重要性是什么