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