I need a typed description of my C++ program that is pasted below for the follow
ID: 3698769 • Letter: I
Question
I need a typed description of my C++ program that is pasted below for the following:
a) Overview of your program;
b) Usage manual of your program;
c) Description of your solutions (including limitations);
d) Potential improvements on your program;
#include<bits/stdc++.h>
#include<string>
#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_C(string exp){
Stack S;
for(unsigned int i =0;i<exp.length();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 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(){ //checks stack if its empty or not
if(top<0)
return true; //if empty return true
return false;
}
};
bool Balanced_HTML(ifstream &infile){
string ch,popout;
stack2 st;
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){
return false;
}
for (unsigned int i=2;i<ch.length()-2;i++){ //checks for same type of closing tag
if(ch[i]!=popout[i-1]){
return false;
}
}
}
}
if(!st.isempty()){ //after all strings are read from the file it checks if the stack is empty
return false;
}
else
return true;
}
int main(){
string expression; // input expression
string filename;
cout << "Enter program file name:";
cin >> filename;
ifstream in(filename.c_str());
if(in.is_open()){
int pos = filename.find("."); //Gets postion based on "."
++pos;
if(filename.substr(pos) == "c" || filename.substr(pos) == "cpp"){ //Checks for c/cpp file
cout << "Calling C/CPP function..." << endl;
cout << filename.substr(pos) <<" Function Result : ";
expression = "";
string line;
while(getline(in, line)){
expression += line+" ";
}
// checks expression
if (Balanced_C(expression))
cout << "Legal ";
else
cout << "Illegal ";
}
else if(filename.substr(pos) == "htm" || filename.substr(pos) == "html"){ //checks for htm/html file
cout << "Calling HTML function..." << endl;
cout << filename.substr(pos) << " Function Result : ";
if (Balanced_HTML(in))
cout << "Legal ";
else
cout << "Illegal ";
}
else
cout << "Error, unknown file type"; //Unknown file
}
else{
cout << "failed to open file" << endl;
}
return 0;
}
Explanation / Answer
Overview of program:
This program will check whether given file is Balanced or not. Example: Suppose we give input file as c or cpp, it will check the blocks i.e., (), {}, []. If we give html. Whether Tags are balanced are not, chck for begining and ending tags...
Usage: Whenever we want to check, whether given file is balaced or not.. we must give filename along with extension. example: sample.c, sample.cpp, sample.html, sample.htm only.
This program is build on using stacks using array... generally arrays are used with static data. It can handle certain limit..for our program it can handle max 1000 blocks.
Instead of using arrays, if we use linked list, it will be create memory with dynamically. we can handle with unlimited data...we don't need to implement two stacks for C and html. If we create objects, it will be sufficient.. because both are performing same operations...
If you have sill any doubts, please give me a comment...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.