Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.13 KB | None | 0 0
  1. /***
  2. CIS 26B: Homework 0
  3. ********************
  4. NAME:
  5. IDE(compiler):
  6. *********************************************************************************************
  7. This program provides antonyms to common words. An antonym is a word with
  8. the opposite meaning. For example, the antonym of happy is sad. The program is to
  9. look for a word and if found, report its antonym. If the word is not found, the
  10. program is to display an appropriate message.
  11.  
  12. The input text file has a word and its antonym on each line, as it is shown below:
  13.  
  14. happy sad
  15. ugly  attractive
  16. nice  rude
  17. cold  warmth
  18.  
  19. Your task:
  20.  
  21. 1. Code review: read and understand this program (draw charts and diagrams)
  22. 2. Call the following functions in main (in this order):
  23. printInfo(), getWords(), printList(), searchManager(), freeList(), and printEnd ()
  24. 3. Implement the freeList() function
  25. 4. Run the program. Save its output at the end of the program as a comment.
  26. */
  27.  
  28. #define _CRT_SECURE_NO_WARNINGS
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32.  
  33.  
  34. #ifdef _MSC_VER
  35. #include <crtdbg.h>
  36.  
  37. #endif
  38.  
  39. const char FILENAME[] = "antonyms_0.txt";
  40. //const char FILENAME[] = "antonyms_1.txt";
  41. //const char FILENAME[] = "antonyms_2.txt";
  42.  
  43. typedef struct
  44. {
  45.     char *word;     // a pointer to a dynamically allocated string
  46.     char *antonym;  // a pointer to its antonym
  47. } PAIR;
  48.  
  49. typedef struct
  50. {
  51.     int size;     // the number of words in the list
  52.     int max_len;  // the length of the longest word
  53.     PAIR *pair;     // a pointer to a dynamically allocated array
  54. } LIST;
  55.  
  56. void printInfo(void);
  57. void printEnd(void);
  58.  
  59. LIST *createList(int num);
  60. LIST *getWords(const char fileName[]);
  61. char *allocateString(char *inString);
  62. void  readPair(FILE *fpWord, PAIR *pair, PAIR *revPair);
  63. void  insertPair(LIST *list, PAIR pair);
  64.  
  65. void printList(LIST *list);
  66. void printLine(int max);
  67. void searchManager(LIST *list);
  68. char *binSearch(LIST *list, char *word);
  69. LIST *freeList(LIST *list);
  70.  
  71. int main(void)
  72. {
  73.     LIST *list;
  74.  
  75.     // call printInfo()
  76.     printInfo();
  77.     // call getWords()
  78.     LIST* words;
  79.     words = getWords(FILENAME);
  80.     // call printList()
  81.     printList(words);
  82.     // call searchManager()
  83.     searchManager(words);
  84.     // call freeList()
  85.     //words = freeList(words);
  86.     // call printEnd ()
  87.     printEnd();
  88.  
  89. #ifdef _MSC_VER
  90.     printf(_CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
  91. #endif
  92.     return 0;
  93.  
  94. }
  95. /********************************************************
  96. Prints information about the project.
  97. PRE  : nothing
  98. POST : info printed
  99. */
  100. void printInfo(void)
  101. {
  102.     printf("\n\n\t\tArrays, Strings, Structures, Sorting, Pointers, and "
  103.         "\n\n\t\t\t\tDynamic Allocation of Memory\n\n");
  104.     printf("This program provides antonyms to common words.\n");
  105.     putchar('\n');
  106. }
  107.  
  108. /********************************************************
  109. Prints a farewell message.
  110. PRE  : nothing.
  111. POST : farewell message printed
  112. */
  113. void printEnd(void)
  114. {
  115.     printf("\n\t\tThank you for using the program,"
  116.         "\n\t\tHave a great day!\n");
  117. }
  118.  
  119. /********************************************************
  120. Allocates the header of the list and the list of pointers
  121. to words and their antonyms.
  122. Pre:  num - number of pairs
  123. Post: list - empty
  124. */
  125. LIST *createList(int num)
  126. {
  127.     LIST *list;
  128.  
  129.     // allocate the header of the list
  130.     list = (LIST *)malloc(sizeof(LIST));
  131.     if (!list)
  132.         printf("Error allocating the header of the list!\n"), exit(102);
  133.  
  134.     // allocate the list of pointers to words and their antonyms + 1 for an empty word
  135.     // to be placed in the beginning of the list to make insert simpler: it acts as
  136.     // a sentinel value
  137.     list->pair = (PAIR *)calloc(2 * num + 1, sizeof(PAIR));
  138.     if (!list)
  139.         printf("Error allocating the list of words and their antonyms!\n"), exit(103);
  140.  
  141.     list->pair->word = allocateString(""); // an empty word
  142.     list->pair->antonym = list->pair->word;
  143.  
  144.     list->size = 1;     // there is one word in the list: the empty word
  145.     list->max_len = 0;
  146.  
  147.     return list;
  148. }
  149.  
  150. /********************************************************
  151. Creates a sorted list of structures containing pointers
  152. to words and their antonyms.
  153. Pre:  fileName - the name of the input file
  154. Post: pointer to the list of structures
  155. */
  156. LIST *getWords(const char fileName[])
  157. {
  158.     FILE *fpWord;
  159.     LIST *list;
  160.     PAIR  pair, revPair;
  161.     int   i, num;
  162.  
  163.     // open the input file
  164.     fpWord = fopen(fileName, "r");
  165.     if (!fpWord)
  166.         printf("Error opening %s!\n", fileName), exit(101);
  167.  
  168.     // read the number of pairs
  169.     fscanf(fpWord, "%d", &num);
  170.     list = createList(num);
  171.     // read and insert pairs of words
  172.     for (i = 0; i < num; i++) {
  173.         readPair(fpWord, &pair, &revPair);
  174.         insertPair(list, pair);
  175.         insertPair(list, revPair);
  176.         // update the length of the longest word
  177.         if (strlen(pair.word) > list->max_len)
  178.             list->max_len = strlen(pair.word);
  179.         if (strlen(pair.antonym) > list->max_len)
  180.             list->max_len = strlen(pair.antonym);
  181.     }
  182.  
  183.     fclose(fpWord);
  184.     return list;
  185. }
  186.  
  187. /********************************************************
  188. Creates a dynamically allocated string with the same contents
  189. as the input string
  190. Pre:  inString - input string
  191. Post: outString - dynamically allocated
  192. */
  193. char *allocateString(char *inString)
  194. {
  195.     char *outString;
  196.     int   stringSize;
  197.  
  198.     stringSize = strlen(inString) + 1;
  199.     outString = (char *)calloc(stringSize, sizeof(char));
  200.     if (outString == NULL)
  201.         printf("ERROR, not enough memory!!!\a\n"), exit(103);
  202.     strcpy(outString, inString);
  203.  
  204.     return outString;
  205. }
  206.  
  207. /********************************************************
  208. Reads a pair of words and prepares them for insetions
  209. Pre:  fpWord
  210. Post: pair, revPair
  211. */
  212. void  readPair(FILE *fpWord, PAIR *pair, PAIR *revPair)
  213. {
  214.     char tempWord[100], tempAntonym[100];
  215.  
  216.     fscanf(fpWord, "%s %s", tempWord, tempAntonym);
  217.     pair->word = allocateString(tempWord);
  218.     pair->antonym = allocateString(tempAntonym);
  219.     revPair->word = pair->antonym;
  220.     revPair->antonym = pair->word;
  221. }
  222.  
  223. /********************************************************
  224. Inserts an element into a sorded list (like in insertion sort)
  225. Pre:  list, pair
  226. Post: list updated
  227. */
  228. void  insertPair(LIST *list, PAIR pair)
  229. {
  230.     int curr = list->size;
  231.     int i = curr - 1;
  232.  
  233.     while (i >= 0 && strcmp(pair.word, list->pair[i].word) < 0) {
  234.         list->pair[i + 1] = list->pair[i];  // shift to make room for the new element
  235.         i--;
  236.     }
  237.     list->pair[i + 1] = pair; // location found: insert the new element into the list
  238.     list->size++;           // update size
  239. }
  240.  
  241. /********************************************************
  242. This function prints the of words and their antonyms
  243. in a variable format
  244. */
  245. void printList(LIST *list)
  246. {
  247.     int i;
  248.     char fmt[20];  // the format string
  249.  
  250.     printLine(list->max_len);
  251.     for (i = 0; i < list->max_len - 4; i++)
  252.         printf(" ");
  253.     printf("word  antonym\n");
  254.     printLine(list->max_len);
  255.  
  256.     sprintf(fmt, "%%%ds  %%-%ds\n", list->max_len, list->max_len); // create the format string
  257.     for (i = 1; i < list->size-1; i++)// print list: skip the empty word
  258. //        printf("%d", i);
  259.         printf(fmt, list->pair[i].word, list->pair[i].antonym);
  260.  
  261.     printLine(list->max_len);
  262. }
  263.  
  264. /********************************************************
  265. This function prints a line of "=" for the output's
  266. header
  267. */
  268. void printLine(int max)
  269. {
  270.     int i;
  271.  
  272.     for (i = 0; i < max; i++)
  273.         printf("=");
  274.     printf("  ");
  275.     for (i = 0; i < max; i++)
  276.         printf("=");
  277.     printf("\n");
  278. }
  279.  
  280. /********************************************************
  281. This function will prompt user to enter an word in order
  282. to let the program find its antonym.
  283. Pre:  the pointer list
  284. Post: nothing
  285. */
  286. void searchManager(LIST *list)
  287. {
  288.     char targetWord[100];
  289.     char quitWord[5] = "quit", quitWord1[5] = "QUIT";
  290.     char *antonym;
  291.  
  292.     printf("\nPlease enter a word or \"quit\" to stop searching: ");
  293.     scanf("%s", targetWord);
  294.     while (strcmp(quitWord, targetWord) != 0 && strcmp(quitWord1, targetWord) != 0)
  295.     {
  296.         antonym = binSearch(list, targetWord);
  297.         if (antonym)
  298.             printf("An antonym for \"%s\" is: \"%s\"\n", targetWord, antonym);
  299.         else
  300.             printf("\"%s\" not found\n", targetWord);
  301.         printf("\nPlease enter a word or \"quit\" to stop searching: ");
  302.         scanf("%s", targetWord);
  303.     }
  304. }
  305.  
  306. /********************************************************
  307. This function will search the list to check if the word
  308. user enter is in the list using binary search
  309. Pre:  list - sorted list of words and antonyms
  310. word - word whose antonym to search for
  311. Return: word's antonym if found, otherwise NULL
  312. */
  313. char *binSearch(LIST *list, char *word)
  314. {
  315.     int lo = 0, hi = list->size-1, mid = 0;
  316.  
  317.     while (lo <= hi) {
  318.         mid = (lo + hi) / 2;
  319.         int cmp = strcmp(word, list->pair[mid].word);
  320.         if (cmp == 0)
  321.             lo = hi + 1;
  322.         else if (cmp < 0)
  323.             hi = mid - 1;
  324.         else
  325.             lo = mid + 1;
  326.     }
  327.     PAIR *pair = &list->pair[mid];
  328.  
  329.     return strcmp(word, pair->word) == 0 ? pair->antonym : NULL;
  330. }
  331.  
  332. /********************************************************
  333. This function will free the list which has been allocated
  334. before.
  335. Pre:  the pointer list
  336. Post: nothing
  337. */
  338.  
  339. LIST *freeList(LIST *list)
  340. {
  341.      int size = list->size;
  342.      for (int i = 0; i < size; i++) {
  343.        free(list->pair[i].word);
  344.      }
  345.     free(list->pair);
  346.     free(list);
  347.     return NULL;
  348. }
  349.  
  350. /************ Output:
  351.  
  352.  
  353. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement