Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have to add these functions to my C++ program that is pasted below for the following: a) it can check whether a C/C++ source

I have to add these functions to my C++ program that is pasted below for the following:

a) it can check whether a C/C++ source program file has balanced symbols;

b) it can check whether a HTML source file has balanced tags;

c) it reads a C/C++ source program file (.c, or .cpp) or a HTML source file (.htm or .html). And based on the input file type, it can automatically decide whether to perform function a) or b) or return an error message on unknown file types;

d) it prints legal on program files with balanced symbols/tags, otherwise it prints illegal and provide debug information such as which line(s) contain which illegal symbols/tags;

e) implement your own stack data structure and use it for both a) and b);

#include

#define MAX 1000

using namespace std;

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 << "Stack is full it is Overflowing";

return false;

}

else{

a[++top] = x;

return true;

}

}

int Stack::pop(){

if (top < 0)

return 0;

else{

int x = a[top--];

return x;

}

}

//Checks the stack if its empty or not

bool Stack::isEmpty(){

return (top < 0);

}

// 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;

}

// class Stack of program 2

class stack2{

string stk[30]; //stack for holding string data

int top;

public:

stack2(){ //initialize pointer to -1

top=-1;

}

void push(string x){ //push string to stack

if(top > 30){ //if stack is full

cout <<"stack overflow"; //tells the user the stack overflow

return;

}

stk[++top]=x; //else increase stack pointer and insert into stack

}

string pop(){ //pop string from stack

if(top <0){ //if stack is empty

cout <<"stack underflow"; //tells the user stack is underflow

return "null"; //return with null

}

return stk[top--]; //returns stack top and then decreases the pointer

}

bool isempty(){ //chack stack empty or not

if(top<0)

return true; //if empty return true

}

};

int main(){

string expression; // input expression

ifstream in("program.c");

if(in.is_open()){

expression = "";

string line;

while(getline(in, line)){

expression += line+" ";

}

// check expression

if (Balanced(expression))

cout << "Legal ";

else

cout << "Illegal ";

}

else{

cout << "failed to open file" << endl;

//return 0;

}

string ch,popout;

stack2 st;

ifstream infile ("et.html"); //open txt file

while(infile>>ch){ //read text file

if(ch[0]=='<'&&ch[1]!='/'&&ch[ch.length()-1]=='>')

st.push(ch);

if(ch[0]=='<'&&ch[1]=='/'&&ch[ch.length()-1]=='>'){

popout=st.pop(); //pops the stack

if(ch.length()!=popout.length()+1){

cout<<"Illegal"; //prints illegal

return 0;

}

for (int i=2;i

if(ch[i]!=popout[i-1]){

cout<<"Illegal"; // prints illegal

return 0;

}

}

}

}

if(!st.isempty()){ //after all strings are read from file it checks if the stack is empty

cout<<"Illegal"; // prints illegal

return 0;

}

else

cout<<"Legal"; //prints legal

return 0;

}

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

Explain what scientific methods are and why they are important.

Answered: 1 week ago