hey i am trying to modify the code to read a txt file to tellif the brackets are
ID: 3616652 • Letter: H
Question
hey i am trying to modify the code to read a txt file to tellif the brackets are matching but my program will only work if thereis a missing right bracket ex: ()()) for all other inputs such as()()()--balanced or ()()( --unbalanced the stackoverflows....Appreciate any help in fixing this problemthanks#include <stdlib.h> #include <iostream> #include <cstdlib> #include <fstream> #include<string> using namespace std;
const int maxstack = 100;
typedef char stackitems;
// ************ STRUCT STACK **************** struct stack { stackitems data[maxstack] ; // array where data isstored int stacktop ; // when it is -1, stack is empty };
stack s;
// ************ FUNCTION INITSTACK **************** void initstack(stack& s) // empty the stack { s.stacktop =-1; // set stacktop to be empty }
// ************ FUNCTION ISEMPTY **************** bool isEmpty(const stack& s) // returns true orfalse
{ return s.stacktop==-1; // function is true if stacktop hasvalue of -1 }
// ************ FUNCTION POP **************** void pop ( stack& s, stackitems& x) // removes the topitem { if (isEmpty(s)) // if stack is empty, cannot removedata { cout << " Stack underflow "<<endl; exit(1); //terminate the program } else { x = s.data[s.stacktop]; // retrieve datafirst s.stacktop--; // remove fromstacktop cout << "Has popped data '" << x << "' fromstack. "; // display info to the user } }
// ************ FUNCTION PUSH **************** void push( stack& s, const stackitems& x) // places element x on the stack { if (s.stacktop == maxstack -1) // not more thanmaxstack-1 is allowed { cout<<" Stack overflow "; // items on thestack. exit(1); //terminate the program } else { s.stacktop++; // move the top of the stack up s.data[s.stacktop]=x; // add new item,x cout << "Has added data '" << x << "' tostack. "; // display message to the user } }
// ************ FUNCTION TOP **************** stackitems top(const stack& s) //returns the top item from the stack in parameter x { if (isEmpty(s)) { cout<<"Stack underflow"<<endl; exit(1); } else return s.data[s.stacktop] ; }
// ************ FUNCTION CHECK **************** void check(ifstream& in) { //parenthases char L_paren = '('; char R_paren = ')'; //braces char L_brace = '{'; char R_brace = '}'; //Square brackets char L_square = '['; char R_square = ']'; // char c_out_left = '<'; char c_out_right = '<';
char c_in_right = '>'; char c_in_left = '>';
bool error = false; char ch;
in.get(ch);
while (( ch != ' ') && (!error) ) { if (ch == L_paren || ch==L_brace || ch==L_square||ch==c_out_left ||ch==c_in_left)
push(s,ch);//intialize function push
if (ch == R_paren || ch==R_brace || ch==R_square ||ch==c_out_right|| ch==c_in_right)
if (isEmpty(s)) {
//cout<<"Unmatched closing bracket" <<ch<<"detected." <<endl; error = true; } else pop(s,ch); in.get(ch); } if ((!error) && isEmpty(s))
cout<<" Result: This expression is not balanced. "<<endl;
else cout <<" Result: This expression is balanced. "<<endl; } void OpenInFile ( ifstream& SomeInFile)
{ string FileName; cout <<"*********************************************** "; cout << "This Program Reads an External C++ File and "; cout << "Determins if brackets {,[,(,<<,>>are balanced. "; cout << "File must be in same folder as thisprogram "; cout <<"*********************************************** "; cout << "Please type the name of your input file: "; cin >> FileName; cout <<" Accessing File "<<FileName<<" ";
//opening file SomeInFile.open(FileName.c_str());
//File not found if (SomeInFile.fail())
{
cout << "Warning: File "<< FileName <<" NotFound " << endl; exit (1);
} } // ************ FUNCTION MAIN **************** void main () {
char tocontinue; ofstream out; ifstream in;
initstack(s); // initialising stack to -1; i.e. it isempty //OpenOutFile (out); OpenInFile (in); check(in);
} // end main
hey i am trying to modify the code to read a txt file to tellif the brackets are matching but my program will only work if thereis a missing right bracket ex: ()()) for all other inputs such as()()()--balanced or ()()( --unbalanced the stackoverflows....Appreciate any help in fixing this problemthanks
#include <stdlib.h> #include <iostream> #include <cstdlib> #include <fstream> #include<string> using namespace std;
const int maxstack = 100;
typedef char stackitems;
// ************ STRUCT STACK **************** struct stack { stackitems data[maxstack] ; // array where data isstored int stacktop ; // when it is -1, stack is empty };
stack s;
// ************ FUNCTION INITSTACK **************** void initstack(stack& s) // empty the stack { s.stacktop =-1; // set stacktop to be empty }
// ************ FUNCTION ISEMPTY **************** bool isEmpty(const stack& s) // returns true orfalse
{ return s.stacktop==-1; // function is true if stacktop hasvalue of -1 }
// ************ FUNCTION POP **************** void pop ( stack& s, stackitems& x) // removes the topitem { if (isEmpty(s)) // if stack is empty, cannot removedata { cout << " Stack underflow "<<endl; exit(1); //terminate the program } else { x = s.data[s.stacktop]; // retrieve datafirst s.stacktop--; // remove fromstacktop cout << "Has popped data '" << x << "' fromstack. "; // display info to the user } }
// ************ FUNCTION PUSH **************** void push( stack& s, const stackitems& x) // places element x on the stack { if (s.stacktop == maxstack -1) // not more thanmaxstack-1 is allowed { cout<<" Stack overflow "; // items on thestack. exit(1); //terminate the program } else { s.stacktop++; // move the top of the stack up s.data[s.stacktop]=x; // add new item,x cout << "Has added data '" << x << "' tostack. "; // display message to the user } }
// ************ FUNCTION TOP **************** stackitems top(const stack& s) //returns the top item from the stack in parameter x { if (isEmpty(s)) { cout<<"Stack underflow"<<endl; exit(1); } else return s.data[s.stacktop] ; }
// ************ FUNCTION CHECK **************** void check(ifstream& in) { //parenthases char L_paren = '('; char R_paren = ')'; //braces char L_brace = '{'; char R_brace = '}'; //Square brackets char L_square = '['; char R_square = ']'; // char c_out_left = '<'; char c_out_right = '<';
char c_in_right = '>'; char c_in_left = '>';
bool error = false; char ch;
in.get(ch);
while (( ch != ' ') && (!error) ) { if (ch == L_paren || ch==L_brace || ch==L_square||ch==c_out_left ||ch==c_in_left)
push(s,ch);//intialize function push
if (ch == R_paren || ch==R_brace || ch==R_square ||ch==c_out_right|| ch==c_in_right)
if (isEmpty(s)) {
//cout<<"Unmatched closing bracket" <<ch<<"detected." <<endl; error = true; } else pop(s,ch); in.get(ch); } if ((!error) && isEmpty(s))
cout<<" Result: This expression is not balanced. "<<endl;
else cout <<" Result: This expression is balanced. "<<endl; } void OpenInFile ( ifstream& SomeInFile)
{ string FileName; cout <<"*********************************************** "; cout << "This Program Reads an External C++ File and "; cout << "Determins if brackets {,[,(,<<,>>are balanced. "; cout << "File must be in same folder as thisprogram "; cout <<"*********************************************** "; cout << "Please type the name of your input file: "; cin >> FileName; cout <<" Accessing File "<<FileName<<" ";
//opening file SomeInFile.open(FileName.c_str());
//File not found if (SomeInFile.fail())
{
cout << "Warning: File "<< FileName <<" NotFound " << endl; exit (1);
} } // ************ FUNCTION MAIN **************** void main () {
char tocontinue; ofstream out; ifstream in;
initstack(s); // initialising stack to -1; i.e. it isempty //OpenOutFile (out); OpenInFile (in); check(in);
} // end main
Explanation / Answer
#include #include #include #include using namespace std; const int maxstack = 100; typedef char stackitems; // ************ STRUCT STACK **************** struct stack { stackitems data[maxstack] ; // array where data isstored int stacktop ; // when it is -1, stack is empty }; stack s; // ************ FUNCTION INITSTACK **************** void initstack(stack& s) // empty the stack { s.stacktop =-1; // set stacktop to be empty } // ************ FUNCTION ISEMPTY **************** bool isEmpty(const stack& s) // returns true or false { return s.stacktop==-1; // function is true if stacktop has value of-1 } // ************ FUNCTION POP **************** void pop ( stack& s, stackitems& x) // removes the topitem { if (isEmpty(s)) // if stack is empty, cannot removedata { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.