/** Add word to a new or existing chain. */ void testNewWord(const StringWrap& word, vector<PeekDeque<StringWrap>* >& chains, const bool& allowDuplicate, const bool& stepGrowth) { bool foundChain = false; bool isDuplicate; for(std::vector<int>::size_type i = 0; i != chains.size(); i++) { // check to see if we can add word into EXISTING chain if(ed1(word.str(), chains.at(i)->returnFront().str()) && !foundChain) { // check front of chain if(!allowDuplicate) { isDuplicate = checkDuplicate(word, i, chains); if(isDuplicate) {break;} } if(stepGrowth && chains.at(i)->returnFront().str().length() >= word.str().length()) { break; } chains.at(i)->pushFront(word); foundChain = true; break; } if(ed1(word.str(), chains.at(i)->returnRear().str()) && !foundChain) { // check rear of chain if(!allowDuplicate) { isDuplicate = checkDuplicate(word, i, chains); if(isDuplicate) {break;} } if(stepGrowth && chains.at(i)->returnRear().str().length() <= word.str().length()) { break; } chains.at(i)->pushRear(word); foundChain = true; break; } } if(!foundChain) { // otherwise create a NEW chain PeekDeque<StringWrap>* newpd = new PeekDeque<StringWrap>(1500); // might need to adjust this to avoid seg fault on larger text files newpd->pushFront(word); chains.push_back(newpd); } }
/** Check for re-occurrence of a specific string in a chain */ bool checkDuplicate(const StringWrap& word, int i, vector<PeekDeque<StringWrap>* >& chains) { for (int k = chains.at(i)->returnFrontItem(); k < chains.at(i)->returnRearSpace(); k++) { chains.at(i)->setPeekIndex(k); if(chains.at(i)->peek().str() == word.str()) { return true; } } return false; }
int main(int argc, char** argv) { if(argc < 2 || argc > 3) { exit(EXIT_FAILURE); } string infileName = argv[1]; int n = -1; if(argc == 3) { string num = argv[2]; if(!isValidInteger(num)) { cout <<"Invalid" << endl; exit(EXIT_FAILURE); } n = atoi(argv[2]); } ifstream* INFILEp = new ifstream(infileName.c_str(), ios_base::in); if(INFILEp == NULL) { cout << "textless" << endl; exit(EXIT_FAILURE); } vector< FlexArray<string> *> faVec; FlexArray<string>* fa= new FlexArray<string>(100); faVec.push_back(fa); string word; int m = n - abs(n); *INFILEp >> word; StringWrap* sw_1 = new StringWrap(word); sw_1->trimNonAlpha(); sw_1->makeLower(); word = sw_1->str(); fa->insert(fa->begin(), word); while ((*INFILEp) >> word && m < n) { if(word.length() > 2) { StringWrap* sw = new StringWrap(word); sw->trimNonAlpha(); sw->makeLower(); word = sw->str(); bool hasAppeared = false; for(int i = 0; i < faVec.size(); i++) { FlexArray<string>* curFa = faVec[i]; FlexArray<string>::iterator it = curFa->begin(); while(!curFa->isTheEnd(it)) { string curWord = curFa->at(it); if(curWord == word) hasAppeared = true; it = it.operator++(9); } } if(!hasAppeared) { bool isNew = true; for(int i = 0; i < faVec.size(); i++) { FlexArray<string>* curFa = faVec[i]; FlexArray<string>::iterator it = curFa->begin(); while(!curFa->isTheEnd(it)) { string curWord = curFa->at(it); if(ed1(curWord, word)) { curFa->insert(it, word); isNew = false; break; } it = it.operator++(9); } } if(isNew) { FlexArray<string>* faNew = new FlexArray<string>(100); faNew->insert(faNew->begin(), word); faVec.push_back(faNew); } } } if(m >= 0) m++; } vector< FlexArray<string>* > soBig; vector<string> longestWords; int maxDif = 0; FlexArray<string>* curFa; for(int i = 0; i < faVec.size(); i++) { FlexArray<string>* curFa = faVec[i]; FlexArray<string>::iterator it = curFa->begin(); int shortestLength = curFa->at(it).length(); int greatestLength = curFa->at(it).length(); string longestWord = curFa->at(it); int count = 0; while(!curFa->isTheEnd(it)) { string curWord = curFa->at(it); if(curWord.length() < shortestLength) shortestLength = curWord.length(); else if(curWord.length() > shortestLength) { greatestLength = curWord.length(); longestWord = curWord; } count++; it = it.operator++(42); } if(count >= 2) { if(greatestLength-shortestLength > maxDif) { soBig.clear(); maxDif = greatestLength - shortestLength; soBig.push_back(curFa); } else if (greatestLength-shortestLength == maxDif) { soBig.push_back(curFa); } longestWords.push_back(longestWord); } } cout << "Chains with longest word differences" << endl; for(int i = 0; i < soBig.size(); i++) { curFa = soBig[i]; FlexArray<string>::iterator it = curFa->begin(); cout << "New Chain:" << endl; while(!curFa->isTheEnd(it)) { cout << curFa->at(it) << endl; it = it.operator++(69); } } cout << endl; cout << "Longest words in chains" << endl; for(int i = 0; i < longestWords.size(); i++) { cout << longestWords[i] << endl; } INFILEp->close(); }