コード例 #1
0
/**
 * Prints the concordance of the given file and performance information. Uses
 * the file input1.txt by default or a file name specified as a command line
 * argument.
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, const char** argv)
{
    // FIXME: implement
    HashMap* map = hashMapNew(1000);
    
    FILE* file = fopen("dictionary.txt", "r");
    clock_t timer = clock();
    loadDictionary(file, map);
    timer = clock() - timer;
    printf("Dictionary loaded in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);
    fclose(file);
    
    char inputBuffer[256];
    int quit = 0;
    while (!quit)
    {
        printf("Enter a word or \"quit\" to quit: ");
        scanf("%s", inputBuffer);
        
        // Implement the spell checker code here..
        
        if (strcmp(inputBuffer, "quit") == 0)
        {
            quit = 1;
        }
    }
    
    hashMapDelete(map);
    return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: jeremy-prater/OSU
int main(int argc, const char** argv)
{
    const char* fileName = "input1.txt";
    if (argc > 1)
    {
        fileName = argv[1];
    }
    printf("Opening file: %s\n", fileName);
    
    clock_t timer = clock();
    
    HashMap* map = hashMapNew(10);
    
    // --- Concordance code begins here ---
    printf ("Opened [%s]\n", fileName);
    FILE * file = fopen (fileName, "r");
    if (file == NULL)
    {
        printf ("File not found! [%s]\n", fileName);
    }
    while (!feof(file))
    {
        fgets (readBuffer, MAX_STRING_LENGTH, file);
        if (readBuffer[strlen(readBuffer) - 1] == '\n')
        {
            readBuffer[strlen(readBuffer) - 1] = 0;
        }
        printf ("Read Line [%s]\n", readBuffer);
        char * curString = strtok (readBuffer, STRING_SEPS);
        while (curString)
        {
            printf (" -- Token: [%s]\n", curString);
            int * value = hashMapGet(map, curString);
            if (value == NULL)
            {
                hashMapPut(map, curString, 1);
            }
            else
            {
                hashMapPut(map, curString, (*value)+1);                
            }
            curString = strtok (NULL, STRING_SEPS);
        }   
    }
    fclose (file);
    // --- Concordance code ends here ---
    
    hashMapPrint(map);
    
    timer = clock() - timer;
    printf("\nRan in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);
    printf("Empty buckets: %d\n", hashMapEmptyBuckets(map));
    printf("Number of links: %d\n", hashMapSize(map));
    printf("Number of buckets: %d\n", hashMapCapacity(map));
    printf("Table load: %f\n", hashMapTableLoad(map));
    
    hashMapDelete(map);
    return 0;
}
コード例 #3
0
ファイル: tests.c プロジェクト: ammiranda/CS261
/**
 * Tests all hash map functions after adding and removing all of the given keys
 * and values.
 * @param test
 * @param links The key-value pairs to be added and removed.
 * @param notKeys Some keys not in the table to test contains and get.
 * @param numLinks The number of key-value pairs to be added and removed.
 * @param numNotKeys The number of keys not in the table.
 * @param numBuckets The initial number of buckets (capacity) in the table.
 */
void testCase(CuTest* test, HashLink* links, const char** notKeys, int numLinks,
              int numNotKeys, int numBuckets)
{
    HashMap* map = hashMapNew(numBuckets);
    Histogram hist;
    
    // Add links
    for (int i = 0; i < numLinks; i++)
    {
        hashMapPut(map, links[i].key, links[i].value);
    }
    
    // Print table
    printf("\nAfter adding all key-value pairs:");
    hashMapPrint(map);
    
    // Check size
    CuAssertIntEquals(test, numLinks, hashMapSize(map));
    
    // Check capacity
    CuAssertIntEquals(test, map->capacity, hashMapCapacity(map));
    
    // Check empty buckets
    int sum = 0;
    for (int i = 0; i < map->capacity; i++)
    {
        if (map->table[i] == NULL)
        {
            sum++;
        }
    }
    CuAssertIntEquals(test, sum, hashMapEmptyBuckets(map));
    
    // Check table load
    CuAssertIntEquals(test, (float)numLinks / map->capacity, hashMapTableLoad(map));
    
    // Check contains and get on valid keys.
    for (int i = 0; i < numLinks; i++)
    {
        CuAssertIntEquals(test, 1, hashMapContainsKey(map, links[i].key));
        int* value = hashMapGet(map, links[i].key);
        CuAssertPtrNotNull(test, value);
        CuAssertIntEquals(test, links[i].value, *value);
    }
    
    // Check contains and get on invalid keys.
    for (int i = 0; i < numNotKeys; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i]));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i]));
    }
    
    // Check that all links are present and have a unique key.
    histFromTable(&hist, map);
    CuAssertIntEquals(test, numLinks, hist.size);
    assertHistCounts(test, &hist);
    histCleanUp(&hist);
    
    // Remove keys
    for (int i = 0; i < numLinks; i++)
    {
        hashMapRemove(map, links[i].key);
    }
    
    // Print table
    printf("\nAfter removing all key-value pairs:");
    hashMapPrint(map);
    
    // Check size
    CuAssertIntEquals(test, 0, hashMapSize(map));
    
    // Check capacity
    CuAssertIntEquals(test, map->capacity, hashMapCapacity(map));
    
    // Check empty buckets
    CuAssertIntEquals(test, map->capacity, hashMapEmptyBuckets(map));
    
    // Check table load
    CuAssertIntEquals(test, 0, hashMapTableLoad(map));
    
    // Check contains and get on valid keys.
    for (int i = 0; i < numLinks; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, links[i].key));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, links[i].key));
    }
    
    // Check contains and get on invalid keys.
    for (int i = 0; i < numNotKeys; i++)
    {
        CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i]));
        CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i]));
    }
    
    // Check that there are no links in the table.
    histFromTable(&hist, map);
    CuAssertIntEquals(test, 0, hist.size);
    assertHistCounts(test, &hist);
    histCleanUp(&hist);
    
    hashMapDelete(map);
}
コード例 #4
0
int main(int argc, const char** argv)
{
	// FIXME: implement
	const char* fileName = "dictionary.txt";
	if (argc > 1)
	{
		fileName = argv[1];
	}
	printf("Opening file: %s\n", fileName);

	HashMap* map = hashMapNew(10);

	// --- spellcheck code begins here ---
	/* open file */
	FILE *fp = fopen(fileName, "r");
	assert(fp != NULL);

	/* get the word */
	char *word = nextWord(fp);
	/* then loop through file */
	if (word != NULL)
	{
		do
		{
			//if already in hash map increment occurrence
			if (hashMapContainsKey(map, word))
			{
				int *i = hashMapGet(map, word);
				int count = *i;
				count++;
				hashMapPut(map, word, count);
			}
			//else put word in hash map with count of 1
			else
			{
				hashMapPut(map, word, 1);
			}

			//update word
			free(word);
			word = NULL;
			word = nextWord(fp);
		} while (word);
	}

	//now close file
	fclose(fp);

	//now check for user input
	int choice = 1;
	do {
		char uInp[256];
		printf("Enter a word: ");
		scanf("%s", uInp);

		for (int i = 0; uInp[i]; i++)
		{
			uInp[i] = tolower(uInp[i]);
		}

		if (hashMapContainsKey(map, uInp))
		{
			printf("You spelled it right! :) \n");
		}
		else
		{
			printf("I cannot find that word, are you sure you spelled it correctly? \n");
		}

		printf("Check another word? 1-yes, 0-no \n");
		scanf("%d", &choice);

		if (choice == 0)
		{
			printf("Goodbye!\n");
		}

	} while (choice);

	// Be sure to free the word after you are done with it here.
	// --- spellchecker ends here ---

	hashMapDelete(map);
	return 0;
}