[英]I have written this code to convert an infix expression to a postfix expression using Stacks in CPP
#include<bits/stdc++.h>
using namespace std;
int prec(char c){
if(c=='^'){
return 3;
}else if(c=='*' || c=='/'){
return 2;
}else if(c=='+' || c=='-'){
return 1;
}
return -1;
}
string infixToPostfix(string );
int main(){
string s = "(a-b/c)*(a/k-l)";
cout<<infixToPostfix(s);
return 0;
}
string infixToPostfix(string s){
stack<char> st;
string res = "";
for(int i=0;i<s.length();i++){
if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z')){
res+=s[i];
}else if(s[i]=='('){
st.push(s[i]);
}else if(s[i]==')'){
while((!st.empty()) && st.top()!='('){
res+=st.top();
st.pop();
}
if(!st.empty()){
st.pop();
}
}else{
while((!st.empty()) && prec(st.top())>prec(s[i])){
res+=st.top();
st.pop();
}
st.push(s[i]);
}
while(!st.empty()){
res+=st.top();
st.pop();
}
}
return res;
}
As you can see I'm trying to convert infix notation to postfix notation but the output is not coming as expected.如您所见,我正在尝试将中缀表示法转换为后缀表示法,但 output 没有按预期出现。 I couldn't even find any syntax error so there's a high chance that there is some logical error.
我什至找不到任何语法错误,所以很有可能存在一些逻辑错误。
Expected Output:预期 Output:
abc/-ak/l-*
Actual Output:实际 Output:
(a-b/c*(a/k-l
I have blown my brain off trying to find the error and I still haven't.我已经把我的大脑吹了起来,试图找到错误,但我仍然没有。 Please help me solve the issue.
请帮我解决问题。
Define two precedence tables, called outstack
for operator when they are outside the stack and instack
for operator when they are inside the stack.定义两个优先级表,当它们在堆栈外时称为
outstack
for operator,当它们在堆栈内时称为instack
for operator。
If any operator is left to right assosiative increase the precedence from outstack
to instack
.如果任何运算符是从左到右关联的,则将优先级从
outstack
增加到instack
。 If it is right to left decrease the precedence.如果从右到左降低优先级。
Op![]() |
outstack pre![]() |
instack pre![]() |
---|---|---|
+ - ![]() |
1 ![]() |
2 ![]() |
* / ![]() |
3 ![]() |
4 ![]() |
^ ![]() |
6 ![]() |
5 ![]() |
( ![]() |
7 ![]() |
0 ![]() |
) ![]() |
0 ![]() |
x ![]() |
The program below uses this logic to convert an infix expression to postfix expression.下面的程序使用此逻辑将中缀表达式转换为后缀表达式。
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <vector>
// For left to right associative operator, precedence increase from out to in
// For right to left associative operator (like ^), precedence decrease from out to in
// Outside stack precedence
std::map<char, int> precedenceOutStack {
{'(', 7},
{')', 0},
{'*', 3},
{'/', 3},
{'+', 1},
{'-', 1},
{'^', 6},
};
// Inside stack precedence
std::map<char, int> precedenceInStack {
{'(', 0},
{'*', 4},
{'/', 4},
{'+', 2},
{'-', 2},
{'^', 5},
};
int getOutPrecedence(char c) {
if(precedenceOutStack.count(c) > 0) {
return precedenceOutStack[c];
} else {
return 0;
}
}
int getInPrecedence(char c) {
if(precedenceInStack.count(c) > 0) {
return precedenceInStack[c];
} else {
return 0;
}
}
std::string infixToPostfix(std::string infix) {
std::string postfix {};
std::stack<char> stk;
size_t i {};
// loop through the input string
while(infix[i]) {
// if its an operand add it to postfix
if(std::isalpha(infix[i])) {
postfix.push_back(infix[i++]);
} else {
if(!stk.empty()) {
auto outPrec = getOutPrecedence(infix[i]);
auto inPrec = getInPrecedence(stk.top());
// check the out precedence of input char with in precedence of stack top
// if its greater push the operator to stack
if( outPrec > inPrec ) {
stk.push(infix[i++]);
}
// else if it is less, append the operator from top of stack to postfix
else if(outPrec < inPrec ) {
postfix.push_back(stk.top());
stk.pop();
}
// only '(' and ')' has equal out and in precedence, ignore them
else if(outPrec == inPrec) {
stk.pop();
++i;
}
} else {
stk.push(infix[i++]);
}
}
}
// pop out remaining opreator from the stack and append them to postfix
while(!stk.empty()) {
postfix.push_back(stk.top());
stk.pop();
}
return postfix;
}
int main()
{
std::vector<std::string> inputs {
"(a-b/c)*(a/k-l)" // abc/-ak/l-*
};
for(const auto& s : inputs) {
std::cout << "Infix: " << s << " -- ";
std::cout << infixToPostfix(s) << std::endl;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.