Could someone possibly help me with this C++ program? I think I understand how t
ID: 3816734 • Letter: C
Question
Could someone possibly help me with this C++ program? I think I understand how to read in a cpp file (not quite sure) but I'm confused how to write code to check for the correct sequences and output the legal sequences. Thanks!
In code, every { must have a corresponding }
Similarly, every [ must have a corresponding ] and every ( must have a corresponding )
Furthermore, the symbols must be “appropriately closed” in the sense that they are closed in the reverse of the order that they are opened.
For example:
{[]} ß is a legal sequence
{[}] ß is NOT a legal sequence because the } was closed when the [ was the last character that was opened
[[]] ß is a legal sequence
[{}{}] ß is a legal sequence
{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}} ß is a legal sequence
{ ß is NOT a legal sequence because the { is never closed
Going further:
for(int i = 0; i < 10; i++){ cout << i; } ß is a legal sequence
And so on.
Your objective is to write a program that reads in a .cpp file and determines if the {},[],() are legally sequenced.
Your program should include a Stack class with the += and -= operators overloaded in order to implement push and pop.
For example, if you want to add a character to the stack, you should NOT use something like:
myStack.push(myChar);
Instead…
myStack += myChar;
Both += and -= will return a Boolean true of false indicating if the operation was successful. An attempt to pop a char from an empty stack will return false.
If you want to remove a character from the stack, you should use something like:
if (myStack -= myChar)
Process myChar;
else
Process Error;
You are NOT allowed to use the standard library “stack” class. You must implement your own stack class. You may use the standard library vector class as a private member variable to help implement your stack or you may implement your stack as a linked-list where a push adds a char to the head of the list and a pop removes a char from the head of the list.
Lastly, your stack class should be implemented in a separate .H and .cpp file. They must NOT be implemented in the same file as your main function.
Once complete, your code should simply output whether or not the evaluated cpp file is a logical sequence of [,{,(
Explanation / Answer
/*match_braces.hpp*/
#include <vector>
struct Stack
{
std::vector<char> v;
bool operator+=(const char rhs);
bool operator-=(const char rhs);
void display();
};
bool is_bracket_open(char c);
bool is_bracket_close(char c);
bool is_bracket(char c);
char open_form_of(char c);
bool is_valid(std::string s);
/*match_braces.cpp*/
#include <iostream>
#include "match_braces.hpp"
using namespace std;
bool Stack::operator+=(const char rhs)
{
v.push_back(rhs);
return true;
}
bool Stack::operator-=(const char rhs)
{
cout<<"Poping --"<<rhs<<endl;
if(v.empty())return false;
if(v.back()==rhs)
{
v.pop_back();
return true;
}
return false;
}
void Stack::display(){
for(int i=0;i<v.size();i++){
cout<<v[i];
}
cout<<endl;
}
bool is_bracket_open(char c){
string braces = "({[";
for (int i = 0;i<braces.length();i++){
if(braces[i]==c)return true;
}
return false;
}
bool is_bracket_close(char c){
string braces = "}])";
for (int i = 0;i<braces.length();i++){
if(braces[i]==c)return true;
}
return false;
}
bool is_bracket(char c){
return is_bracket_close(c) || is_bracket_open(c);
}
char open_form_of(char c){
string open_brac = "({[";
string close_brac = ")}]";
for(int i=0;i<close_brac.length();i++){
if(close_brac[i]==c)return open_brac[i];
}
return '$';//error
}
bool is_valid(string s){
Stack st;
for(int i = 0 ;i<s.length();i++){
if(is_bracket_open(s[i])){
st+=s[i];
}else{
if(!(st-=open_form_of(s[i])))return false;
}
st.display();
}
return true;
}
/*match_braces_driver.cpp*/
#include "match_braces.cpp"
#include <fstream>
int main(int argc, char const *argv[])
{
char str[256];
std::cout << "Enter the name of the file: ";
std::cin.get (str,256); // get c-string
std::ifstream is(str); // open file
string content = "";
char c;
// loop getting single characters
while (is.get(c)) {
/*only brackets are useful to us*/
if(is_bracket(c)){
content += c;
}
}
std::cout << content<<endl;
if(is_valid(content)){
cout<<"Valid";
}else{
cout<<"Invalid";
}
cout<<endl;
is.close(); // close file
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.