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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.
Transcribed Image Text:```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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education