繁体   English   中英

使用逻辑运算符将后缀固定为-代码错误

[英]infix to postfix with logical operators - code error

我试图编写一个函数字符串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;
}

当op1的优先级(AKA权重)比op2高时,HasHigherPrecedence(op1,op2)预计将返回非零值,否则返回零。 但是,它返回两个操作权重的最大值,通常不为零。 您只需要更改函数即可使其返回:

return op1Weight > op2Weight;

如果为true,则为1;如果为false,则为零。 这样可以解决操作员的分流问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM