Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************
- * Class: MAGSHIMIM C2 *
- * Week: 6 *
- * Name: Yakov Shuhmacher *
- * Credits: *
- **********************************/
- #include <stdio.h>
- #include <string.h>
- #define STR_LEN 50
- #define ADD_PRO 1
- #define ADD_CON 2
- #define PRINT_REASONS 3
- #define EXIT 4
- typedef struct list
- {
- char* listName;
- int numReasons;
- char** reasons;
- } list;
- void addReason(list* proList);
- void printReasons(list* proList, list* conList, char* dillema);
- int main(void)
- {
- int i = 0;
- int option = 0;
- char proName[STR_LEN] = "PRO";
- char conName[STR_LEN] = "CON";
- list proList = { proName, 0, (char*)malloc(sizeof(char) * STR_LEN) };
- list conList = { conName, 0, (char*)malloc(sizeof(char) * STR_LEN) };
- char dillema[STR_LEN] = { 0 };
- printf("what is your dillema?\n");
- fgets(dillema, STR_LEN, stdin);
- dillema[strlen(dillema) - 1] = 0;
- while (EXIT != option)
- {
- printf("Choose option:\n");
- printf("%d - Add %s reason\n", ADD_PRO, proName);
- printf("%d - Add %s reason\n", ADD_CON, conName);
- printf("%d - Print reasons\n", PRINT_REASONS);
- printf("%d - Exit\n", EXIT);
- scanf("%d", &option);
- switch (option)
- {
- case ADD_PRO:
- addReason(&proList);
- break;
- case ADD_CON:
- addReason(&conList);
- break;
- case PRINT_REASONS:
- printReasons(&proList, &conList, dillema);
- break;
- case EXIT:
- printf("Good luck!");
- break;
- default:
- break;
- }
- }
- for (i = 0; i < proList.numReasons; i++)
- {
- free(proList.reasons[i]);
- }
- for (i = 0; i < conList.numReasons; i++)
- {
- free(conList.reasons[i]);
- }
- free(proList.reasons);
- free(conList.reasons);
- getchar();
- getchar();
- return 0;
- }
- /*
- This function adds a reason
- Input - list* proList
- Output - None
- */
- void addReason(list* list)
- {
- list->numReasons++;
- list->reasons = (char**)realloc(list->reasons, sizeof(char*) * list->numReasons);
- list->reasons[list->numReasons - 1] = (char*)malloc(sizeof(char) * STR_LEN);
- printf("Enter a reason to add to list %s:\n", (list->listName));
- fgets(list->reasons[list->numReasons - 1], STR_LEN, stdin);
- list->reasons[list->numReasons - 1][strlen(list->reasons[list->numReasons - 1]) - 1] = 0;
- }
- /*
- This function prints the reasons
- Input - list* prolist, list* conList, char* dillema
- Output - None
- */
- void printReasons(list* proList, list* conList, char* dillema)
- {
- int i = 0;
- printf("Your dillema:\n");
- printf("%s\n", dillema);
- printf("List %s\n", proList->listName);
- printf("---------\n");
- for (i = 0; i < proList->numReasons; i++)
- {
- printf("%s\n", proList->reasons[i]);
- }
- printf("\n");
- printf("List %s\n", conList->listName);
- printf("---------\n");
- for (i = 0; i < conList->numReasons; i++)
- {
- printf("%s\n",conList->reasons[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement