Advertisement
Yonka2019

Untitled

Apr 27th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. /*********************************
  3. * Class: MAGSHIMIM C2            *
  4. * Week 5                         *
  5. * HW  solution                   *
  6. **********************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define PRO_OP 1
  13. #define CON_OP 2
  14. #define PRINT_OP 3
  15. #define EXIT_OP 4
  16.  
  17. #define STR_LEN 50
  18. #define MAX_LIST_LENGTH 10
  19.  
  20. typedef struct reasonList
  21. {
  22.     char* listName;
  23.     char* reasons[MAX_LIST_LENGTH];
  24.     int numReasons;
  25. } reasonList;
  26.  
  27.  
  28. void initList(reasonList* list, char* name);
  29. void addReason(reasonList* list);
  30. void printList(reasonList list);
  31. int menu(void);
  32. void myFgets(char str[], int n);
  33. void deleteList(reasonList list);
  34.  
  35. int main(void)
  36. {
  37.     char dillema[STR_LEN] = { 0 };
  38.     int op = 0;
  39.  
  40.     reasonList proList;
  41.     initList(&proList, "PRO");
  42.     reasonList conList;
  43.     initList(&conList, "CON");
  44.  
  45.     printf("What is your dillema?\n");
  46.     myFgets(dillema, STR_LEN);
  47.  
  48.     while (op != EXIT_OP)
  49.     {
  50.         op = menu();
  51.  
  52.         switch (op)
  53.         {
  54.         case(PRO_OP):
  55.             addReason(&proList);
  56.             break;
  57.         case(CON_OP):
  58.             addReason(&conList);
  59.             break;
  60.         case(PRINT_OP):
  61.             printf("Your dillema:\n");
  62.             printf("%s\n\n", dillema);
  63.  
  64.             printList(proList);
  65.             printList(conList);
  66.             break;
  67.         case(EXIT_OP):
  68.             deleteList(proList);
  69.             deleteList(conList);
  70.             break;
  71.         }
  72.     }
  73.     printf("Good luck!\n");
  74.     getchar();
  75.     return 0;
  76. }
  77.  
  78. /*
  79. Function will initialize a reason list
  80. input: the list to init, and its name
  81. output: none
  82. */
  83. void initList(reasonList* list, char* listName)
  84. {
  85.     list->listName = listName;
  86.     list->numReasons = 0;
  87.     list->reasons[0] = 0;
  88. }
  89.  
  90. /*
  91. Function will add a reason to the list
  92. input: the list to add to and its name
  93. output: none
  94. */
  95. void addReason(reasonList* list)
  96. {
  97.     char* input = (char*)malloc(STR_LEN * sizeof(char));
  98.  
  99.     printf("Enter a reason to add to list %s:\n", list->listName);
  100.  
  101.     myFgets(input, STR_LEN);
  102.  
  103.     list->reasons[list->numReasons] = input;
  104.     list->numReasons++;
  105. }
  106.  
  107. /*.
  108. Function will print a list of reasons
  109. input: the list
  110. output: none
  111. */
  112. void printList(reasonList list)
  113. {
  114.     int i = 0;
  115.  
  116.     printf("List %s\n"
  117.         "----------\n", list.listName);
  118.     for (i = 0; i < list.numReasons; i++)
  119.     {
  120.         printf("%s\n", list.reasons[i]);
  121.     }
  122.     printf("\n");
  123. }
  124.  
  125. /*
  126. Function shows menu and returns user's choice
  127. input: none
  128. output: user's choice
  129. */
  130. int menu(void)
  131. {
  132.     int op = 0;
  133.     printf("Choose option:\n");
  134.     printf("%d - Add PRO reason\n", PRO_OP);
  135.     printf("%d - Add CON reason\n", CON_OP);
  136.     printf("%d - Print reasons\n", PRINT_OP);
  137.     printf("%d - Exit\n", EXIT_OP);
  138.     scanf("%d", &op);
  139.     while (op < PRO_OP || op > EXIT_OP)
  140.     {
  141.         printf("Invalid option. Try again: ");
  142.         scanf("%d", &op);
  143.     }
  144.     getchar(); // clean buffer
  145.     return op;
  146. }
  147.  
  148. /*
  149. Function will delete a list
  150. input: the list to delete
  151. output: none
  152. */
  153. void deleteList(reasonList list)
  154. {
  155.     int i = 0;
  156.  
  157.     for (i = 0; i < list.numReasons; i++)
  158.     {
  159.         free(list.reasons[i]);
  160.     }
  161. }
  162.  
  163. /*
  164. Function will perform the fgets command and also remove the newline
  165. that might be at the end of the string - a known issue with fgets.
  166. input: the buffer to read into, the number of chars to read
  167. */
  168. void myFgets(char str[], int n)
  169. {
  170.     fgets(str, n, stdin);
  171.     str[strcspn(str, "\n")] = 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement