Question
I need to combine both these C++ programs I have pasted below into one program. #include #define MAX 1000 using namespace std; class Stack{ int
I need to combine both these C++ programs I have pasted below into one program.
#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; } int main(){ string expression; // input expression ifstream in("program.c"); if(in.is_open()) { expression = ""; string line; while(getline(in, line)) { expression += line+" "; } } else { cout << "failed to open file" << endl; return 0; } // check expression if (Balanced(expression)) cout << "Legal "; else cout << "Illegal "; return 0; } #include #include #include using namespace std; class stack{ string stk[30]; //stack for holding string data int top; public: stack(){ //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 ch,popout; stack 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started