Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C++ code problem (if you can include makefile as well). Thank You! StackOfChars.h is going to use Node.h. Here is my code

Need help with C++ code problem (if you can include makefile as well). Thank You!

StackOfChars.h is going to use Node.h. Here is my code for Node.h and StackofChars.h you can see if it is right or not:

//Node.h

#ifndef NODE_H #define NODE_H

#include using namespace std;

class Node { private: char m_entry; Node* m_next;

public: Node(char entry); char getEntry() const; void setEntry(char entry); Node* getNext() const; void setNext(Node* next);

};

Node::Node(char entry) : m_entry(entry), m_next(NULL) {}

char Node::getEntry() const { return m_entry; }

void Node::setEntry(char entry) { m_entry = entry; }

Node* Node::getNext() const { return m_next; }

void Node::setNext(Node *next) { m_next = next; }

#endif

//StackofChars.h

#ifndef STACKOFCHARS_H #define STACKOFCHARS_H

#include "Node.h"

class StackOfChars { private: Node* m_top;

public:

StackOfChars();

StackOfChars(const StackOfChars& orig);

~StackOfChars(); void operator=(const StackOfChars& rhs); void push(char entry);

void pop();

char peek() const;

bool isEmpty() const;

};

StackOfChars::StackOfChars() : m_top(NULL) {

}

StackOfChars::StackOfChars(const StackOfChars &orig) { m_top = NULL; Node *currOrig = orig.m_top; Node *curr = m_top;

while(currOrig != NULL) { Node *node = new Node(currOrig->getEntry()); if(curr == NULL) { m_top = node; curr = m_top; } else { curr->setNext(node); curr = curr->getNext(); }

currOrig = currOrig->getNext(); } }

StackOfChars::~StackOfChars() { while(m_top != NULL) { Node *temp = m_top; m_top = m_top->getNext(); delete(temp); } }

void StackOfChars:: operator=(const StackOfChars& rhs) { while(m_top != NULL) { Node *temp = m_top; m_top = m_top->getNext(); delete(temp); }

Node *currOrig = rhs.m_top; Node *curr = m_top;

while(currOrig != NULL) { Node *node = new Node(currOrig->getEntry()); if(curr == NULL) { m_top = node; curr = m_top; } else { curr->setNext(node); curr = curr->getNext(); }

currOrig = currOrig->getNext(); } }

void StackOfChars:: push(char entry) { Node *node = new Node(entry); if(m_top == NULL) m_top = node; else { node->setNext(m_top); m_top = node; } }

void StackOfChars:: pop() { if(m_top != NULL) { Node *node = m_top; m_top = m_top->getNext(); delete(node); } }

char StackOfChars:: peek() const { return m_top->getEntry(); }

bool StackOfChars:: isEmpty() const { return(m_top == NULL); }

#endif

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Requirements You will create a program that can ran in two modes, test mode and parser mode. From the command-line the user will decide if they want to launch your test suite (-t) or enter parser mode (-p). Sample executions: To enter test mode $> ./labo2 -t To enter parser mode $> ./labo2 -p Test Mode Before attempting to use our Stacks to solve problems, we need to make sure they work. I have provided you with some starter tests that you must implement. You will also be required to add new tests for methods not mentioned here Each test should be ran in isolation, meaning the tests could be run in any order and they don't share any objects/data. Sample Test Output $>./labo2 -t Test #1: New stack is empty: PASSED Test #2: Push on empty stack makes it non-empty: PASSED Test #3: Popping all all elements makes stack empty: FAILED Test #4: Copy constructor copies all elements in correct order: FAILED Stack Tester class Runs a battery of tests to verify that our Stack is working Has a single entry point called run Tests() Each test prints what test is currently running and if the Stack passed or failed Each test method should work in a vacuum, meaning tests shouldn't pass Stack objects from test to test Sample StackTester.h class StackTester public: StackTester(); //This will call all your test methods void runTests(); private: * @brief Creates an empty stack and verifies isEmpty()) returns true void test01(); /** * @brief Creates an empty stack pushes 1 value, verifies isEmpty() returns false **/ void test02(); * @brief Creates an empty stack, then pushes once, pops once, and verifies is Empty returns true **/ void test03(); //more test methods as needed Parser Mode Once you know your Stack is working, begin working on parser mode. In this mode, the user will be allowed to enter a single string consisting of left and right curly braces. You must verify whether or not the sequence is balanced. Example runs: $>./lab02 -p Enter your sequence: {} Sequence is balanced. $>./lab02 -p Enter your sequence: }{ Sequence is not balanced. $>./lab02 -p Enter your sequence: {{{}} Sequence is not balanced. $>./lab02 -p Enter your sequence: {}{} Sequence is balanced. $>./lab02 -p Enter your sequence: {{{{{{}}}}}}{{{{{}}}}}{}{}{}{}{}{}{{}} Sequence is balanced. #ifndef STACKOFCHARS_H #define STACKOFCHARS_H class StackofChars private: Node* m_top; public: StackofChars(); StackofChars (const StackofChars& orig); -StackofChars(); void operator=(const StackofChars& rhs); /** Here's an example of a doxygen comment block. Do this for all methods * @pre None * @post entry is added to top of the stack * @param entry, the element to be added to the stack * @throw None **/ void push(char entry); /** Here's an example of a doxygen comment block. Do this for all methods * @pre Stack is non-empty * @post Top element is removed from the stack * @param None * @throw None **/ void pop(); char peek() const; bool isEmpty() const; }; #endif Node.h #ifndef NODE_H #define NODE_H class Node private: char m_entry; Node* m next; public: Node (char entry); char getEntry() const; void setEntry(char entry); Node* getNext() const; void setNext (Node* next); #endif Requirements You will create a program that can ran in two modes, test mode and parser mode. From the command-line the user will decide if they want to launch your test suite (-t) or enter parser mode (-p). Sample executions: To enter test mode $> ./labo2 -t To enter parser mode $> ./labo2 -p Test Mode Before attempting to use our Stacks to solve problems, we need to make sure they work. I have provided you with some starter tests that you must implement. You will also be required to add new tests for methods not mentioned here Each test should be ran in isolation, meaning the tests could be run in any order and they don't share any objects/data. Sample Test Output $>./labo2 -t Test #1: New stack is empty: PASSED Test #2: Push on empty stack makes it non-empty: PASSED Test #3: Popping all all elements makes stack empty: FAILED Test #4: Copy constructor copies all elements in correct order: FAILED Stack Tester class Runs a battery of tests to verify that our Stack is working Has a single entry point called run Tests() Each test prints what test is currently running and if the Stack passed or failed Each test method should work in a vacuum, meaning tests shouldn't pass Stack objects from test to test Sample StackTester.h class StackTester public: StackTester(); //This will call all your test methods void runTests(); private: * @brief Creates an empty stack and verifies isEmpty()) returns true void test01(); /** * @brief Creates an empty stack pushes 1 value, verifies isEmpty() returns false **/ void test02(); * @brief Creates an empty stack, then pushes once, pops once, and verifies is Empty returns true **/ void test03(); //more test methods as needed Parser Mode Once you know your Stack is working, begin working on parser mode. In this mode, the user will be allowed to enter a single string consisting of left and right curly braces. You must verify whether or not the sequence is balanced. Example runs: $>./lab02 -p Enter your sequence: {} Sequence is balanced. $>./lab02 -p Enter your sequence: }{ Sequence is not balanced. $>./lab02 -p Enter your sequence: {{{}} Sequence is not balanced. $>./lab02 -p Enter your sequence: {}{} Sequence is balanced. $>./lab02 -p Enter your sequence: {{{{{{}}}}}}{{{{{}}}}}{}{}{}{}{}{}{{}} Sequence is balanced. #ifndef STACKOFCHARS_H #define STACKOFCHARS_H class StackofChars private: Node* m_top; public: StackofChars(); StackofChars (const StackofChars& orig); -StackofChars(); void operator=(const StackofChars& rhs); /** Here's an example of a doxygen comment block. Do this for all methods * @pre None * @post entry is added to top of the stack * @param entry, the element to be added to the stack * @throw None **/ void push(char entry); /** Here's an example of a doxygen comment block. Do this for all methods * @pre Stack is non-empty * @post Top element is removed from the stack * @param None * @throw None **/ void pop(); char peek() const; bool isEmpty() const; }; #endif Node.h #ifndef NODE_H #define NODE_H class Node private: char m_entry; Node* m next; public: Node (char entry); char getEntry() const; void setEntry(char entry); Node* getNext() const; void setNext (Node* next); #endif

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

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions