//-------------------------------------------------------------------------- // Manarags II // anagram generator by Tom 7 //-------------------------------------------------------------------------- #include #include #include #include #define MAXWORDLEN 32 #define STATMOD 1000 struct element { string word; element * next; }; int printlist(element*); void destroy (element**); void buildlist(string,element**&); string subtract (string test, string in); void anagram (string, string, element**); int filter (int testlen, const char * test,string in); element * forced = NULL; string input; int RECURSION,STATUS; int QUIET=0, MINLEN=0, BIGGEST=0; main (int argc,char**argv) { string infile = "wordlist.asc"; if (!argv[1]) { cerr << "Manarags 2 by Tom Murphy 7\n" "http://tom7.org/\n\n" "mana [-q][-i wordfile][-m minlen][-f forced]\n [-f forced][...][-b biggestwords] phrasetoanagram\n\n" "recommended that you redirect to a file:\n\n" "mana theymightbegiants > tmbg.txt\n"; exit(-1); } for (int x=1;x -"<word; input = subtract(fd->word,input); fd = fd->next; } cerr << endl; } element ** wordlist = (element**) calloc(MAXWORDLEN,sizeof(element*)); buildlist(infile, wordlist); if (BIGGEST) { int n = MAXWORDLEN; while (--n>=0 && printlist(wordlist[n])); } else { if (!QUIET) cerr << "Anagramming...\n"; RECURSION=STATUS=0; anagram("",input,wordlist); } destroy(wordlist); free(wordlist); } void anagram(string done, string left, element**words) { if (!left.length()) { element * fd = forced; while (fd) { cout << fd->word << " "; fd = fd->next; } cout << done << endl; if (!QUIET) { if (!(STATUS % STATMOD)) cerr << STATUS << ": " << done << endl; STATUS++; } return; } int n = left.length()-1; if (n > MAXWORDLEN) n=MAXWORDLEN; for (;n>=MINLEN;--n) { if (!RECURSION && !QUIET) cerr << "[" << n+1 << "]\n"; element * h = words[n]; while (h) { if (filter(h->word.length(),h->word.c_str(),left)) { RECURSION++; anagram(done + h->word + " ",subtract(h->word,left),words); RECURSION--; } h = h->next; } } } void destroy(element** ha) { element * t, * f; for (int n=0;nnext; delete t; } } } int printlist(element*h) { while (h) { cout << h->word << endl; h = h->next; if (!--BIGGEST) return 0; } return 1; } void buildlist(string fname,element**&wlist) { ifstream infile(fname.c_str()); string newword; while (infile >> newword) if (newword.length() >= MINLEN && newword.length() <= input.length() && newword.length() <= MAXWORDLEN && filter(newword.length(),newword.c_str(),input)) wlist[newword.length()-1] = new element((element){newword,wlist[newword.length()-1]}); infile.close(); } string subtract(string test, string in) { int u,v; string out; for (u=0;u