用逻辑运算符中缀到postfix - 代码错误

时间:2014-11-20 19:02:14

标签: c++ stack

我正在尝试编写一个函数字符串InfixToPostfix(string infixExpr),它以中缀表示法作为输入表达式字符串,并返回包含该表达式的后缀表示法的字符串。中缀符号可以有 插入语。该程序需要处理这些运算符*,/,%,+, - ,<,< =,> =,>,== ,! =,&安培;&安培;,|| (仅限二元运算符)

这是我正在尝试的代码..它与父母的完美运行但是在没有括号的情况下输出略有错误。例如,如果输入为A + B / C,则输出应为ABC / +,而是给出AB + C /。请帮我看看错误。感谢

#include<iostream>
#include<stack>
#include<string>

using namespace std;

// Function to convert Infix expression to postfix 
string InfixToPostfix(string expression); 
// Function to verify whether an operator has higher precedence over other
int HasHigherPrecedence(string operator1, string operator2); 
// Function to verify whether a character is operator symbol or not. 
bool IsOperator(string C);
// Function to verify whether a character is alphanumeric character (letter) or not. 
bool IsOperand(string C);
int GetOperatorWeight(string opt);
bool IsDoubleOperator(char c1, char c2);



int main() 
{
    string expression; 
    cout<<"Enter Infix Expression \n";
    getline(cin,expression);

    string postfix = InfixToPostfix(expression);
    cout<<"Output = "<<postfix<<"\n";
    return 0;
}

// Function to evaluate Postfix expression and return output
string InfixToPostfix(string expression)
{
    // Declaring a Stack from Standard template library in C++. 
    stack<string> S;
    string postfix = ""; // Initialize postfix as empty string.
    for(int i = 0;i< expression.length();i++) {
        string ex="";
        ex+=expression[i];

        // Scanning each character from left. 
        // If character is a delimitter, move on. 
        if(ex == " " || ex == ",") continue; 

        // If character is operator, pop two elements from stack, perform operation and push the result back. 
        else if(IsOperator(ex)) 
        {
            while(!S.empty() && S.top() != "(" && HasHigherPrecedence(S.top(),ex))
            {
                postfix+= S.top();
                S.pop();
            }
            S.push(ex);
        }
        // Else if character is an operand
        else if(IsOperand(ex))
        {
            postfix +=ex;
        }

        else if (ex == "(") 
        {
            S.push(ex);
        }

        else if(ex == ")") 
        {
            while(!S.empty() && S.top() !=  "(") {
                postfix += S.top();
                S.pop();
            }
            S.pop();
        }
        else if (IsDoubleOperator(expression[i], expression[i+1]))
          {
                string db="";
                string f="";
                db=+expression[i];
                f=+expression[i+1];
                db=db+f;      

              while(!S.empty() && S.top() != "(" && HasHigherPrecedence(S.top(),db))
               {

                    postfix+= S.top();
                    S.pop();

                }

                S.push(db);
                i++;
          }
    }

    while(!S.empty()) {
        postfix += S.top();
        S.pop();
    }

    return postfix;
}

// Function to verify whether a character is english letter or numeric digit. 
// We are assuming in this solution that operand will be a single character
bool IsOperand(string C) 
{
    if(C >= "A" && C <= "Z") return true;
    return false;
}

// Function to verify whether a character is operator symbol or not. 
bool IsOperator(string C)
{
    if(C == "+" || C == "-" || C == "*" || C == "/" || C== "%")
        return true;

    return false;
}


// Function to get weight of an operator. An operator with higher weight will have higher precedence. 
int GetOperatorWeight(string op)
{
    int weight = -1; 
    if(op=="&&"|| op=="||")
        weight = 1;

    if(op=="=="|| op=="!=")
        weight = 2;

    if(op=="<"|| op==">"|| op=="<="|| op==">=")
        weight = 3;
    if(op=="+"|| op=="-")
        weight = 4;


    if(op=="/"|| op=="%"|| op=="/")
        weight = 5;



    return weight;
}

// Function to perform an operation and return output. 
int HasHigherPrecedence(string op1, string op2)
{
    int op1Weight = GetOperatorWeight(op1);
    int op2Weight = GetOperatorWeight(op2);

    // If operators have equal precedence, return true if they are left associative. 
    // return false, if right associative. 
    // if operator is left-associative, left one should be given priority. 

    if (op1Weight > op2Weight)
        return op1Weight;
    else
        return op2Weight;

}
bool IsDoubleOperator( char c1, char c2)
{
    string db="";
    string f="";
    db=+c1;
    f=+c2;
    db=db+f;

    if (db=="&&" ||db=="||" || db==">=" || db=="<=" || db=="!=" ||db=="==")
    {

    //cout<<db;
        return true;
    }

    return false;
}

1 个答案:

答案 0 :(得分:0)

当op1具有比op2更高的优先级(A.K.A.权重)时,HasHigherPrecedence(op1,op2)预期返回非零,否则返回零。然而,它返回两个操作的最大值&#39;重量,通常是非零的。您只需更改函数即可返回:

return op1Weight > op2Weight;

当为真时为1,为假时为零。这应该可以解决您的操作员分流问题。