将中缀表示法表达式转换为后缀表示法
我正在为我的数据结构课程做作业,其中我必须将中缀表达式转换为后缀表达式。我几乎完成了它,但当我尝试输入类似 a+b+c 的内容时,我不断收到错误
它可以很好地处理 a+b 和 a+b*c 。
我真的不知道出了什么问题。如果有人能给我指明方向或看到我的代码的问题,我将非常感激。
#include <iostream>
#include <stack>
using namespace std;
//checks priority of operators.
int priority(char e){
int pri = 0;
if(e == '*' || e == '/' || e == '%'){
pri = 2;
}else{
if(e == '+' || e == '-'){
pri = 1;
}
}
return pri;
}
void main(){
cout << "This program will convert an infix expression to a postfix expression." << endl;
cout << "Please enter your expression without any spaces." << endl;
stack<char> charStack;
char input[100];
char output[100];
char n1;
char *o;
o = &output[0];
cin >> input;
int n = 0;
while(input[n] != 0){
if(isdigit(input[n]) || isalpha(input[n])){
*o = input[n];
n++;
o++;
}
if(input[n] == '('){
charStack.push(input[n]);
n++;
}
if(input[n] == ')'){
n1 = charStack.top();
charStack.pop();
while(n1 != '('){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
n++;
}
if(input[n] == '+' || input[n] == '-' || input[n] == '*' || input[n] == '/' || input[n] == '%'){
if(charStack.empty() == true){
charStack.push(input[n]);
}else{
n1 = charStack.top();
charStack.pop();
while(priority(n1) >= priority(input[n])){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
charStack.push(n1);
charStack.push(input[n]);
}
n++;
}
}
while(!charStack.empty()){
*o = charStack.top();
o++;
charStack.pop();
}
*o = '\0';
cout << output << endl;
}
I'm doing an assignment for my Data Structures course where I have to convert an infix expression to a postfix expression. I'm almost done with it but I keep getting an error when I try entering something like a+b+c
It can handle a+b and a+b*c just fine.
I'm really not sure what's wrong with it. If someone could point me in a direction or see the problem with my code, I would really appreciate it.
#include <iostream>
#include <stack>
using namespace std;
//checks priority of operators.
int priority(char e){
int pri = 0;
if(e == '*' || e == '/' || e == '%'){
pri = 2;
}else{
if(e == '+' || e == '-'){
pri = 1;
}
}
return pri;
}
void main(){
cout << "This program will convert an infix expression to a postfix expression." << endl;
cout << "Please enter your expression without any spaces." << endl;
stack<char> charStack;
char input[100];
char output[100];
char n1;
char *o;
o = &output[0];
cin >> input;
int n = 0;
while(input[n] != 0){
if(isdigit(input[n]) || isalpha(input[n])){
*o = input[n];
n++;
o++;
}
if(input[n] == '('){
charStack.push(input[n]);
n++;
}
if(input[n] == ')'){
n1 = charStack.top();
charStack.pop();
while(n1 != '('){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
n++;
}
if(input[n] == '+' || input[n] == '-' || input[n] == '*' || input[n] == '/' || input[n] == '%'){
if(charStack.empty() == true){
charStack.push(input[n]);
}else{
n1 = charStack.top();
charStack.pop();
while(priority(n1) >= priority(input[n])){
*o = n1;
o++;
n1 = charStack.top();
charStack.pop();
}
charStack.push(n1);
charStack.push(input[n]);
}
n++;
}
}
while(!charStack.empty()){
*o = charStack.top();
o++;
charStack.pop();
}
*o = '\0';
cout << output << endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在弹出运算符代码中的元素之前,不检查堆栈是否为空。这是问题的一部分。
顺便说一句,它应该是
int main()
而不是void
,并且您不需要将事物与true
进行比较:charStack.empty() == true
与charStack.empty()
相同。You don't check whether the stack is empty before popping an element in the code for operators. That's part of the problem.
By the way, it should be
int main()
instead ofvoid
, and you don't need to compare things withtrue
:charStack.empty() == true
is the same ascharStack.empty()
.请参阅我的内联评论。
See my comments inline.