Code:
/*********************************************************************************
** CIS 15BG Fall, 2011
***************
**
** Homework 6:
** 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:
** // <--
**
** Date // <-- 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;
int max_len; // the 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 printList (LIST *list, int num);
int findMaxSize (LIST* list, char* temp1, char* temp2, char** longest, PAIR pair);
void printInfo (void);
void printEnd (void);
int main (void)
{
LIST *list;
int i;
printInfo ();
list = getWords(FILENAME);
printf( _CrtDumpMemoryLeaks() ? "\nSorry, Memory Leak\n" : "\nCongratulations! No Memory Leak\n");
printEnd ();
system("pause");
return 0;
}// main
/* =================================================================================
STUDENT - write your code below
================================================================================= */
/* =================================================================================
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;
char *temp1, *temp2, *longest;
// open the input file
fpWord = fopen (fileName, "r");
if (!fpWord)
printf ("Error opening %s!\n", fileName), exit (101);
else
printf("\t%s was read succesfully!\n", fileName);
// read the number of pairs
fscanf(fpWord, "%d", &num);
printf("\tthere are %d pairs\n\n", num);
list = createList(num);
// read the first pair
readPair(fpWord, &pair, &revPair);
// 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
temp1 = pair.word;
temp2 = pair.antonym;
// read and insert the rest of the pairs
for( i = 1; i < num; i++ ){
readPair(fpWord, &pair, &revPair);
insertPair(list, pair);
insertPair(list, revPair);
list->max_len = findMaxSize(list, temp1, temp2, &longest, pair);
}
printList(list, num);
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
void printList (LIST* list, int num)
{
int i;
for(i = 0; i < num; i++)
printf("\t%s %s\n", list->pair[i]);
return;
} //printList
/* ============================================================
Finds length of longest string from file
Pre: inString - input string
Post: outString - dynamically allocated
*/
int findMaxSize (LIST* list, char* temp1, char* temp2, char** longest, PAIR pair)
{
int maxsize;
if((strlen(pair.word) > strlen(temp1)))
strcpy(temp1, pair.word);
if((strlen(pair.antonym) > strlen(temp2)))
strcpy(temp2, pair.antonym);
if(strlen(temp1) > strlen(temp2))
{
maxsize = strlen(temp1);
*longest = temp1;
}
else
{
maxsize = strlen(temp2);
*longest = temp2;
}
return maxsize;
} //findMaxSize
The function that messes up the data is findMaxSize. It does what it's supposed to do, but I need to keep the data the way it is.