Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given a text file, your program will determine if all the parentheses, braces, a

ID: 3720087 • Letter: G

Question

Given a text file, your program will determine if all the parentheses, braces, and square brackets match, and are nested appropriately. Your program should work for mathematical formulas and most computer programs.

Your program should read in the characters from the file, but ignore all characters except for the following: { } ( ) [ ]

Your program should use the IntStack declaration (IntStack.h), which will be available from Tracs).

The general algorithm is to use a stack to store the opening unmatched brackets. When a closing bracket is encountered, check it against the one on top of the stack-- make sure it matches, and pop it off. When you are finished there should be no unmatched brackets left on the stack.

Input/Output:

Your driver program must prompt the user to enter a filename. It should then try to open the file and then check it to ensure the brackets all match appropriately. If they all match, the program should output a message saying so. If not, the program should output an appropriate error message (there are three possible error messages corresponding to three types of errors shown as follows).

There are three types of errors that can happen (and with any kind of bracket):

missing } : if you reach the end of the file, and there is an opening { that was never matched, like: int main () { x[size]=10;

expected } but found ) : this is a wrong closing bracket, like: {x[i]=10;)...

unmatched } : this occurs if there is a closing bracket but not an opening bracket (not even one of the wrong kind), like: int main () { x[i]=10; } }...

NOTES:

• This program must be done in a Linux or Unix environment, using a command line compiler like g++. Do not use codeblocks, eclipse, or Xcode to compile.

• As soon as your driver encounters an error, your program should stop and print out the appropriate error message. Do NOT try to keep going and find more errors!

• The stack is an int stack but you will probably want to put characters on the stack. You can push a character directly on the int stack (it will automatically be converted to an int). When you pop I recommend popping into a char variable first (this will automatically convert the int back into a character).

• It might be easier to store the expected closing character on the stack when an opening bracket is encountered. This simplifies the matching when a closing bracket is encountered.

• Beware of stack underflow! Do NOT try to pop an empty stack! If you get an error message like this when you run your program: Assertion failed: (!isEmpty()) then you are popping an empty stack: your program should prevent this and output its own appropriate error message instead. • Follow the style guidelines from the Tracs. Logistics: For this assignment you need to submit two files: IntStack.cpp and your driver file. Please zip these into one file, assign6_xxxxxx.zip (where xxxxx is your NetID). Then submit the zip file

Pls use this header file

Please use structure in the class as shown in the header file

Explanation / Answer

IntStack.cpp

#include "IntStack.h"
#include <cstddef>

IntStack::IntStack() {
    head = NULL;

// initially head is null.
}

IntStack::~IntStack() {
    delete head;

// deleteing head.
}

void IntStack::push(int x) {

// If stack is empty then insert into first node.

    if(head == NULL) {
        head = new Node;
        head->data = x;
        head->next = NULL;
    }

// If stack is not empty then insert in begining of list.
    else {
        Node *p = new Node;
        p->data = x;
        p->next = head;
        head = p;
    }
}

int IntStack::pop() {

// Move head to the next node and return the previous head.
    int x = head->data;
    Node *p = head;
    head = head->next;
    p->next = NULL;
    delete p;
    return x;
}

bool IntStack::isFull() {

    // This is vague in case of link list implementation of stack.
    // Not sure about the approach.

    Node *tmp = new Node;
    if(tmp == NULL)
        return true;
    delete tmp;
    return false;
}

bool IntStack::isEmpty() {

// Check if head is empty.

    if(head == NULL)
        return true;
    return false;
}


Main.cpp

#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <cstdio>
#include <string>
#include <fstream>
#include "IntStack.h"


using namespace std;

int main() {
  
    string filename;
    ifstream filePtr;
    IntStack st;


    cout<<"Enter the file name ";

    cin>>filename;
  
    filePtr.open(filename.c_str(),ios_base::in);
// Accepting file name
    if(!filePtr.is_open()) {
        cout<<"Invalid file name ";
        exit(0);
    }
  
   //Reading each character.
    char c = filePtr.get();
    while (filePtr.good()) {
        //cout<<c;
        char c1;
       // If any of opening bracket push in stack
        if(c == '{' || c == '[' || c == '(') {
            //ASSERT(!st.isFull());
            st.push(c);
        }
       // for every opening bracket.
        else if (c == '}') {
           // If stack is empty then report error.
            if(st.isEmpty()) {
                cout<<"unmatched } ";
                exit(0);
            }
           //Pop out the character.
            c1 = st.pop();
           // if closing is not matched report error.
            if(c1 != '{') {
                cout<<"expected } but found "<<c1<<" ";
                exit(0);
            }
        }
        else if (c == ']') {
           // If stack is empty then report error.
            if(st.isEmpty()) {
                cout<<"unmatched ] ";
                exit(0);
            }
           //Pop out the character.
            c1 = st.pop();
           // if closing is not matched report error.
            if(c1 != '[') {
                cout<<"expected ] but found "<<c1<<" ";
                exit(0);
            }
        }
        else if (c == ')') {
           // If stack is empty then report error.
            if(st.isEmpty()) {
                cout<<"unmatched ) ";
                exit(0);
            }
           //Pop out the character.
            c1 = st.pop();
           // if closing is not matched report error.
            if(c1 != '(') {
                cout<<"expected ) but found "<<c1<<" ";
                exit(0);
            }
        }
        c = filePtr.get();

    }
// If stack is not empty then report the error for missing bracket.
    if(!st.isEmpty()) {
        char c1 = st.pop();
        if(c1 == '{')
            c1 = '}';
        else if(c1 == '(')
            c1 = ')';
        else if(c1 == '[')
            c1 = ']';
        cout<<"missing "<<c1<<endl;
    }

    return 0;
}


Input file: hello.cpp( change the brackets to get the desired results)

#include <iostream>

using namespace std;

int foo(){}
int main() {
    int a[10];
    foo();
    cout<<"Hello";
}

Sample output:

mandar@ubuntu:/mnt/hgfs/chegg/parenthesis$ g++ IntStack.cpp main.cpp -o in

mandar@ubuntu:/mnt/hgfs/chegg/parenthesis$ ./in
Enter the file name
hello.cpp
missing }

mandar@ubuntu:/mnt/hgfs/chegg/parenthesis$ ./in
Enter the file name
hello.cpp
unmatched }

mandar@ubuntu:/mnt/hgfs/chegg/parenthesis$ ./in
Enter the file name
hello.cpp
expected } but found [

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote