#include #include #include #include // for sort using namespace std; #include "tvector.h" #include "strutils.h" // for ToLower #include "prompt.h" /** * Simple anagram finder * @author Owen Astrachan * @date 1/9/2004 */ struct Ana { Ana() { // default constructor for tvector use } Ana(const string& s) : myWord(s), mySorted(s) { sort(mySorted.begin(), mySorted.end()); } string myWord; string mySorted; }; ostream& operator << (ostream& out, const Ana& a) // post: print Ana object { out << a.myWord; return out; } bool operator == (const Ana& lhs, const Ana& rhs) // post: return true if Ana objects are equal { return lhs.mySorted == rhs.mySorted; } void printAnagrams(const Ana& word, const tvector& v) // post: print all values in v that are equal to word { cout << "anagrams for " << word << " : "; for(int k=0; k < v.size(); k++){ if (word == v[k]){ cout << v[k] << " "; } } cout << endl; } int main(int argc, char * argv[]) { tvector list; string filename,word; if (argc > 1) { filename = argv[1]; } else { cout << "enter filename: "; getline(cin,filename); } ifstream input(filename.c_str()); while (input >> word) { list.push_back(Ana(word)); } while(true) { word = ""; cout << "word (return to stop) "; getline(cin,word); if (word == "") break; // out of loop printAnagrams(Ana(word),list); } }