Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help making changes to my code below when I completeed both part 1 and 2 from the screenshot I attached where it includes an

Need help making changes to my code below when I completeed both part 1 and 2 from the screenshot I attached where it includes an input file (program.c) need help making changes to wwhere it includes that file please in C++.

image text in transcribed

#include

using namespace std;

#define MAX 1000

class Stack{

int top;

public:

int a[MAX]; // Max size of Stack

Stack(){

top = -1;

}

//top is initialized to -1 to check the emptiness of the stack

bool push(int x);

int pop();

bool isEmpty();

int peek();

};

int Stack::peek(){

return a[top];

}

bool Stack::push(int x){

if (top >= MAX){

cout

return false;

}

else{

a[++top] = x;

return true;

}

}

int Stack::pop(){

if (top

return 0;

else{

int x = a[top--];

return x;

}

}

//Checks the stack if its empty or not

bool Stack::isEmpty(){

return (top

}

// checks for pairs

bool Pair(char opening,char closing){

if(opening == '(' && closing == ')') return true;

else if(opening == '{' && closing == '}') return true;

else if(opening == '[' && closing == ']') return true;

return false;

}

bool Balanced(string exp){

Stack S;

for(int i =0;i

if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')

S.push(exp[i]);

else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){

if(S.isEmpty() || !Pair(S.peek(),exp[i]))

return false;

else

S.pop();

}

}

return S.isEmpty() ? true:false;

}

int main(){

string expression; // input expression from console

cout

cin>>expression;

// check expression

if(Balanced(expression))

cout

else

cout

return 0;

}

Description: Part I: Implement stack using array representation Your program should support normal stack operations as learned from the class Part II: Make use of your own stack implementation (from Part I) to check any program for balanced symbols: .0.0 Example: t0;, 0);: legal (G, (O;: illegal Write your program using C++, it reads the string of symbols from a program file (e.g.. program.c) and print "legal" or "illegal" as the result

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

3. Are you varying your speaking rate and volume?

Answered: 1 week ago