petclub.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* petclub.c -- use a binary search tree */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "tree.h"
  6. char menu(void);
  7. void addpet(Tree * pt);
  8. void droppet(Tree * pt);
  9. void showpets(const Tree * pt);
  10. void findpet(const Tree * pt);
  11. void printitem(Item item);
  12. void uppercase(char * str);
  13. char * s_gets(char * st, int n);
  14. int main(void)
  15. {
  16. Tree pets;
  17. char choice;
  18. InitializeTree(&pets);
  19. while ((choice = menu()) != 'q')
  20. {
  21. switch (choice)
  22. {
  23. case 'a' : addpet(&pets);
  24. break;
  25. case 'l' : showpets(&pets);
  26. break;
  27. case 'f' : findpet(&pets);
  28. break;
  29. case 'n' : printf("%d pets in club\n",
  30. TreeItemCount(&pets));
  31. break;
  32. case 'd' : droppet(&pets);
  33. break;
  34. default : puts("Switching error");
  35. }
  36. }
  37. DeleteAll(&pets);
  38. puts("Bye.");
  39. return 0;
  40. }
  41. char menu(void)
  42. {
  43. int ch;
  44. puts("Nerfville Pet Club Membership Program");
  45. puts("Enter the letter corresponding to your choice:");
  46. puts("a) add a pet l) show list of pets");
  47. puts("n) number of pets f) find pets");
  48. puts("d) delete a pet q) quit");
  49. while ((ch = getchar()) != EOF)
  50. {
  51. while (getchar() != '\n') /* discard rest of line */
  52. continue;
  53. ch = tolower(ch);
  54. if (strchr("alrfndq",ch) == NULL)
  55. puts("Please enter an a, l, f, n, d, or q:");
  56. else
  57. break;
  58. }
  59. if (ch == EOF) /* make EOF cause program to quit */
  60. ch = 'q';
  61. return ch;
  62. }
  63. void addpet(Tree * pt)
  64. {
  65. Item temp;
  66. if (TreeIsFull(pt))
  67. puts("No room in the club!");
  68. else
  69. {
  70. puts("Please enter name of pet:");
  71. s_gets(temp.petname,SLEN);
  72. puts("Please enter pet kind:");
  73. s_gets(temp.petkind,SLEN);
  74. uppercase(temp.petname);
  75. uppercase(temp.petkind);
  76. AddItem(&temp, pt);
  77. }
  78. }
  79. void showpets(const Tree * pt)
  80. {
  81. if (TreeIsEmpty(pt))
  82. puts("No entries!");
  83. else
  84. Traverse(pt, printitem);
  85. }
  86. void printitem(Item item)
  87. {
  88. printf("Pet: %-19s Kind: %-19s\n", item.petname,
  89. item.petkind);
  90. }
  91. void findpet(const Tree * pt)
  92. {
  93. Item temp;
  94. if (TreeIsEmpty(pt))
  95. {
  96. puts("No entries!");
  97. return; /* quit function if tree is empty */
  98. }
  99. puts("Please enter name of pet you wish to find:");
  100. s_gets(temp.petname, SLEN);
  101. puts("Please enter pet kind:");
  102. s_gets(temp.petkind, SLEN);
  103. uppercase(temp.petname);
  104. uppercase(temp.petkind);
  105. printf("%s the %s ", temp.petname, temp.petkind);
  106. if (InTree(&temp, pt))
  107. printf("is a member.\n");
  108. else
  109. printf("is not a member.\n");
  110. }
  111. void droppet(Tree * pt)
  112. {
  113. Item temp;
  114. if (TreeIsEmpty(pt))
  115. {
  116. puts("No entries!");
  117. return; /* quit function if tree is empty */
  118. }
  119. puts("Please enter name of pet you wish to delete:");
  120. s_gets(temp.petname, SLEN);
  121. puts("Please enter pet kind:");
  122. s_gets(temp.petkind, SLEN);
  123. uppercase(temp.petname);
  124. uppercase(temp.petkind);
  125. printf("%s the %s ", temp.petname, temp.petkind);
  126. if (DeleteItem(&temp, pt))
  127. printf("is dropped from the club.\n");
  128. else
  129. printf("is not a member.\n");
  130. }
  131. void uppercase(char * str)
  132. {
  133. while (*str)
  134. {
  135. *str = toupper(*str);
  136. str++;
  137. }
  138. }
  139. char * s_gets(char * st, int n)
  140. {
  141. char * ret_val;
  142. char * find;
  143. ret_val = fgets(st, n, stdin);
  144. if (ret_val)
  145. {
  146. find = strchr(st, '\n'); // look for newline
  147. if (find) // if the address is not NULL,
  148. *find = '\0'; // place a null character there
  149. else
  150. while (getchar() != '\n')
  151. continue; // dispose of rest of line
  152. }
  153. return ret_val;
  154. }