Last active
December 15, 2015 03:09
-
-
Save chrisportela/5191877 to your computer and use it in GitHub Desktop.
UCF HSPT 2012 - Zombies.c ------------ [http://hspt.ucfprogrammingteam.org/hsptFiles/problemSet2012.pdf]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct Dictionary { | |
void * next; | |
char word[1]; | |
} DICT, *PDICT; | |
// Declare function prototype | |
void freeDictionary(char **array, int items); | |
int main() | |
{ | |
int scenarios = 0, dictionaryCount = 0, utteranceCount = 0, correctionCount = 0, i = 0, j = 0; | |
char **dictionary, **utterance, **corrections; | |
FILE* inFile; | |
inFile = fopen("Zombie.in","r"); | |
if (inFile == NULL) | |
{ | |
// print some kind of error | |
return -1; | |
} | |
fscanf(inFile," %d ", &scenarios);//Get # of scenarios | |
for(i = 0; i < scenarios; i++) | |
{ | |
printf("Scenario #%d:\n",i+1); | |
//Create Dictionary of words | |
fscanf(inFile, " %d ", &dictionaryCount); | |
dictionary = (char **)calloc(dictionaryCount, sizeof(char *)); | |
for (j = 0; j < dictionaryCount; j++) | |
{ | |
char word[40]; // = (char *)malloc(sizeof(char[40])); | |
dictionary[j] = (char *)malloc(4040); | |
fscanf(inFile,"%s",word); | |
strcpy(dictionary[j], word); | |
} | |
//Create list of Utterances | |
fscanf(inFile, " %d ", &utteranceCount); | |
utterance = (char **)calloc(utteranceCount, sizeof(char *)); | |
for (j = 0; j < utteranceCount; j++) | |
{ | |
char word[40]; // = (char *)malloc(sizeof(120); | |
utterance[j] = (char *)malloc(120); | |
fscanf(inFile,"%s",word); | |
strcpy(utterance[j], word); | |
} | |
//Create list of 'corrected' words | |
correctionCount = dictionaryCount; | |
corrections = (char **)calloc(correctionCount, sizeof(char *)); | |
for (j = 0; j < correctionCount; j++) | |
{ | |
corrections[j] = (char *)malloc(40); | |
} | |
/* | |
for( char c in u[1]) | |
c in D[1]is true | |
if all true put in coorections c[x] | |
for(char c in D[1]) | |
c in U[1] true | |
else remove D[1] from c[1] | |
# c[] is corrections, d[] is dictionary, u[] is utterances # | |
*/ | |
correctionCount = 0; //reset for now, don't know if I should have done this... | |
//Get rid of all words that do not contain letters required for utterance | |
for (i = 0; i < utteranceCount; i++) | |
{ | |
char * location; | |
int k = 0, isCorrection = 1; | |
utterance[i]; // Got the utterance string | |
for (j = 0; j < dictionaryCount; j++) | |
{ | |
dictionary[j]; // Got the dictionary string | |
for (k = 0; k < strlen(utterance[i]); k++) | |
{ | |
if(isCorrection) // initial at 1, reset to 0, will be 1 at end if is correction or stop before it ends | |
{ | |
isCorrection = 0; | |
utterance[i][k];//Each char in an utterance | |
location = strchr(dictionary[j],utterance[i][k]); | |
while(location != NULL) | |
{ | |
isCorrection = 1; | |
location = strchr(location+1, utterance[i][k]); | |
} | |
} | |
} | |
for (k = 0; k < strlen(utterance[i]); k++) | |
{ | |
if(isCorrection) // initial at 1, reset to 0, will be 1 at end if is correction or stop before it ends | |
{ | |
isCorrection = 0; | |
utterance[i][k];//Each char in an utterance | |
location = strchr(dictionary[j],utterance[i][k]); | |
while(location != NULL) | |
{ | |
isCorrection = 1; | |
location = strchr(location+1, utterance[i][k]); | |
} | |
} | |
} | |
//add to corrections if isCorrection == 1 | |
if(isCorrection) | |
{ | |
correctionCount++; | |
corrections[correctionCount-1] = dictionary[j]; | |
} | |
isCorrection = 1; | |
} | |
if(correctionCount > 0) | |
{ | |
printf("Did you mean:\n"); | |
for (j = 0; j < correctionCount; j ++) | |
{ | |
printf("%s?\n",corrections[j]); | |
} | |
printf("\n"); | |
} | |
else | |
{ | |
printf("No matches found.\n\n"); | |
} | |
correctionCount = 0; | |
} | |
//Release Memory | |
freeDictionary(dictionary, dictionaryCount); | |
freeDictionary(utterance, utteranceCount); | |
} | |
} | |
void freeDictionary(char **array, int items) | |
{ | |
int i = 0; | |
for (i = 0; i <items; i++) | |
{ | |
free(array[i]); | |
} | |
memset(array, 0x0, sizeof(char *) * items); | |
free(array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment