Question: I need help with this code, the first 2 steps work in main but the 3 rd step ( Evaluate ) leads to the program

I need help with this code, the first 2 steps work in main but the 3rd step(Evaluate) leads to the program resulting in this "bash: line 1: 8 Segmentation fault (core dumped) bash -c 'g++-o jdoodle *.cpp && ./jdoodle mainnull' ." This is the code:
#include
#include
#include
#include
#include
using namespace std;
// Function to check if parentheses in the expression are balanced
bool isValidParentheses(const string& expr){
stack s;
for (char ch : expr){
if (ch =='('){
s.push(ch); // Push opening parenthesis
} else if (ch ==')'){
if (s.empty()|| s.top()!='('){
return false; // Mismatched parentheses
}
s.pop(); // Match closing parenthesis
}
}
return s.empty(); // Return true if all parentheses matched
}
// Function to determine the precedence of operators
int precedence(char op){
if (op =='+'|| op =='-') return 1;
if (op =='*'|| op =='/') return 2;
return 0; // Parentheses or invalid operator
}
// Function to convert an infix expression to postfix
string infixToPostfix(const string& expr){
stack s;
string result;
for (char ch : expr){
if (isdigit(ch)){
result += ch; // Append operand to result
} else if (ch =='('){
s.push(ch); // Push '(' onto stack
} else if (ch ==')'){
while (!s.empty() && s.top()!='('){
result += s.top();
s.pop(); // Append operators until '(' is found
}
s.pop(); // Remove '('
} else {
while (!s.empty() && precedence(s.top())>= precedence(ch)){
result += s.top(); // Pop operators with higher precedence
s.pop();
}
s.push(ch); // Push current operator
}
}
// Append remaining operators in stack
while (!s.empty()){
result += s.top();
s.pop();
}
return result;
}
// Function to evaluate a postfix expression
int evaluatePostfix(const string& expr){
stack s;
for (char ch : expr){
if (isdigit(ch)){
s.push(ch -'0'); // Convert character to integer and push
} else {
int val2= s.top(); s.pop();
int val1= s.top(); s.pop();
switch (ch){
case '+': s.push(val1+ val2); break;
case '-': s.push(val1- val2); break;
case '*': s.push(val1* val2); break;
case '/': s.push(val1/ val2); break;
}
}
}
return s.top(); // The result is the final value on the stack
}
int main(){
std::string expression ="(5+3)*8+2";
// Step 1: Validate parentheses
if (!isValidParentheses(expression)){
std::cout << "Invalid parentheses!" << std::endl;
return 0; // If parentheses are invalid, exit the program
}
// Step 2: Convert infix to postfix
std::string postfix = infixToPostfix(expression);
std::cout << "Postfix Expression: "<< postfix << std::endl; // Print the postfix expression
// Step 3: Evaluate postfix expression
int result = evaluatePostfix(postfix); // Evaluate the postfix expression
std::cout << "Result: "<< result << std::endl; // Output the evaluation result
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!