0

This is all about stacks.We should use 2 stacks for this. I would like to convert infix expression to postfix expression and evaluate this with 2 function like, string Infix2Postfix(string& s) and int EvaluatePostfixExpression(string& s).The operators +, -, *, /, (, ), and follow the usual precedence rules: * and / has the highest precedence, + and - come next, and ( has the lowest precedence. My code isnt working ı really really need help!

    #include <iostream>
    #include <stack>
    #include <string>
    
    using namespace std;
    
    bool isOperator(char ch) {
        return ch == '+' || ch == '*';
    }
    int precedence(char op) {
        return op == '+' ? 1 : 2;
    }
    bool shouldReduce(stack<char>& stack, char op) {
        return !stack.empty() && precedence(stack.top()) >= precedence(op);
    } 
    string Infix2Postfix(string& s) {
        string postfix;
        stack<char>stack;
        for (int i = 0; i < s.size(); i++) {
            if (isdigit(s[i])) {
                postfix += s[i];
            }
            else if (isOperator(s[i])) {
                while (shouldReduce(stack, s[i]) && stack.top() != '(')
                    postfix += stack.top();
                stack.pop();
                stack.push(s[i]);
            }
            else if (s[i] == '(') {
                stack.push(s[i]);
            }
            else { // infix[i] == ')'
                while (stack.top() != '(') {
                 postfix.push_back(stack.top());
                 stack.pop();                      
                }
            }
            while (!stack.empty()) {
                postfix.push_back(stack.top());
                stack.pop();         
            }
        }
        string result;
        result = postfix;
        return result;
    }

// main function
#include <stdio.h>
#include <string>

using namespace std;

extern string Infix2Postfix(string& s);
extern int EvaluatePostfixExpression(string& s);
int Test1() {
    // infixExpr: 3500 - ((43*12) + (47/2));    
    // Let the C++ compiler do the evaluation :-)
    int expressionValue = 3500 - ((43 * 12) + (47 / 2));

    // postfix: 3500 43 12 * 47 2 / + -
    string postfixExpr = "3500 43 12 * 47 2 / + -";
    int result = EvaluatePostfixExpression(postfixExpr);
    if (result != expressionValue) return 0;

    return result;
} //end-Test1

/****************************************************
 * Test2
 ****************************************************/
int Test2() {
    int expressionValue = 20 + 2 * 3 + (2 * 8 + 5) * 4;

    string infixExpr = "20 + 2 * 3     + (2*8 + 5)* 4";
    string postfixExpr = Infix2Postfix(infixExpr);
    printf("Infix Expr: <%s>\nPostfixExpr: <%s>\n", infixExpr.c_str(), postfixExpr.c_str());

    int result = EvaluatePostfixExpression(postfixExpr);
    if (result != expressionValue) return 0;

    return result;
} //end-Test2

/****************************************************
 * Test3
 ****************************************************/
int Test3() {
    // Let the C++ compiler do the evaluation :-)
    int expressionValue = 20 * 2 + 3 - (2 * 8 + 5) * 4;

    string infixExpr = "20* 2 + 3 - (2*8 + 5)* 4";
    string postfixExpr = Infix2Postfix(infixExpr);
    printf("Infix Expr: <%s>\nPostfixExpr: <%s>\n", infixExpr.c_str(), postfixExpr.c_str());

    int result = EvaluatePostfixExpression(postfixExpr);
    if (result != expressionValue) return 0;

    return result;
} //end-Test3

/****************************************************
 * Test4
 ****************************************************/
int Test4() {
    int expressionValue = 220 - 45 - 10;

    string infixExpr = "220 - 45 - 10";
    string postfixExpr = Infix2Postfix(infixExpr);
    printf("Infix Expr: <%s>\nPostfixExpr: <%s>\n", infixExpr.c_str(), postfixExpr.c_str());

    int result = EvaluatePostfixExpression(postfixExpr);
    if (result != expressionValue) return 0;

    return result;
} //end-Test4

/****************************************************
 * Test5
 ****************************************************/
int Test5() {
    int expressionValue = (((13 + 35) * 22) / 45) - (45 + 34 * (190 - 34)) / 100;

    string infixExpr = "(((13+35)*22)/45) - (45+34*(190-34))/100";
    string postfixExpr = Infix2Postfix(infixExpr);
    printf("Infix Expr: <%s>\nPostfixExpr: <%s>\n", infixExpr.c_str(), postfixExpr.c_str());

    int result = EvaluatePostfixExpression(postfixExpr);
    if (result != expressionValue) return 0;

    return result;
} //end-Test5
Jane P.
  • 21
  • 2
  • [My code isnt working ı really really need help!](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Also [what is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – PaulMcKenzie Dec 03 '20 at 23:40
  • Asking the infix question and providing reasonable code! Yes! We almost never see this. Usually it's just a homework dump of the problem description and zero code. You're miles ahead of your competition already. The next step is to run your code in your development environment's debugger and step through the program looking for where it does something unexpected (for example, storing the wrong value or taking the wrong path) that'll usually be a bug. Figure out why it happened and you've got your solution. – user4581301 Dec 03 '20 at 23:42
  • When you write a program, you need to know what every line, function, loop, etc. was supposed to accomplish. It is *your* code. If the program doesn't do what it's supposed to do, the next thing is to use the debugger to see where the program deviates from the plan you had. When you see where the program goes off the rails, you either amend the program so that it follows your plan, or determine that the plan was broken and to make fixes to the plan -- then implement the new plan in the code. Rinse and repeat. – PaulMcKenzie Dec 03 '20 at 23:44
  • Recommendation: finish off the question with a `main` function that starts the ball rolling by calling `Infix2Postfix` with a string you know will cause failure. That way you know everyone will be running exactly the same problem and [probably](https://en.cppreference.com/w/cpp/language/ub) finding the same bugs. – user4581301 Dec 03 '20 at 23:45
  • main function has many tests for this functions for example "20 + 2 * 3 + (2*8 + 5)* 4" for the infix expression this return just 2 despite the for loop. couldnt find whats wrong. Any idea? – Jane P. Dec 04 '20 at 00:02
  • Sorry for being unclear. What I meant was you should add the `main` function to the question. In this case a slightly modified `main` that only calls `Infix2Postfix("20 + 2 * 3 + (2*8 + 5)* 4")` and prints the result. This way everyone trying to answer the question has one complete program that they can drop unchanged into their development systems and see exactly what you do. Within reason, of course. UB will do what it wants. – user4581301 Dec 04 '20 at 00:14
  • This is a good effort. I see that you didn't implement '-' and '/' operators, and that the stack.pop() should be inside the shouldReduce loop instead of after it... but you should really use a debugger. Stepping through your code will show you these problems much faster than SO can. – Matt Timmermans Dec 04 '20 at 00:56

0 Answers0