In C++
Only the evalPalindrome() method in Palindrome.cpp needs to be modified, everything else is good.
StackMain.cpp
#include #include #include #include #include #include "Palindrome.h" using namespace std;
void testForPalindrome(string origPhrase);
int main (void) { const int NUM_PHRASES = 4; string allPhrases[NUM_PHRASES] = {"kayak", "Read dear", "Madam, I'm Adam", "The End"}; for (int i = 0; i
void testForPalindrome(string origPhrase) { string reversePhrase;
Palindrome phrase(origPhrase);
if (phrase.evalPalindrome(reversePhrase)) { cout
Palindrome.cpp
#include "Palindrome.h" using namespace std;
Palindrome::Palindrome () { phrase = ""; }
Palindrome::Palindrome (string newPhrase) { phrase = newPhrase; } bool Palindrome::evalPalindrome (string& reversePhrase) { //Place code here }
ArrayStack.cpp
#include // For assert #include "ArrayStack.h" // Header file
template ArrayStack::ArrayStack() : top(-1) { } // end default constructor
// Copy constructor and destructor are supplied by the compiler
template bool ArrayStack::isEmpty() const { return top template bool ArrayStack::push(const ItemType& newEntry) { bool result = false; if (top template bool ArrayStack::pop() { bool result = false; if (!isEmpty()) { top--; result = true; } // end if return result; } // end pop
template ItemType ArrayStack::peek() const { assert(!isEmpty()); // Enforce precondition // Stack is not empty; return top return items[top]; } // end peek // End of implementation file.
StackInterface.h
#ifndef _STACK_INTERFACE #define _STACK_INTERFACE
template class StackInterface { public: /** Sees whether this stack is empty. @return True if the stack is empty, or false if not. */ virtual bool isEmpty() const = 0; /** Adds a new entry to the top of this stack. @post If the operation was successful, newEntry is at the top of the stack. @param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */ virtual bool push(const ItemType& newEntry) = 0; /** Removes the top of this stack. @post If the operation was successful, the top of the stack has been removed. @return True if the removal is successful or false if not. */ virtual bool pop() = 0; /** Returns the top of this stack. @pre The stack is not empty. @post The top of the stack has been returned, and the stack is unchanged. @return The top of the stack. */ virtual ItemType peek() const = 0; }; // end StackInterface #endif
Palindrome.h
#include #include #include "ArrayStack.h" using namespace std;
#ifndef PALINDROME_H #define PALINDROME_H class Palindrome { private: /** * stores the letters in the string while it is being evaluated * to determine if it is a palindrome */ ArrayStack letters;
/** * original phrase to evaluate to determine if it is a palindrome */ string phrase;
public: /** * Default constructor. Initializes expression to an empty string. */ Palindrome ();
/** * Overloaded constructor that initializes the phrase. * @param - newPhrase - original phrase tested to determine if it is a * palindrome */ Palindrome (string newPhrase); /** * Evaluates the phrase to determine if it is a palindrome. Uses * a stack to reverse the order of the letters in the phrase and * to determine if the original phrase is a palindrome. A palindrome * is a sequence of letters that reads the same forward and backward; * however, all spaces and punctuation are ignored. * @return - true if phrase is a palindrome; false otherwise * @param reversePhrase - orginal phrase in reverse order, including * all puncutation and spaces */ bool evalPalindrome (string& reversePhrase); }; #endif
ArrayStack.h
#ifndef _ARRAY_STACK #define _ARRAY_STACK
#include "StackInterface.h"
const int MAX_STACK = 50;
template class ArrayStack : public StackInterface { private: ItemType items[MAX_STACK]; // Array of stack items int top; // Index to top of stack public: ArrayStack(); // Default constructor bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); ItemType peek() const; }; // end ArrayStack
#include "ArrayStack.cpp" #endif
Write a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. For this assignment, most of the code is completed. Click here to download the files needed for this Lab. Review the included files. The main) method uses the overloaded constructor to declare several objects and initialize them to various phrases to test your program for correctness. For each test phrase, the application outputs a message about whether the phrase is a palindrome or not (based on the value returned by evalPalindrome0) the original phrase, and the phrase in reverse order (using the value of reversePhrase changed by evalPalindrome0.) For this lab assignment, implement the evalPalindrome) method, as defined in the Javadoc comments found inside the class definition above. The method you write MUST make use of ArrayStack named letters to reverse the order of the original phrase and to determine if the phrase is a palindrome or not. In other words, the method you write must call the methods in the ArrayStack class to reverse the order of the original phrase and to determine if it is a palindrome. Note that a library named cctype is included in Palindrome.h. This library contains methods like toupper0, tolower. isalpha0, and ispunct0, which you may find useful