/* * wordentry.cpp * * Created on: Oct 18, 2010 * Author: mike * for use in Yale course CPSC 427a, Fall 2010 */ #include "word.hpp" const char* Word::allowed_chars; int Word::triangle_max; int Word::triangle_min; bool Word::allowed_set[256]; // Construct a Word from a string. // Computes profile word. Word::Word(const char* cword) : len(strlen(cword)) { if (len > triangle_max) fatal("WordEntry: '%s' longer than triangle size %i", cword, triangle_max); strcpy(this->cword, cword); strcpy(profile, cword); sort(); } // Sort letters in profile using insertion sort void Word::sort() { int posmin; // position of min char on pass char curmin; // the char at position posmin for (int pass = 0; pass < len - 1; pass++) { posmin = pass; curmin = profile[pass]; for (int k = pass + 1; k < len; k++) { if (profile[k] < curmin) { posmin = k; curmin = profile[k]; } } profile[posmin] = profile[pass]; profile[pass] = curmin; } } // Tests if *this is an anagram of w2 with at most one letter deleted bool Word::operator<=(const Word& w2) const { // Need profile merge here int k; if (len > w2.len) return false; for (k=0; k Word::triangle_max) return false; for (int k = 0; k < len; k++) { if (!Word::allowed_set[(int) cword[k]]) return false; } return true; } ostream& Word::debugPrint(ostream& out) const { return out << cword << " (" << profile << ")"; } ostream& Word::print(ostream& out) const { return out << cword; }