Code:
** Pointers, Structures, Arrays, Strings, and Dynamic Allocation of Memory
**
**********************************************************************************
This program provides antonyms to common words. An antonym is a word with
the opposite meaning. For example, the antonym of happy is sad. The program is to
look for a word and if found, report its antonym. If the word is not found, the
program is to display an appropriate message.
The input text file has a word and its antonym on each line, as it is shown below:
happy sad
ugly attractive
nice rude
cold warmth
****************************************
**
** Written By:
** // <-- write your name here
**
** Date: 6/12/2012 // <-- write the date here
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <crtdbg.h>
#define FILENAME "antonyms_0.txt"
typedef struct
{
char *word;
char *antonym;
} PAIR;
typedef struct
{
int size; // number of words
int len; // length of the longest word
PAIR *pair;
} LIST;
LIST *createList(int num);
LIST *getWords( char *fileName );
char *allocateString( char *inString );
void readPair(FILE *fpWord, PAIR *pair, PAIR *revPair);
void insertPair(LIST *list, PAIR pair);
void printTable(LIST *list);
int binarySearch(LIST *list, char *target);
char searchManager(LIST *list);
void freeMemory(LIST *list);
void printInfo (void);
void printEnd (void);
int main (void)
{
LIST *list;
PAIR *pair;
printInfo ();
list = getWords(FILENAME);
printTable(list);
searchManager(list);
freeMemory(list);
// printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
printEnd ();
return 0;
}// main
/* =================================================================================
STUDENT - write your code below
================================================================================= */
void printTable(LIST *list)
{
int i;
char fmt[20];
int max = (list->len) -1;
for(i = 0; i <= max; i++)
{
printf("=");
}
printf(" ");
for(i = 0; i <= max; i++)
{
printf("=");
}
printf("\n%10s %-10s\n", "Word", "Antonym");//formatting
for(i = 0; i <= max; i++)
{
printf("=");
}
printf(" ");
for(i = 0; i <= max; i++)
{
printf("=");
}
printf("\n");
for(i = 0; i < list->size; i++)
{
printf("%10s %-10s\n", list-> pair[i]);//formatting
}
for(i = 0; i <= max; i++)
{
printf("=");
}
printf(" ");
for(i = 0; i <= max; i++)
{
printf("=");
}
printf("\n");
return;
}
int binarySearch(LIST *list, char *target)
{
int first, mid, last;
int result;
int locn = -1;
first = 0;
last = (list->size) -1;
mid = (list->size) / 2;
while(first <= last && locn == -1 )
{
mid = (first + last) / 2;
result = strcmp(list ->pair[mid].word, target);
if(result > 0){
last = mid - 1;
}
else if( result < 0){
first = mid + 1;
}
else return mid;
}
return -1;
}
char searchManager(LIST *list)
{
char terminate[5] = "QUIT";
int quit = 1;
char *target;
int locn;
if(!(target = malloc(30 * sizeof(char)))){
printf("Not enough memory available!\n");
exit(100);
}
do
{
printf("Which word would you like to search for? (Enter 'QUIT' to quit)\n");
scanf("%s", target);
quit = strcmp(target, terminate) == 0;
if(!quit)
{
locn = binarySearch(list, target);
if(locn != -1)
{
printf("Your word: %s\nIt's antonym: %s\n", list->pair[locn]);
}
else
printf("Your word was not found.\n");
}
}
while(!quit);
}
void freeMemory(LIST *list)
{
int i;
for(i = 0; i < list->size; i++){
free(list->pair[i].word);
free(list->pair[i].antonym);
}
free(list);
return;
}
/* =================================================================================
INSTRUCTOR
================================================================================= */
/* ============================================================
Prints information about the project.
PRE : nothing
POST : info printed
*/
void printInfo (void)
{
printf("\n\n\t\tPointers, \n\n\t\tStructures, \n\n\t\tArrays, "
"\n\n\t\tStrings, and\n\n\t\tDynamic Allocation of Memory\n\n");
printf("\tThis program provides antonyms to common words.\n");
putchar('\n');
return;
} // printInfo
/* ============================================================
Prints a farewell message.
PRE : nothing.
POST : farewell message printed
*/
void printEnd (void)
{
printf("\n\t\tThank you for using the program,"
"\n\t\tHave a great day!\n");
return;
} // printEnd
/* ============================================================
Creates a sorted list of structures containing pointers
to words and their antonyms.
Pre: fileName - the name of the input file
Post: pointer to the list of structures
*/
LIST *getWords( char *fileName )
{
FILE *fpWord;
LIST *list;
PAIR pair, revPair;
int i, num;
int len;
// open the input file
fpWord = fopen (fileName, "r");
if (!fpWord)
printf ("Error opening %s!\n", fileName), exit (101);
// read the number of pairs
fscanf(fpWord, "%d", &num);
list = createList(num);
// read the first pair
readPair(fpWord, &pair, &revPair);
len = strlen(pair.word);
if(len > list-> len)
list-> len = len;
len = strlen(pair.antonym);
if(len > list-> len)
list-> len = len;
// insert the first pair
if( strcmp(pair.word, pair.antonym) < 0 ){
list->pair[0] = pair;
list->pair[1] = revPair;
}
else{
list->pair[0] = revPair;
list->pair[1] = pair;
}
list->size = 2; // the first pair inserted: list has two words
for( i = 1; i < num; i++ ){
//find the length of the longest word
len = strlen(pair.word);
if(len > list-> len)
list-> len = len;
len = strlen(pair.antonym);
if(len > list-> len)
list-> len = len;
// read and insert the rest of the pairs
readPair(fpWord, &pair, &revPair);
insertPair(list, pair);
insertPair(list, revPair);
}
fclose(fpWord);
return list;
} // getWords
/* ============================================================
Allocates the header of the list and the list of pointers
to words and their antonyms.
Pre: num - number of pairs
Post: list - empty
*/
LIST *createList(int num)
{
LIST *list;
// allocate the header of the list
list = (LIST *)malloc(sizeof(LIST));
if (!list)
printf ("Error allocating the header of the list!\n"), exit (102);
// allocate the list of pointers to words and their antonyms
list->pair = (PAIR *) calloc(2 * num, sizeof(PAIR));
if (!list)
printf ("Error allocating the list of words and their antonyms!\n"), exit (103);
list->size = 0; // empty list
return list;
}// createList
/* ============================================================
Reads a pair of words and prepares them for insetions
Pre: fpWord
Post: pair, revPair
*/
void readPair(FILE *fpWord, PAIR *pair, PAIR *revPair)
{
char tempWord [100];
char tempAntonym [100];
fscanf(fpWord, "%s %s", tempWord, tempAntonym);
pair->word = allocateString(tempWord);
pair->antonym = allocateString(tempAntonym);
revPair->word = pair->antonym;
revPair->antonym = pair->word;
return;
}// readPair
/* ============================================================
Inserts a word into a sorded list of words
Pre: list word
Post: list updated
*/
void insertPair(LIST *list, PAIR pair)
{
int curr = list->size;
int i;
i = curr - 1;
while ( i >= 0 && strcmp(pair.word, list->pair[i].word) < 0 ){
list->pair[i+1] = list->pair[i];
i--;
}
list->pair[i+1] = pair;
list->size++;
return;
}// insertPair
/* ============================================================
Creates a dynamically allocated string with the same contents
as the input string
Pre: inString - input string
Post: outString - dynamically allocated
*/
char *allocateString( char *inString )
{
char *outString;
int stringSize;
stringSize = strlen( inString ) + 1;
outString = (char *) calloc (stringSize, sizeof (char));
if ( outString == NULL)
printf ("ERROR, not enough memory!!!\a\n"), exit (103);
strcpy (outString, inString);
return outString;
}// allocateString
-Ok,so this is my pretty much finished program.