[英]Infix to Postfix Converter in C++ gives no output
I made a function infixToPostfix()
which converts an infix expression to a postfix expression.我制作了一个函数
infixToPostfix()
,它将中缀表达式转换为后缀表达式。 But my program generates no output on running.但是我的程序在运行时不会产生任何输出。 Please point out the errors in my program.
请指出我程序中的错误。
Code:代码:
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdlib>
using namespace std;
int isOperator(char x)
{
return (x == '-' || x == '+' || x == '/' || x == '*');
}
int precedenceOfOperators(char x)
{
if (x == '+' || x == '-')
{
return 1;
}
else if (x == '/' || x == '*')
{
return 2;
}
return 0;
}
char *infixToPostfix(char infix[])
{
int i = 0, j = 0;
stack<char> lobby;
char *postfix = (char *)malloc((strlen(infix + 1)) * sizeof(char));
while (infix[i] != '\0')
{
if (!isOperator(infix[i]))
{
postfix[j] = infix[i];
i++;
j++;
}
else
{
if (!lobby.empty())
{
if (precedenceOfOperators(infix[i]) > precedenceOfOperators(lobby.top()))
{
lobby.push(infix[i]);
}
else
{
postfix[j] = lobby.top();
lobby.pop();
j++;
}
}
else if (lobby.empty())
{
lobby.push(infix[i]);
}
}
}
while (!lobby.empty())
{
postfix[j] = lobby.top();
lobby.pop();
j++;
}
return postfix;
}
Implementation:执行:
int main()
{
char infix[] = {'a', '-', 'b', '+', 'c', '*', 'd'};
char *postfix = infixToPostfix(infix);
for (int j = 0; postfix[j] != '\0'; j++)
{
cout << postfix[j];
}
return 0;
}
Logic:逻辑:
Your code is exceeding the time limit because it is stuck in the infinite loop.您的代码超过了时间限制,因为它陷入了无限循环。 You have not updated the
i variable
- which is the index of the infix array in the else part of the loop ie when infix[i]
is an operator.您尚未更新
i variable
- 它是循环的 else 部分中的中缀数组的索引,即当infix[i]
是运算符时。 ie in this part of the code即在这部分代码中
else
{
if (!lobby.empty())
{
if (precedenceOfOperators(infix[i]) > precedenceOfOperators(lobby.top()))
{
lobby.push(infix[i]);
}
else
{
postfix[j] = lobby.top();
lobby.pop();
j++;
}
}
else if (lobby.empty())
{
lobby.push(infix[i]);
}
}
Here is the updated code which is giving perfect output.这是提供完美输出的更新代码。 ( I have made some minor changes as per my convenience, you can keep the code the same and add the
i++
in the else part) (我根据自己的方便做了一些小改动,你可以保持代码不变并在 else 部分添加
i++
)
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdlib>
using namespace std;
int isOperator(char x)
{
return (x == '-' || x == '+' || x == '/' || x == '*');
}
int precedenceOfOperators(char x)
{
if (x == '+' || x == '-')
{
return 1;
}
else if (x == '/' || x == '*')
{
return 2;
}
return 0;
}
string infixToPostfix(char infix[])
{
int i = 0, j = 0;
if(sizeof(infix)==0)
{
return "";
}
int n=sizeof(infix)/sizeof(infix[0]);
stack<char> lobby;
string postfix = "";
while (i < n)
{
if (!isOperator(infix[i]))
{ postfix=postfix+infix[i];
i++;
j++;
}
else
{
if (!lobby.empty())
{
if (precedenceOfOperators(infix[i]) > precedenceOfOperators(lobby.top()))
{
lobby.push(infix[i]);
}
else
{
postfix = postfix+lobby.top();
lobby.pop();
j++;
}
}
else if (lobby.empty())
{
lobby.push(infix[i]);
}
i++;
}
}
while (lobby.empty()==false)
{
postfix = postfix+lobby.top();
lobby.pop();
j++;
}
return postfix;
}
int main()
{
char infix[] = {'a', '-', 'b', '+', 'c', '*', 'd'};
string postfix = infixToPostfix(infix);
for (int j = 0; j < postfix.size() ; j++)
{
cout << postfix[j];
}
return 0;
}
infixToPostfix()
.infixToPostfix()
的 while 循环的 else 部分。 Updated infixToPostfix()
:更新
infixToPostfix()
:
string infixToPostfix(string infix)
{
int i = 0;
stack<char> lobby;
string postfix;
if (infix.size() == 0)
{
return "";
}
while (infix[i] != '\0')
{
if (!isOperator(infix[i]))
{
postfix = postfix + infix[i];
i++;
}
else
{
while (!lobby.empty() && precedenceOperator(infix[i]) <= precedenceOperator(lobby.top()))
{
postfix += lobby.top();
lobby.pop();
}
lobby.push(infix[i]);
i++;
}
}
while (!lobby.empty())
{
postfix = postfix + lobby.top();
lobby.pop();
}
return postfix;
}
i variable
should also be updated in the else part of the while loop.i variable
也应该在 while 循环的 else 部分中更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.