In: Computer Science
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on.
These are a few of the files that will be called, each named input1.cpp , input2.cpp and so on until input6.cpp -
input1.cpp
#include <iostream>
using namespace std;
void main (void)
{
cout << "Hello World!" << endl;
}
input2.cpp
#include <iostream>
using namespace std;
void main (void)
cout << "Hello World!" << endl;
}
----------------Stack.h-----------------
#ifndef MY_STACK
#define MY_STACK
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
using namespace std;
class Stack
{
// declare stack array arr
char *arr;
// to store the index of the top most element
int top;
// to store the current capacity of arr
int size;
public:
// constructor
Stack();
// constructor
Stack(int);
// destructor
~Stack();
// function to check if the stack is empty
bool isEmpty();
// function to check if the stack is full
bool isFull();
// push element intot the stack
void push(char);
// return the top most element in the stack
char peek();
// pop element from stack
void pop();
// resize the array to double the original capacity
void resize();
// display contents of stack
void view();
};
#endif
------------------Stack.cpp--------------------
#include "Stack.h"
// constructor
Stack::Stack()
{
// declare an array of size 4
arr = new char[4];
// set initial capacity to 4
size = 4;
top = -1;
}
// constructor
Stack::Stack(int new_size)
{
// declare an array of size new_size
arr = new char[new_size];
// set initial capacity to new_size
this->size = new_size;
top = -1;
}
// destructor
Stack::~Stack()
{
delete arr;
size = 0;
top = -1;
}
// function to check if the stack is empty
bool Stack::isEmpty()
{
if(top == -1)
return true;
return false;
}
// function to check if the stack is full
bool Stack::isFull()
{
if(top == size - 1)
return true;
return false;
}
void Stack::push(char n)
{
// if stack is full
if(isFull())
// resize the array to double the original capacity
resize();
arr[++top] = n;
}
// return the top most element in the stack
char Stack::peek()
{
if(isEmpty())
return -1;
return arr[top];
}
// pop element from stack
void Stack::pop()
{
// if stack is empty
if(isEmpty())
cout<<"The stack is alreasy empty";
// remove the top most element of the stack
top--;
}
// resize the array to double the original capacity
void Stack::resize()
{
// create a new int array double the size of arr
char *temp = new char[2 * size];
int i;
for( i = 0 ; i < size ; i++ )
// cop contents of arr to temp
temp[i] = arr[i];
// set capacity to double the original capacity
size *= 2;
// set temp as the new stack array
arr = temp;
}
// display contents of stack
void Stack::view()
{
int i;
for( i = 0 ; i <= top ; i++ )
cout<<arr[i]<<" ";
cout<<endl;
}
----------------------main.cpp--------------------
#include "Stack.cpp"
bool isInPair( char ch1 , char ch2 )
{
return ( ch1 == '(' && ch2 == ')' ) || ( ch1 == '{' && ch2 == '}' ) || ( ch1 == '[' && ch2 == ']' );
}
bool isBalanced(string str)
{
// create an object of type stack
Stack ob;
int i;
for( i = 0 ; i < str.length() ; i++ )
{
// if the current bracket is opening bracket
if(str[i] == '(' || str[i] == '{' || str[i] == '[')
// push it into stack
ob.push(str[i]);
// if the current bracket is closing bracket
else if( str[i] == ')' || str[i] == '}' || str[i] == ']' )
{
// check if stack is empty or not and the brackets on the top of stack and current character don't form a pair
if(ob.isEmpty() || !isInPair( ob.peek() , str[i]) )
return false;
// check if stack is not empty or not and the brackets on the top of stack and current character form a pair
else
// remove top most element from stack
ob.pop();
}
}
// if stack is empty
if( ob.isEmpty() )
return true;
else
return false;
}
int main()
{
cout<<"Enter file name : ";
string fname, str;
cin>>fname;
// open file in read mode
ifstream in(fname.c_str());
// read complete line
getline(in, str);
if( isBalanced( str ) )
cout<<"It is balanced.";
else
cout<<"it is not balanced.";
return 0;
}
--------------------input.txt--------------------
{}(([][][[()(),|||'''"";;_-+= ]]))
Sample Output