예제 #1
0
void queryMode(__gnu_cxx::hash_map<string, details> &hashMap)
// REQ: valid filled hash map
// MOD: none
// EFF: enters and input mode and lets the user query about data located in hash map
{
	string input, lower;
	bool found = false;
	
	cout << "Enter word: ";
	getline(cin, input);
	
	while (input != ":quit")
	{
		if (input == "")
		{
			cout << "Appeared 0 times\n";
		}
		else
		{
			parseQuery(input);	
			lower = lowerCase(input);
			found = hashMap.count(lower);
		
			if (found == 0)
			{
				cout << "Appeared 0 times\n";
			}
			else
			{
				if (hashMap[lower].unique == true && (input != hashMap[lower].appear))
				{
					cout << "Appeared " << hashMap[lower].appearances << " times as \"" 
						 << hashMap[lower].appear << "\"\n";
				}
				else
				{
					cout << "Appeared " << hashMap[lower].appearances << " times\n";
				}
			}
		}	
		cout << "Enter word: ";
		getline(cin, input);
	}
	cout << "Quitting\n";
	return;
}
예제 #2
0
void evaluateWord(__gnu_cxx::hash_map<string, details> &hashMap, ostream &outFile, string &word, int &numWords)
// REQ: valid output file
// MOD: hash map
// EFF: given a word and a hash map, indexes the word into the hash map, otherwise updates information about
//       the number of appearances for a given word
{
 	while (word.substr(0, 1) == "'") // removes all ' at the beginning of the string
 	{
 		word = word.substr(1, word.length() - 1);
 	}
 	while (word.substr(word.length() - 1, 1) == "'") // removes all ' at the end of the string
 	{
 		word = word.substr(0, word.length() - 1);
 	}
 	
 	string lower = lowerCase(word);
	
	bool found = hashMap.count(lower);

	if (found == 0)  // place word in hash map
	{
		details tmp;
			tmp.appear = word;
			tmp.unique = true;
			tmp.wordNum = numWords;
			tmp.appearances = 1;
		hashMap[lower] = tmp;
		outFile << numWords << " ";
		numWords++;
	}
	else  // updates information about entry in hash map
	{
		if (hashMap[lower].unique && (hashMap[lower].appear != word))
		{
			hashMap[lower].unique = false;
		}
		hashMap[lower].appearances++;
		outFile << hashMap[lower].wordNum << " ";
	}	
	
	word = "";
	return;
}