Question: Here is my assignment question: Download the CharStack.h and the test driver file from the class web page. You may also download the stack class

Here is my assignment question:

Download the CharStack.h and the test driver file from the class web page.

You may also download the stack class file from the class web page, but the stack class here is a

character stack and you need to change the StackElement to char. Write the implementation of

the function members of the CharStack class in a file named CharStack.cpp. (You can download

the two input files for testing.)

What I have so far:

stack.h

#ifndef STACK #define STACK #include using namespace std; const int CAPACITY = 128; typedef char StackElement; class Stack { public: void stack(); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); private: StackElement myArray[CAPACITY]; char myTop; StackElement garbage; }; inline void Stack::stack() //Purpose: constructor //Precondition: declared stack //Postcondition: stack has been declared as empty { myTop = -1; } inline bool Stack::empty() const //Purpose: Sees if stack is empty //Precondition: declared stack //Postcondition: returns true if stack is empty { return (myTop == -1); } #endif

stack.cpp

#include "stack.h" #include #include using namespace std; void Stack::push(const StackElement & value) //Purpose: adds value to stack //Precondition: stack and value to be added //Postcondition: stack with value added to the top unless full { if (myTop < CAPACITY - 1) { ++myTop; myArray[myTop] = value; } else { cerr << "Stack full." << endl; exit(1); } } void Stack::display(ostream & out) const //Purpose: displays values in stack //Precondition: full stack //Postcondition: displays the contents of stack { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; } StackElement Stack::top() const //Purpose: returns value at top of stack //Precondition: stack //Postcondition: returns value at the top of the stack { if (!empty()) return(myArray[myTop]); else { cerr << "Stack empty." << endl; return garbage; } } void Stack::pop() //Purpose: removes value at the top of the stack //Precondition: stack //Postcondition: returns the stack with top value removed { if (!empty()) myTop--; else cerr << "Stack empty." << endl; }

CharStack.h

#ifndef CHARSTACK #define CHARSTACK #include #include #include "stack.h" using namespace std; class CharStack { private: Stack myCharacters; public: void StringToStack(const string & inStr); friend ostream & operator <<(ostream & out, const CharStack & CS); CharStack Reverse(); bool IsEqual(CharStack & B); }; #endif CHARSTACK

CharStack.cpp

#include "CharStack.h" void CharStack::StringToStack(const string & inStr) //Purpose: reads string into its private stack //Precondition: checks if a character is a letter //Postcondition: letters of string are put in private stack { for (int i = 0; i < inStr.length(); i++) { if ((inStr[i] >= 'a'&&inStr[i] <= 'z') || (inStr[i] >= 'A' &&inStr[i] <= 'Z')) { myCharacters.push(inStr[i]); } } } CharStack CharStack::Reverse() //Purpose:empties its private stack into another stack for reversal //Precondition: private stack is not empty //Postcondition: reverses the stack { CharStack ReverseStack; Stack Temp = myCharacters; while (!Temp.empty()) { ReverseStack.myCharacters.push(Temp.top()); Temp.pop(); } return ReverseStack; } bool CharStack::IsEqual(CharStack & B) //Purpose: tests if both private stack and B's stack are the same //Precondition: neither stack is empty //Postcondition: if stack is empty, displays error otherwise sends true or false { if (myCharacters.empty() || B.myCharacters.empty()) { cerr << "Stack is empty." << endl; return false; } while (!myCharacters.empty() && !B.myCharacters.empty()) { if (tolower(myCharacters.top()) != tolower(B.myCharacters.top())) return false; myCharacters.pop(); B.myCharacters.pop(); } return myCharacters.empty() && B.myCharacters.empty(); } ostream & operator<<(ostream & out, const CharStack & CS) //Purpose: overloaded output //Precondition: stack is not empty //Postcondition: contents of stack have been output to ostream out { CharStack Temp = CS; while (!Temp.myCharacters.empty()) { out << Temp.myCharacters.top(); Temp.myCharacters.pop(); } return out; }

Main.cpp

#include #include #include #include #include #include "CharStack.h" #include "stack.h" using namespace std; int main(void) { ifstream in; string fileName, line[30]; int i = 0; CharStack N, M, P; cout << "Enter file name for palindrom check: " << endl; cin >> fileName; in.open(fileName.c_str()); assert(in.is_open()); while (!in.eof()) { getline(in, line[i]); N.StringToStack(line[i]); cout << N << endl; P = N; M = N.Reverse(); if (P.IsEqual(M)) cout << "This line is a palindrome line" << endl; else cout << "This line is not a palindrom line" << endl; i++; } cout << " Program ended normally. "; }

Test.txt

Yaweh. The way. Madam, I'm Adam. Lager, sir, is regal. Neil A. sees alien! Was it a rat I saw? Put Eliot's toilet up. Do geese see God? Enid and Edna dine. Never odd or even. Won ton? Not now! Not a banana baton! Ned, I am a maiden. Ma is a nun, as I am. I'm aloof; a fool am I. Too bad, I hid a boot. Todd erases a red dot. Flee to me, remote elf. He lived as a devil, eh? Poor Dan is in a droop. Rats live on no evil star. Panic in a Titanic, I nap.

But after I build and test the program all that comes up is "Stack full." What did I do wrong and need to change?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!