Implement a stack and solutions to the following problems: balancing parenthesis, evaluating postfix expressions and transforming infix expressions into postfix expressions. We are providing some sample code and input files: public/ balancing.cpp - main method to check whether an expression is balanced infix2postfix.cpp - main method to transform an infix expression into postfix input_balanced.txt - test cases for balancing.cpp input_infix2postfix.txt - test cases for infixtopostfix.cpp input_postfixEval.txt - test cases for postfixEval.cpp postfixEval.cpp - main method to evaluate postfix expressions stack.cpp - stack implementation stack.hpp - stack header file • To compile, run $ g++ stack.cpp balancing.cpp $ g++ stack.cpp postfixEval.cpp $ g++ stack.cpp infixtopostfix.cpp • To run each program, run $ ./a.out • The test cases follow this format: expected_solution input. Given input, your job is to implement code that gets the expected_solution. Obviously, you need to calculate expected_solution, not just print it. • balancing.cpp must balance round parentheses, and square and curly brackets (() [] {}) • While we provide a few test cases, you are expected to add more to make sure your code works. Note: in postfixEval, we just consider each single digital is an input number. For example, “65-” means the two operands are “6” and “5”, NOT “65”. what should the code be for the balancing.cpp
Implement a stack and solutions to the following problems: balancing parenthesis, evaluating postfix expressions and transforming infix expressions into postfix expressions.
We are providing some sample code and input files:
public/
balancing.cpp
- main method to check whether an expression is balanced
infix2postfix.cpp
- main method to transform an infix expression into postfix
input_balanced.txt
- test cases for balancing.cpp
input_infix2postfix.txt
- test cases for infixtopostfix.cpp
input_postfixEval.txt
- test cases for postfixEval.cpp
postfixEval.cpp
- main method to evaluate postfix expressions
stack.cpp
- stack implementation
stack.hpp
- stack header file
• To compile, run
$ g++ stack.cpp balancing.cpp
$ g++ stack.cpp postfixEval.cpp
$ g++ stack.cpp infixtopostfix.cpp
• To run each program, run
$ ./a.out
• The test cases follow this format: expected_solution input. Given input, your job is to implement code that gets the expected_solution. Obviously, you need to calculate expected_solution, not just print it.
• balancing.cpp must balance round parentheses, and square and curly brackets (() [] {})
• While we provide a few test cases, you are expected to add more to make sure your code works.
Note: in postfixEval, we just consider each single digital is an input number. For example, “65-” means the two operands are “6” and “5”, NOT “65”.
what should the code be for the balancing.cpp
![```cpp
#include "stack.hpp"
using namespace std;
int main(){
freopen("input_balanced.txt", "r", stdin);
string s, r;
int line_counter;
while(cin >> r){
cin >> s;
Stack<char> stack;
bool isBalanced = true;
bool solution;
if(r[0] == 'Y' || r[0] == 'y'){
solution = true;
} else {
solution = false;
}
// The input file is in the format "expected_solution expression"
// So variable solution tells you whether 'expression' is balanced or not
for(int i=0; i<s.length(); ++i){
// WRITE CODE HERE so that isBalanced indicates whether 's' is balanced
}
// Checking if you stored in isBalanced the correct value
if(isBalanced == solution){
cout << "line " << line_counter << ": OK [" << solution << " " << isBalanced << "]" << endl;
} else {
cout << "line " << line_counter << ": ERROR [" << solution << " " << isBalanced << "]" << endl;
}
line_counter++;
}
}
```
### Explanation:
This C++ program reads expressions from an input file named `input_balanced.txt` and checks if they are balanced. The format for the input file is `"expected_solution expression"`, where `expected_solution` is 'Y' or 'y' (indicating the expression should be balanced) and any other character indicates it should be unbalanced.
- The string `s` holds the expression to be checked.
- The boolean `isBalanced` is used to track if the expression `s` is balanced.
- The variable `solution` reflects the expected balance state of the expression as indicated in the input file.
- The logic for determining balance is yet to be implemented within the `for` loop, indicated by the comment within the loop.
- The program verifies whether the computed value of `isBalanced` matches the `solution`. It prints `OK` if they match or `ERROR` if they don't, along with the line number for reference.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F02a699f1-d60c-495e-b821-a86bb0a104cb%2F5057bef8-e697-4b0c-9a38-a252c0e0a5db%2Fxuyblz_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images









