简体   繁体   中英

How to check if an expression is infix or postfix using stack in c++

I am using my custom stack file. This is a c++ code that I am trying to write but I am not being able to write it. If you could kindly help me out and write a code as to how should I check if my given expression is infix or postfix. I tried to write a code but I could not do it and when I tried to take some ideas from the net it had codes for converting infix to postfix but I need a code that will help me determine if my expression is infix or postfix. Thank you.

Underneath the code is given but i am not being able to fix my mistake, this code runs but there is a problem even if expression is infix it still calls wrong expression, so I do not know where I am making a mistake.


#include <iostream>
#include <string>
#include "stacktype.cpp"
using namespace std;

string infixToPostFix(string infix);
int higherPrecedenceValidate(char op1, char op2);
int getPrecedence(char op);
int evaluatePostFix(string postfix);

int main()
{
    string infix,postfix;
    int result;
    cout << "Infix: ";
    cin >> infix;
    postfix = infixToPostFix(infix);
    cout << "\nPostfix: " << postfix << endl << endl;
    if(postfix != "Wrong Expression"){
        result = evaluatePostFix(postfix);
        cout << "Result: " << result << endl << endl; 
    }


}


string infixToPostFix(string infix){
    StackType<char> operators;
    bool isMathOperatorRepeated = false;
    bool isOperaendRepeated = false;
    string postfix;
    for(int i = 0; i < infix.size(); i++){
        // Checking Operator
        if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/'){
            if(isMathOperatorRepeated){
                postfix = "Wrong Expression";

                /* 
                After this for loop there is while loop
                which is checking rest of the char and add it with postfix string .
                So this pushed char should be pop out 
                beacuse infix expression is wrong.
                */

                while (!operators.IsEmpty())
                {
                    operators.Pop();
                }
                break;
            }
            while (!operators.IsEmpty() && higherPrecedenceValidate(operators.Top(),infix[i]))
            {
                postfix = postfix + operators.Top();
                operators.Pop();
            }
            operators.Push(infix[i]);
            isMathOperatorRepeated = true;
            isOperaendRepeated = false;

        }
        // Checking Operand
        else if(infix[i] >= '0' && infix[i] <= '9')
        {
            if(isOperaendRepeated){
                postfix = "Wrong Expression";

                /* 
                After this for loop there is while loop
                which is checking rest of the char and add it with postfix string .
                So this pushed char should be pop out 
                beacuse infix expression is wrong.
                */

                while (!operators.IsEmpty())
                {
                    operators.Pop();
                }
                break;
            }
            postfix = postfix + infix[i];
            isMathOperatorRepeated = false;
            isOperaendRepeated = true;

        }
        //Checking open bracket
        else if(infix[i] == '(' ){
            operators.Push(infix[i]);
            isMathOperatorRepeated = false;
            isOperaendRepeated = false;
        }

        //Checking closing bracket
        else if(infix[i] == ')' ){

            while (!operators.IsEmpty() && operators.Top() != '(')
            {
                postfix = postfix + operators.Top();
                operators.Pop();
            }

            /*
            checking stack beacuse we know 
            that if the infix char is ')'  
            and the stack is empty then the infix expression is wrong

            */
            if(operators.IsEmpty()){
                postfix = "Wrong Expression";
                break;

            }
            else{
                operators.Pop();
            }
            // poping the opening bracket
            isMathOperatorRepeated = false;
            isOperaendRepeated = false;
        }

        // checking that infix expression has invalid char
        else{
            postfix = "Wrong Expression";

            /* 
                After this for loop there is while loop
                which is checking rest of the char in stack.
                So this pushed char should be pop out 
                beacuse infix expression is wrong.
            */
            while (!operators.IsEmpty())
            {
                operators.Pop();
            }
            break;
        }


    }
    // poping rest of element from the stack..
    while (!operators.IsEmpty())
    {
        if(operators.Top() == '('){
            postfix = "Wrong Expression";
            break;
        }
        else{
            postfix = postfix + operators.Top();
            operators.Pop();
        }
    }
    return postfix;
}

int evaluatePostFix(string postfix){
    StackType<int> finalNumbers;
    for(int i = 0; i < postfix.size(); i++){
        // Checking Operator
        if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/'){
            int resultOfTwoNumber;
            int number2 = finalNumbers.Top();
            finalNumbers.Pop();
            int number1 = finalNumbers.Top();
            finalNumbers.Pop();
            switch (postfix[i])
            {
                case '+':
                    resultOfTwoNumber = number1 + number2;
                    break;
                case '-':
                    resultOfTwoNumber = number1 - number2;
                    break;
                case '*':
                    resultOfTwoNumber = number1 * number2;
                    break;
                case '/':
                    resultOfTwoNumber = number1 / number2;
                    break;
            }
            finalNumbers.Push(resultOfTwoNumber);

        }
        // Checking Operand
        else if(postfix[i] >= '0' && postfix[i] <= '9')
        {
            finalNumbers.Push(postfix[i] - '0');

        }
    }
    return finalNumbers.Top();
}


int higherPrecedenceValidate(char operator1, char operator2)
{
    int op1 = getPrecedence(operator1);
    int op2 = getPrecedence(operator2);
    if(op1 == op2)
        return true;
    return op1 > op2 ?  true: false;
}

int getPrecedence(char op)
{
    int weight = 0;
    switch(op)
    {
    case '+':
    case '-':
        weight = 1;
        break;
    case '*':
    case '/':
        weight = 2;
        break;
    }
    return weight;
}

Surely an expression is postfix if it has two distinct number with no intervening operator. In other words, something like:

42 99 +

Otherwise I'm pretty certain you could consider it infix.

The only case where you couldn't reliably detect it is the one case where it doesn't matter, a single number with no operations.

One distinguishing factor between postfix and infix expression is that, postfix expression will never have brackets in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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