#ifndef _ANAWORD_H #define _ANAWORD_H #include #include using namespace std; #include "tvector.h" // Used for finding anagrams: words with the same letters // but which are different words, e.g., "bagel" a "gable" // author: Owen Astrachan // // an Anaword object prints as a regular string, but // compares using a normalized (also called canonicalized) form // // Example: the Anaword version of the string "bagel" // prints as bagle, but will be compared with // other Anawords as a vector of counts // of one 'a', one 'b', one 'e', one 'g', one 'l' // Since the counts for "gable" are the same, "gable" // and "bagel" will be equal when compared using operator == // // basically an Anaword takes a string and converts it to a twenty-six // digit number based on the counts of a's, b's, c's, ... z's so that // aardvark is represented as: // // 3 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0 // // // operations: // // Anaword(const string & word) -- construct from a string // // bool equal(const Anaword & rhs) -- compare rhs for equality // bool operator == (lhs, rhs) -- compare Anawords lhs == rhs // // bool less(const Anaword & rhs) -- compare rhs for inequality < // bool operator < (lhs,rhs) -- compare Anawords lhs < rhs // bool operator <= (lhs,rhs) -- compare Anawords lhs <= rhs // // string toString() -- returns uncanonicalized "bagel" // ostream & << operator(ostream, -- print using << // Anaword) class Anaword { public: Anaword(const string & word); // construct from string Anaword(); // default (for vector) bool equal(const Anaword & rhs) const; // compare for == bool less(const Anaword & rhs) const; // compare for < string toString() const; // return "bagel" or "gable" private: void normalize(); // helper function, sorts string myWord; // regular string: "bagel" tvector myCounts; // canonicalized form }; bool operator == (const Anaword & lhs, const Anaword & rhs); bool operator != (const Anaword & lhs, const Anaword & rhs); bool operator < (const Anaword & lhs, const Anaword & rhs); bool operator <= (const Anaword & lhs, const Anaword & rhs); ostream & operator << (ostream & out, const Anaword & a); #endif