Advertisement
Guest User

Untitled

a guest
May 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. /*********************************
  2. * Class: MAGSHIMIM C2 *
  3. * Week: 6 *
  4. * Name: Yakov Shuhmacher *
  5. * Credits: *
  6. **********************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define STR_LEN 50
  12. #define ADD_PRO 1
  13. #define ADD_CON 2
  14. #define PRINT_REASONS 3
  15. #define EXIT 4
  16.  
  17. typedef struct list
  18. {
  19. char* listName;
  20. int numReasons;
  21. char** reasons;
  22. } list;
  23.  
  24. void addReason(list* proList);
  25. void printReasons(list* proList, list* conList, char* dillema);
  26. int main(void)
  27. {
  28. int i = 0;
  29. int option = 0;
  30. char proName[STR_LEN] = "PRO";
  31. char conName[STR_LEN] = "CON";
  32. list proList = { proName, 0, (char*)malloc(sizeof(char) * STR_LEN) };
  33. list conList = { conName, 0, (char*)malloc(sizeof(char) * STR_LEN) };
  34.  
  35. char dillema[STR_LEN] = { 0 };
  36.  
  37. printf("what is your dillema?\n");
  38. fgets(dillema, STR_LEN, stdin);
  39. dillema[strlen(dillema) - 1] = 0;
  40. while (EXIT != option)
  41. {
  42. printf("Choose option:\n");
  43. printf("%d - Add %s reason\n", ADD_PRO, proName);
  44. printf("%d - Add %s reason\n", ADD_CON, conName);
  45. printf("%d - Print reasons\n", PRINT_REASONS);
  46. printf("%d - Exit\n", EXIT);
  47. scanf("%d", &option);
  48. switch (option)
  49. {
  50. case ADD_PRO:
  51. addReason(&proList);
  52. break;
  53.  
  54. case ADD_CON:
  55. addReason(&conList);
  56. break;
  57. case PRINT_REASONS:
  58. printReasons(&proList, &conList, dillema);
  59. break;
  60. case EXIT:
  61. printf("Good luck!");
  62. break;
  63. default:
  64. break;
  65. }
  66.  
  67. }
  68. for (i = 0; i < proList.numReasons; i++)
  69. {
  70. free(proList.reasons[i]);
  71. }
  72. for (i = 0; i < conList.numReasons; i++)
  73. {
  74. free(conList.reasons[i]);
  75. }
  76. free(proList.reasons);
  77. free(conList.reasons);
  78.  
  79. getchar();
  80. getchar();
  81. return 0;
  82. }
  83. /*
  84. This function adds a reason
  85. Input - list* proList
  86. Output - None
  87. */
  88. void addReason(list* list)
  89. {
  90.  
  91. list->numReasons++;
  92. list->reasons = (char**)realloc(list->reasons, sizeof(char*) * list->numReasons);
  93. list->reasons[list->numReasons - 1] = (char*)malloc(sizeof(char) * STR_LEN);
  94. printf("Enter a reason to add to list %s:\n", (list->listName));
  95. fgets(list->reasons[list->numReasons - 1], STR_LEN, stdin);
  96. list->reasons[list->numReasons - 1][strlen(list->reasons[list->numReasons - 1]) - 1] = 0;
  97.  
  98. }
  99. /*
  100. This function prints the reasons
  101. Input - list* prolist, list* conList, char* dillema
  102. Output - None
  103. */
  104. void printReasons(list* proList, list* conList, char* dillema)
  105. {
  106. int i = 0;
  107. printf("Your dillema:\n");
  108. printf("%s\n", dillema);
  109.  
  110. printf("List %s\n", proList->listName);
  111. printf("---------\n");
  112. for (i = 0; i < proList->numReasons; i++)
  113. {
  114. printf("%s\n", proList->reasons[i]);
  115. }
  116. printf("\n");
  117.  
  118. printf("List %s\n", conList->listName);
  119. printf("---------\n");
  120. for (i = 0; i < conList->numReasons; i++)
  121. {
  122. printf("%s\n",conList->reasons[i]);
  123. }
  124. printf("\n");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement