Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.99 KB | None | 0 0
  1. /******************************************
  2.  * *Student name: Shaked Arbili
  3.  * *Student ID: 206230880
  4.  * *Exercise name: Exercise 6
  5.  * ******************************************/
  6. #define _CRT_SECURE_NO_WARNINGS
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12.  
  13. typedef struct
  14. {
  15.     char id[10];
  16.     char *name;
  17.     char *lastName;
  18.     char *age;
  19.     char gender;
  20.     char *userName;
  21.     char *pass;
  22.     unsigned char hobbies;
  23.     char *desc;
  24. }user;
  25. user* ptrUser;
  26.  
  27.  
  28. typedef struct womanUser
  29. {
  30.     user *womData;
  31.     struct womanUser *next;
  32.  
  33. }womanUser;
  34.  
  35. enum hobbies {BASEBALL = 1,BASKETBALL,BICYCLE,BOOKS,DRAWING,GYM,MOVIES,POETRY};
  36. enum memoryActions {MALLOC = 0,REALLOC = 1};
  37.  
  38. //declaring functions
  39. void login(user **arrMen, womanUser *womHead, int *menSize);
  40. int usernameExist(user **arrMen, womanUser *womHead, char *username, int *menSize);
  41. int idExist(user **arrMen, womanUser *womHead, char id[10],int *menSize);
  42. void loadData(user ***arrMen, womanUser **womHead,int *menSize);
  43. void freeAll(user **arrMen, womanUser *womHead,int *menSize);
  44. void freeUserPointers(user* ptrUser);
  45. void newMember(user *ptrUser, user ***arrMen, womanUser *womHead, int *menSize);
  46. void addNew(user *ptrUser, user ***arrMen, womanUser **ptrWomHead, int *menSize);
  47. void mainMenu(user *ptrUser, user **arrMen, womanUser *womHead, int *menSize);
  48. void allocateMemory(user *ptrUser,int memoAction);
  49. void findMatch(user **arrMen, womanUser *womHead, user *ptrUser, int *menSize);
  50. void deleteUser(user ***arrMen, womanUser **womHead, char id[10], char gender, int *menSize);
  51. void findHobbie(char *hobbies, char commonHobbie);
  52. void writeFile(user **arrMen, womanUser *womHead, int *menSize);
  53. int isCorrectPass(user **arrMen, womanUser *womHead, char *username, char *pass, int *menSize, user *ptrUser);
  54.  
  55. static user **arrMen = NULL;
  56. static womanUser *womHead = NULL;
  57. static int menSize = 0;
  58.  
  59. void cleanup() // vcs
  60. {
  61.     freeAll(arrMen, womHead, &menSize);
  62.     //if (ptrUser != NULL)
  63.     //{
  64.         //if (ptrUser->name != NULL)
  65.         //  freeUserPointers(ptrUser); // vcs fix exception
  66.         //free(ptrUser);
  67.         //ptrUser = NULL;
  68.     //}
  69. }
  70.  
  71. void exit1(/*user* ptrUser, */int _Code) // vcs
  72. {
  73.     cleanup();
  74.     exit(_Code);
  75. }
  76.  
  77.  
  78. /************************************************************************
  79. * function name: main *
  80. * The Input: The user should enter a number of actions he wants to do:
  81.              case 1 - login
  82.              case 2 - register as a new member
  83.              case 3 - write all the data to a file  
  84. * The output:  -*
  85. * The Function operation: This function is used as a login menu for the user *
  86. *************************************************************************/
  87. int main()
  88. {
  89.     int option;
  90.     //user **arrMen = NULL;
  91.     //int menSize = 0;
  92.     // pointer to the first element in women linked list
  93.     //womanUser *womHead = NULL;
  94.  
  95.     loadData(&arrMen, &womHead, &menSize);
  96.     printf("Welcome! please choose an option\n");
  97.     printf("1 - Log in\n");
  98.     printf("2 - New member\n");
  99.     printf("3 - Exit\n");
  100.     scanf("%d", &option);
  101.     while(option)
  102.     {
  103.         switch (option)
  104.         {
  105.         case 1:
  106.             login(arrMen, womHead, &menSize);
  107.             break;
  108.         case 2:
  109.             newMember(ptrUser, &arrMen, womHead, &menSize);
  110.             break;
  111.         case 3:
  112.             writeFile(arrMen, womHead, &menSize);
  113.             cleanup();
  114.             return 0;
  115.             break;
  116.         default:
  117.             printf("Bad choice, please try again\n");
  118.             break;
  119.  
  120.         }
  121.         printf("Welcome! please choose an option\n");
  122.         printf("1 - Log in\n");
  123.         printf("2 - New member\n");
  124.         printf("3 - Exit\n");
  125.         scanf("%d", &option);
  126.     }// end of while
  127.     return 0;
  128. }
  129.  
  130.  
  131. /************************************************************************
  132. * function name: login *
  133. * The Input: The user should enter username and password*
  134. * The output:  If user exists this function sends him to the main menu
  135. * The Function operation: The function checks if the user is exist in the system
  136. *************************************************************************/
  137. void login(user **arrMen, womanUser *womHead, int *menSize)
  138. {
  139.     char username[15], pass[11];
  140.     int exist, i;
  141.     char dummy;
  142.     womanUser *current;
  143.     scanf("%c", &dummy);
  144.     printf("Please enter your username:\n");
  145.     scanf("%s", username);
  146.     // check correctness of the username
  147.     exist = usernameExist(arrMen, womHead, username, menSize);
  148.     if (!exist)
  149.     {
  150.         // give the user second chance to enter his correct login details
  151.         printf("User do not exist in the system, please try again\n");
  152.         printf("Please enter your username:\n");
  153.         scanf("%s", username);
  154.         exist = usernameExist(arrMen, womHead, username, menSize);
  155.         if (!exist)
  156.             return;
  157.     }
  158.     printf("Please enter your password:\n");
  159.     scanf("%s", pass);
  160.     //search in women list
  161.     current = womHead;
  162.     while (current != NULL)
  163.     {
  164.         if (strcmp(current->womData->userName, current->womData->userName) == 0)
  165.             if (strcmp(current->womData->pass, pass) == 0)
  166.             {
  167.                 printf("Hello %s!\n", current->womData->name);
  168.                 mainMenu(current->womData, arrMen, womHead, menSize);
  169.                 return;
  170.             }
  171.         current = current->next;
  172.     }
  173.     if (arrMen != NULL)
  174.     {
  175.         //search in men array
  176.         for (i = 0; i < *menSize; i++)
  177.         {
  178.             if (strcmp(arrMen[i]->userName, username) == 0)
  179.                 if (strcmp(arrMen[i]->pass, pass) == 0)
  180.                 {
  181.                     printf("Hello %s!\n", arrMen[i]->name);
  182.                     mainMenu(arrMen[i], arrMen, womHead, menSize);
  183.                     return;
  184.                 }
  185.         }
  186.     }
  187.    
  188.     printf("Wrong password\n");
  189.     return;
  190. }
  191.  
  192. /************************************************************************
  193. * function name: usernameExist *
  194. * The Input: Ths function gets username *
  195. * The output:  Returns 1 if username exist in the system, else 0 *
  196. * The Function operation: Returns True ot False - 1 or 0 according to the user existness *
  197. *************************************************************************/
  198. int usernameExist(user **arrMen, womanUser *womHead, char *username, int *menSize)
  199. {
  200.     womanUser *current;
  201.     int i;
  202.     //search in women linked list if username is exist
  203.     current = womHead;
  204.     while (current != NULL)
  205.     {
  206.         if (strcmp(current->womData->userName,username) == 0)
  207.             return 1;
  208.         current = current->next;
  209.     }
  210.     if (arrMen != NULL)
  211.     {
  212.         //search in men array if username is exist
  213.         for (i = 0; i < *menSize; i++)
  214.         {
  215.             if (strcmp(arrMen[i]->userName,username) == 0)
  216.                 return 1;
  217.         }
  218.     }
  219.     return 0;
  220. }
  221.  
  222. /************************************************************************
  223. * function name: idExist *
  224. * The Input: Ths function gets id *
  225. * The output:  Returns 1 if id exist in the system, else 0 *
  226. * The Function operation: Returns True ot False - 1 or 0 according to the id existness *
  227. *************************************************************************/
  228. int idExist(user **arrMen, womanUser *womHead,char id[10],int *menSize)
  229. {
  230.     womanUser *current;
  231.     int i;
  232.     //search in women list
  233.     current = womHead;
  234.     while (current != NULL)
  235.     {
  236.         if (strcmp(current->womData->id,id) == 0)
  237.             return 1;
  238.         current = current->next;
  239.     }
  240.     if (arrMen != NULL)
  241.     {
  242.         //search in men array
  243.         for (i = 0; i < *menSize; i++)
  244.         {
  245.             if (strcmp(arrMen[i]->id,id) == 0)
  246.                 return 1;
  247.         }
  248.     }
  249.     return 0;
  250. }
  251.  
  252.  
  253. /************************************************************************
  254. * function name: loadData *
  255. * The Input:  The function gets a file with data about users*
  256. * The output:  - *
  257. * The Function operation: The function loads data from a file and enters it to the right structre
  258. *************************************************************************/
  259.  
  260. void loadData(user ***arrMen, womanUser **ptrWomHead, int *menSize)
  261. {
  262.     FILE *fptr;
  263.     char line[300];
  264.     int i, countLines = 0, tmpHobbie, usersSize = 0;
  265.     char *detail;
  266.     womanUser *previousPtr = NULL,*currentPtr = NULL;
  267.     currentPtr = *ptrWomHead;
  268.  
  269.     fptr = fopen("input.txt", "r");
  270.     if (fptr == NULL)
  271.         return;
  272.     while (fgets(line, 300, fptr))
  273.         usersSize++;
  274.     fclose(fptr);
  275.  
  276.     user users[100];
  277.    
  278.     fptr = fopen("input.txt", "r");
  279.     if (fptr == NULL)
  280.         return;
  281.     while (fgets(line, 300, fptr))
  282.     {
  283.         tmpHobbie = 0;
  284.         // allocate enough memory for features of user
  285.         users[countLines].name = (char *)malloc(15 * sizeof(char));
  286.         if (users[countLines].name == NULL)
  287.             exit1(1);
  288.         users[countLines].lastName = (char *)malloc(15 * sizeof(char));
  289.         if (users[countLines].lastName == NULL)
  290.             exit1(1);
  291.         users[countLines].age = (char *)malloc(3 * sizeof(char));
  292.         if (users[countLines].age == NULL)
  293.             exit1(1);
  294.         users[countLines].userName = (char *)malloc(10 * sizeof(char));
  295.         if (users[countLines].name == NULL)
  296.             exit1(1);
  297.         users[countLines].pass = (char *)malloc(15 * sizeof(char));
  298.         if (users[countLines].pass == NULL)
  299.             exit1(1);
  300.         users[countLines].desc = (char *)malloc(250 * sizeof(char));
  301.         if (users[countLines].desc == NULL)
  302.             exit1(1);
  303.    
  304.         detail = strtok(line, ";");
  305.         strcpy(users[countLines].id ,detail);
  306.         detail = strtok(NULL, ";");
  307.         strcpy(users[countLines].name,detail);
  308.         detail = strtok(NULL, ";");
  309.         strcpy(users[countLines].lastName ,detail);
  310.         detail = strtok(NULL, ";");
  311.         strcpy(users[countLines].age,detail);
  312.         detail = strtok(NULL, ";");
  313.         users[countLines].gender = *detail;
  314.         detail = strtok(NULL, ";");
  315.         strcpy(users[countLines].userName,detail);
  316.         detail = strtok(NULL, ";");
  317.         strcpy(users[countLines].pass, detail);
  318.         detail = strtok(NULL, ";");
  319.  
  320.         for (i = 0; i <= (int)strlen(detail) - 1; i += 2)
  321.         {
  322.             tmpHobbie = tmpHobbie | (1 << ((int)(detail[i] - '0') - 1));
  323.         }
  324.         users[countLines].hobbies = tmpHobbie;
  325.         detail = strtok(NULL, ";");
  326.         strcpy(users[countLines].desc ,detail);
  327.         // allocate the exact memory for each feature
  328.         users[countLines].name = (char*)realloc(users[countLines].name, strlen(users[countLines].name) * sizeof(char) + 1);
  329.         if (users[countLines].name == NULL)
  330.             exit1(1);
  331.         users[countLines].lastName = (char*)realloc(users[countLines].lastName, strlen(users[countLines].lastName) * sizeof(char) + 1);
  332.         if (users[countLines].lastName == NULL)
  333.             exit1(1);
  334.         users[countLines].age = (char*)realloc(users[countLines].age, strlen(users[countLines].age) * sizeof(char) + 1);
  335.         if (users[countLines].age == NULL)
  336.             exit1(1);
  337.         users[countLines].userName = (char*)realloc(users[countLines].userName, strlen(users[countLines].userName) * sizeof(char) + 1);
  338.         if (users[countLines].userName == NULL)
  339.             exit1(1);
  340.         users[countLines].pass = (char*)realloc(users[countLines].pass, strlen(users[countLines].pass) * sizeof(char) + 1);
  341.         if (users[countLines].pass == NULL)
  342.             exit1(1);
  343.         users[countLines].desc = (char*)realloc(users[countLines].desc, strlen(users[countLines].desc) * sizeof(char) + 1);
  344.         if (users[countLines].desc == NULL)
  345.             exit1(1);
  346.         countLines++;
  347.  
  348.     }
  349.     fclose(fptr);
  350.     // classify users to the right group and add them to the right structure
  351.     for (i = 0; i < usersSize; i++)
  352.     {
  353.         addNew(&users[i], arrMen, ptrWomHead, menSize);
  354.     }
  355. }
  356.  
  357.  
  358. /************************************************************************
  359. * function name: freeAll *
  360. * The Input: The function gets the data structues *
  361. * The output:  - *
  362. * The Function operation:  The function free all the pointers which were allocated in the program*
  363. *************************************************************************/
  364. void freeAll(user **arrMen, womanUser *womHead, int *menSize)
  365. {
  366.     womanUser *current = womHead;
  367.     womanUser *previous = current;
  368.     int i;
  369.     //free all pointers in woman list
  370.     while (current != NULL)
  371.     {
  372.         if (current->womData->name != NULL)
  373.         {
  374.             free(current->womData->name);
  375.             current->womData->name = NULL;
  376.         }
  377.         if (current->womData->lastName != NULL)
  378.         {
  379.             free(current->womData->lastName);
  380.             current->womData->lastName = NULL;
  381.         }
  382.         if (current->womData->age != NULL)
  383.         {
  384.             free(current->womData->age);
  385.             current->womData->age = NULL;
  386.         }
  387.         if (current->womData->userName != NULL)
  388.         {
  389.             free(current->womData->userName);
  390.             current->womData->userName = NULL;
  391.         }
  392.         if (current->womData->pass != NULL)
  393.         {
  394.             free(current->womData->pass);
  395.             current->womData->pass = NULL;
  396.         }
  397.         if (current->womData->desc != NULL)
  398.         {
  399.             free(current->womData->desc);
  400.             current->womData->desc = NULL;
  401.         }
  402.         previous = current;
  403.         current = current->next;
  404.         if (previous != NULL)
  405.         {
  406.             free(previous);
  407.             previous = NULL;
  408.         }
  409.     }
  410.     //free all pointers in men array
  411.     for (i = 0; i < *menSize; i++)
  412.     {
  413.         if (arrMen[i]->name != NULL)
  414.         {
  415.             free(arrMen[i]->name);
  416.             arrMen[i]->name = NULL;
  417.         }
  418.         if (arrMen[i]->lastName != NULL)
  419.         {
  420.             free(arrMen[i]->lastName);
  421.             arrMen[i]->lastName = NULL;
  422.  
  423.         }
  424.         if (arrMen[i]->age != NULL)
  425.         {
  426.             free(arrMen[i]->age);
  427.             arrMen[i]->age = NULL;
  428.  
  429.         }
  430.         if (arrMen[i]->userName != NULL)
  431.         {
  432.             free(arrMen[i]->userName);
  433.             arrMen[i]->userName = NULL;
  434.         }
  435.         if (arrMen[i]->pass != NULL)
  436.         {
  437.             free(arrMen[i]->pass);
  438.             arrMen[i]->pass = NULL;
  439.         }
  440.         if (arrMen[i]->desc != NULL)
  441.         {
  442.             free(arrMen[i]->desc);
  443.             arrMen[i]->desc = NULL;
  444.         }
  445.     }
  446.     if (arrMen != NULL)
  447.     {
  448.         free(arrMen);
  449.         arrMen = NULL;
  450.     }
  451. }
  452. /************************************************************************
  453. * function name: freeUserPointers *
  454. * The Input: The function gets a specific pointer to a user struct*
  455. * The output:  - *
  456. * The Function operation:  The function frees all the pointers of user*
  457. *************************************************************************/
  458. void freeUserPointers(user *ptrUser)
  459. {
  460.     if (ptrUser->name != NULL)
  461.     {
  462.         free(ptrUser->name);
  463.         ptrUser->name = NULL;
  464.     }
  465.     if (ptrUser->lastName != NULL)
  466.     {
  467.         free(ptrUser->lastName);
  468.         ptrUser->lastName = NULL;
  469.     }
  470.     if (ptrUser->age != NULL)
  471.     {
  472.         free(ptrUser->age);
  473.         ptrUser->age = NULL;
  474.     }
  475.     if (ptrUser->userName != NULL)
  476.     {
  477.         free(ptrUser->userName);
  478.         ptrUser->userName = NULL;
  479.     }
  480.     if (ptrUser->pass != NULL)
  481.     {
  482.         free(ptrUser->pass);
  483.         ptrUser->pass = NULL;
  484.     }
  485.     if (ptrUser->desc != NULL)
  486.     {
  487.         free(ptrUser->desc);
  488.         ptrUser->desc = NULL;
  489.     }
  490. }
  491.  
  492. /************************************************************************
  493. * function name: newMember *
  494. * The Input: The  *
  495. * The output:  - *
  496. * The Function operation:  *
  497. *************************************************************************/
  498. void newMember(user *ptrUser, user ***arrMen, womanUser *womHead, int *menSize)
  499. {
  500.     int exist;
  501.     char tmpHobbies[8], dummy;;
  502.     int i;
  503.     int hobbie;
  504.  
  505.     ptrUser = (user *)malloc(sizeof(user));
  506.     if (ptrUser == NULL)
  507.         exit1(1);
  508.     allocateMemory(ptrUser, MALLOC);
  509.     printf("Please enter your ID:\n");
  510.     scanf("%s", ptrUser->id);
  511.     //check that id doesnt exist
  512.     exist = idExist(*arrMen, womHead, ptrUser->id, menSize);
  513.     if (exist)
  514.     {
  515.         printf("Error: User already exists\n");
  516.         freeUserPointers(ptrUser);
  517.         return;
  518.     }
  519.     printf("Please enter your first name:\n");
  520.     scanf("%s", ptrUser->name);
  521.     printf("Please enter your last name:\n");
  522.     scanf("%s", ptrUser->lastName);
  523.     printf("Please enter your age (18 to 100):\n");
  524.     scanf("%s", ptrUser->age);
  525.     if (strcmp(ptrUser->age, "18") < 0 || (strcmp(ptrUser->age, "100") > 0 \
  526.         && (strlen(ptrUser->age) >= 3) || strlen(ptrUser->age) != 2 && strlen(ptrUser->age) != 3))
  527.     {
  528.         freeUserPointers(ptrUser);
  529.         return;
  530.     }
  531.     printf("Please enter your gender (F-female, M-male):\n");
  532.     scanf("%s", &ptrUser->gender);
  533.     printf("Choose a username (3-10 characters):\n");
  534.     scanf("%s", ptrUser->userName);
  535.     if (strlen(ptrUser->userName) < 3)
  536.     {
  537.         freeUserPointers(ptrUser);
  538.         return;
  539.     }
  540.     if ((ptrUser->userName[0] < 'A' || ptrUser->userName[0] > 'z') || (ptrUser->userName[0] > 'Z') \
  541.         && ptrUser->userName[0] < 'a')
  542.     {
  543.         freeUserPointers(ptrUser);
  544.         return;
  545.     }
  546.     printf("please choose 4 hobbies: Baseball=1, Basketball=2, Bicycle=3, Books=4, Drawing=5, Gym=6, Movies=7, \
  547.             Poetry=8\n");
  548.     scanf("%c", &dummy);
  549.     fgets(tmpHobbies, 8, stdin);
  550.     hobbie = 0;
  551.     for (i = 0; i < sizeof(tmpHobbies); i += 2)
  552.         hobbie = hobbie | (1 << ((int)(tmpHobbies[i] - '0') - 1));
  553.     ptrUser->hobbies = hobbie;
  554.     printf("Choose a password (attention-minimum 3 characters):\n");
  555.     scanf("%s", ptrUser->pass);
  556.     if (strlen(ptrUser->pass) < 3)
  557.     {
  558.         freeUserPointers(ptrUser);
  559.         return;
  560.     }
  561.     printf("Some words about yourself:\n");
  562.     scanf("%c", &dummy);
  563.     fgets(ptrUser->desc, 250, stdin);
  564.     allocateMemory(ptrUser, REALLOC);
  565.     addNew(ptrUser,arrMen, &womHead, menSize);
  566.    
  567.     printf("Hi %s, lets find love!\n", ptrUser->name);
  568.     mainMenu(ptrUser, *arrMen, womHead, menSize);
  569.  
  570. }
  571.  
  572. void addNew(user *ptrUser, user ***ptrArrMen, womanUser **ptrWomHead, int *menSize)
  573. {
  574.     typedef int bool;
  575.     bool inserted = 0;
  576.     womanUser *currentPtr = NULL, *previousPtr = NULL;
  577.     womanUser *newWoman;
  578.     currentPtr = *ptrWomHead;
  579.     if (ptrUser->gender == 'F')
  580.     {
  581.         if (*ptrWomHead == NULL)
  582.         {
  583.             *ptrWomHead = (womanUser *)malloc(sizeof(womanUser));
  584.             if (*ptrWomHead == NULL)
  585.                 exit1(1);
  586.             (*ptrWomHead)->womData = ptrUser;
  587.             (*ptrWomHead)->next = NULL;
  588.         }
  589.         else
  590.         {
  591.             //check where we should put this woman according to ABC order
  592.             newWoman = (womanUser*)malloc(sizeof(womanUser));
  593.             if (newWoman == NULL)
  594.                 exit1(1);
  595.             newWoman->womData = ptrUser;
  596.             while (!inserted)
  597.             {
  598.                 if (strcmp(ptrUser->lastName, currentPtr->womData->lastName) < 0)
  599.                 {
  600.                     // insert the new woman at the first place
  601.                     if (currentPtr == *ptrWomHead)
  602.                     {
  603.                         newWoman->next = *ptrWomHead;
  604.                         *ptrWomHead = newWoman;
  605.                         inserted = 1;
  606.                     }
  607.                     else
  608.                     {
  609.                         //should enter the new women between two elements
  610.                         newWoman->next = previousPtr;
  611.                         previousPtr = newWoman;
  612.                         inserted = 1;
  613.                     }
  614.                 }
  615.                 else
  616.                 {
  617.                     if (currentPtr->next == NULL)
  618.                     {
  619.                         // insert the new woman at the last place
  620.                         newWoman->womData = NULL;
  621.                         currentPtr->next = newWoman;
  622.                         inserted = 1;
  623.                     }
  624.                     else
  625.                     {
  626.                         previousPtr = currentPtr;
  627.                         currentPtr = currentPtr->next;
  628.                     }
  629.                 }
  630.             }// end of while
  631.         }
  632.        
  633.     }
  634.     else
  635.     {
  636.         (*menSize)++;
  637.         if (*ptrArrMen == NULL)
  638.         {
  639.             *ptrArrMen = (user **)malloc(*menSize * sizeof(user *));
  640.             if (ptrArrMen == NULL)
  641.                 exit1(1);
  642.         }
  643.         else
  644.         {
  645.             *ptrArrMen = (user **)realloc(*ptrArrMen, (*menSize) * sizeof(user*));
  646.             if (ptrArrMen == NULL)
  647.                 exit1(1);
  648.         }
  649.         (*ptrArrMen)[*menSize - 1] = ptrUser;
  650.     }
  651.  
  652.  
  653. }
  654. //*/
  655. /************************************************************************
  656. * function name: mainMenu*
  657. * The Input: The user should enter number of task 1/2/3 and for each case:
  658.              case 1 - 4 characters
  659.              case 2 - 2 numbers and an operator
  660.              case 3 - 4 average temperature for each season *
  661. * The output:  case 1 - returns valid or invalid output
  662.                case 2 - returns a result after an arithmetic action
  663.                case 3 - returns the weather condition during the year *
  664. * The Function operation:  *
  665. *************************************************************************/
  666. void mainMenu(user *ptrUser, user **arrMen, womanUser *womHead, int *menSize)
  667. {
  668.     int option;
  669.     printf("Please choose an option:\n");
  670.     printf("1. Find a match\n");
  671.     printf("2. I found love, DELETE me\n");
  672.     printf("3. Log out\n");
  673.     scanf("%d", &option);
  674.     while (option >= 1 && option <= 3)
  675.     {
  676.         switch (option)
  677.         {
  678.         case 1:
  679.             findMatch(arrMen, womHead, ptrUser,menSize);
  680.             break;
  681.         case 2:
  682.             deleteUser(&arrMen, &womHead, ptrUser->id, ptrUser->gender, menSize);
  683.             return;
  684.             break;
  685.         case 3:
  686.             return;
  687.             break;
  688.         default:
  689.             printf("Bad choice, please try again\n");
  690.             return;
  691.  
  692.         }
  693.         printf("Please choose an option:\n");
  694.         printf("1. Find a match\n");
  695.         printf("2. I found love, DELETE me\n");
  696.         printf("3. Log out\n");
  697.         scanf("%d", &option);
  698.     }// end of while
  699. }
  700. /************************************************************************
  701. * function name: allocateMemory *
  702. * The Input: The function gets a pointer to user struct and a memory actions we would like to do -
  703.              malloc or realloc *
  704. * The output:  - *
  705. * The Function operation: The function does malloc or realloc to ptrUser params*
  706. *************************************************************************/
  707. void allocateMemory(user *ptrUser, int memoAction)
  708. {
  709.     switch (memoAction)
  710.     {
  711.     case MALLOC:
  712.         ptrUser->name = (char *)malloc(15 * sizeof(char));
  713.         if (ptrUser->name == NULL)
  714.             exit1(1);
  715.         ptrUser->lastName = (char *)malloc(15 * sizeof(char));
  716.         if (ptrUser->lastName == NULL)
  717.             exit1(1);
  718.         ptrUser->age = (char *)malloc(3 * sizeof(char));
  719.         if (ptrUser->age == NULL)
  720.             exit1(1);
  721.         ptrUser->userName = (char *)malloc(10 * sizeof(char));
  722.         if (ptrUser->userName == NULL)
  723.             exit1(1);
  724.         ptrUser->pass = (char *)malloc(15 * sizeof(char));
  725.         if (ptrUser->pass == NULL)
  726.             exit1(1);
  727.         ptrUser->desc = (char *)malloc(250 * sizeof(char));
  728.         if (ptrUser->desc == NULL)
  729.             exit1(1);
  730.         break;
  731.     case REALLOC:
  732.         ptrUser->name = (char*)realloc(ptrUser->name, strlen(ptrUser->name) * sizeof(char) + 1);
  733.         if (ptrUser->name == NULL)
  734.             exit1(1);
  735.         ptrUser->lastName = (char*)realloc(ptrUser->lastName, strlen(ptrUser->lastName) * sizeof(char) + 1);
  736.         if (ptrUser->lastName == NULL)
  737.             exit1(1);
  738.         ptrUser->age = (char*)realloc(ptrUser->age, strlen(ptrUser->age) * sizeof(char) + 1);
  739.         if (ptrUser->age == NULL)
  740.             exit1(1);
  741.         ptrUser->userName = (char*)realloc(ptrUser->userName, strlen(ptrUser->userName) * sizeof(char) + 1);
  742.         if (ptrUser->userName == NULL)
  743.             exit1(1);
  744.         ptrUser->pass = (char*)realloc(ptrUser->pass, strlen(ptrUser->pass) * sizeof(char) + 1);
  745.         if (ptrUser->pass == NULL)
  746.             exit1(1);
  747.         ptrUser->desc = (char*)realloc(ptrUser->desc, strlen(ptrUser->desc) * sizeof(char) + 1);
  748.         if (ptrUser->desc == NULL)
  749.             exit1(1);
  750.         break;
  751.     default:
  752.         break;
  753.     }
  754. }
  755. /************************************************************************
  756. * function name: main *
  757. * The Input: The user should enter number of task 1/2/3 and for each case:
  758.              case 1 - 4 characters
  759.              case 2 - 2 numbers and an operator
  760.              case 3 - 4 average temperature for each season *
  761. * The output:  case 1 - returns valid or invalid output
  762.                case 2 - returns a result after an arithmetic action
  763.                case 3 - returns the weather condition during the year *
  764. * The Function operation: case 1 - checks validity of input according to some criterias
  765.                           case 2 - operates as a calculator
  766.                           case 3 - calculates weather average temperature *
  767. *************************************************************************/
  768. void deleteUser(user ***ptrArrMen, womanUser **ptrWomHead, char id[10], char gender, int *menSize)
  769. {
  770.     int i;
  771.     typedef int bool;
  772.     bool deleted = 0;
  773.     womanUser *current,*prev, *tmp;
  774.     if (gender == 'M')
  775.     {
  776.         //delete from men array
  777.         for (i = 0; i < *menSize; i++)
  778.         {
  779.             if (strcmp((*ptrArrMen)[i]->id, id) == 0)
  780.             {
  781.                 freeUserPointers((*ptrArrMen)[i]);
  782.                 deleted = 1;
  783.             }
  784.             // move all the other users left
  785.             if (deleted)
  786.                 (*ptrArrMen)[i] = (*ptrArrMen)[i + 1];
  787.         }
  788.         (*menSize)--;
  789.         *ptrArrMen = (user **)realloc(*ptrArrMen, (*menSize) * sizeof(*ptrArrMen));
  790.         if (*ptrArrMen == NULL)
  791.             exit1(1);
  792.     }
  793.     else
  794.     {
  795.         //delete from woman list
  796.         current = *ptrWomHead;
  797.         prev = *ptrWomHead;
  798.  
  799.         // delete the first women in the list
  800.         if (current != NULL && strcmp(current->womData->id, id) == 0)
  801.         {
  802.             tmp = (*ptrWomHead)->next;
  803.             // free old head
  804.             if (*ptrWomHead != NULL)
  805.             {
  806.                 free(*ptrWomHead);
  807.                
  808.             }
  809.             // change head
  810.             *ptrWomHead = tmp;
  811.             return;
  812.         }
  813.         while (current != NULL)
  814.         {
  815.             if (strcmp(current->womData->id,id) == 0)
  816.             {
  817.                 //item in the middle of the list
  818.                 tmp = prev->next;
  819.                 prev->next = current->next;
  820.                 freeUserPointers(tmp->womData);
  821.                 if (tmp != NULL)
  822.                 {
  823.                     free(tmp);
  824.                     tmp = NULL;
  825.                 }
  826.             }
  827.             prev = current;
  828.             current = current->next;
  829.  
  830.         }
  831.     }
  832.  
  833.  
  834. }
  835. /************************************************************************
  836. * function name: main *
  837. * The Input: The user should enter number of task 1/2/3 and for each case:
  838.              case 1 - 4 characters
  839.              case 2 - 2 numbers and an operator
  840.              case 3 - 4 average temperature for each season *
  841. * The output:  case 1 - returns valid or invalid output
  842.                case 2 - returns a result after an arithmetic action
  843.                case 3 - returns the weather condition during the year *
  844. * The Function operation: case 1 - checks validity of input according to some criterias
  845.                           case 2 - operates as a calculator
  846.                           case 3 - calculates weather average temperature *
  847. *************************************************************************/
  848. void findMatch(user **arrMen, womanUser *womHead, user *ptrUser,int *menSize)
  849. {
  850.     int i, j, countOnes = 0;
  851.     char commonHobbie, origCommonHobbie;
  852.     char *hobbies;
  853.     typedef int bool;
  854.     bool matched = 0;
  855.     womanUser *current = womHead;
  856.     char *minAge, *maxAge;
  857.     minAge = (char*)malloc(3 * sizeof(char));
  858.     if (minAge == NULL)
  859.         exit1(1);
  860.     maxAge = (char*)malloc(4 * sizeof(char));
  861.     if (maxAge == NULL)
  862.         exit1(1);
  863.     //initialize hobbies so we can use strcat
  864.     hobbies = (char*)malloc(250 * sizeof(char));
  865.     if (hobbies == NULL)
  866.         exit1(1);
  867.     printf("Please choose ages range:\n");
  868.     scanf("%s %s", minAge,maxAge);
  869.    
  870.     if (ptrUser->gender == 'F')
  871.     {
  872.         //if the user is a woman ,search a match in men array
  873.         for (i = 0; i < *menSize ; i++)
  874.         {
  875.             strcpy(hobbies, "\0");
  876.             if (strcmp(arrMen[i]->age, minAge) >= 0 && strcmp(arrMen[i]->age, maxAge) <= 0)
  877.             {
  878.                 origCommonHobbie = arrMen[i]->hobbies & ptrUser->hobbies;
  879.                 commonHobbie = origCommonHobbie;
  880.                 for (j = 0; j < 8; j++)
  881.                 {
  882.                     countOnes += commonHobbie & 1;
  883.                     commonHobbie = commonHobbie >> 1;
  884.                 }
  885.                 if (2 <= countOnes)
  886.                 {
  887.                     findHobbie(hobbies, arrMen[i]->hobbies);
  888.                     printf("Name: %s %s Age: %s Hobbies: %s Description: %s \n", \
  889.                         arrMen[i]->name, arrMen[i]->lastName, arrMen[i]->age, hobbies, arrMen[i]->desc);
  890.                     matched = 1;
  891.                 }
  892.             }
  893.  
  894.         } //end of for
  895.         if (minAge != NULL) {
  896.             free(minAge);
  897.             minAge = NULL;
  898.         }
  899.         if (maxAge != NULL) {
  900.             free(maxAge);
  901.             maxAge = NULL;
  902.         }
  903.         if (hobbies != NULL) {
  904.             free(hobbies);
  905.             hobbies = NULL;
  906.         }
  907.         if (!matched)
  908.             return;
  909.     }
  910.     else
  911.     {
  912.         //if the user is a woman ,search a match in man array
  913.         while (current != NULL)
  914.         {
  915.             strcpy(hobbies, "\0");
  916.             if (strcmp(current->womData->age,minAge) >= 0 && strcmp(current->womData->age,maxAge) <= 0)
  917.             {
  918.                 origCommonHobbie = current->womData->hobbies & ptrUser->hobbies;
  919.                 commonHobbie = origCommonHobbie;
  920.                 for (j = 0; j < 8; j++)
  921.                 {
  922.                     countOnes += commonHobbie & 1;
  923.                     commonHobbie = commonHobbie >> 1;
  924.                 }
  925.                 if (2 <= countOnes)
  926.                 {
  927.                     findHobbie(hobbies, current->womData->hobbies);
  928.                     printf("Name: %s %s Age: %s Hobbies: %s Description: %s \n", \
  929.                         current->womData->name, current->womData->lastName, current->womData->age, \
  930.                         hobbies, current->womData->desc);
  931.                     matched = 1;
  932.                 }
  933.             }
  934.             current = current->next;
  935.         } // end of while
  936.         if (minAge != NULL) {
  937.             free(minAge);
  938.             minAge = NULL;
  939.         }
  940.         if (maxAge != NULL) {
  941.             free(maxAge);
  942.             maxAge = NULL;
  943.         }
  944.         if (hobbies != NULL) {
  945.             free(hobbies);
  946.             hobbies = NULL;
  947.         }
  948.         if (!matched)
  949.             return;
  950.     }
  951.  
  952. }
  953. /************************************************************************
  954. * function name: main *
  955. * The Input: The user should enter number of task 1/2/3 and for each case:
  956.              case 1 - 4 characters
  957.              case 2 - 2 numbers and an operator
  958.              case 3 - 4 average temperature for each season *
  959. * The output:  case 1 - returns valid or invalid output
  960.                case 2 - returns a result after an arithmetic action
  961.                case 3 - returns the weather condition during the year *
  962. * The Function operation: case 1 - checks validity of input according to some criterias
  963.                           case 2 - operates as a calculator
  964.                           case 3 - calculates weather average temperature *
  965. *************************************************************************/
  966. void findHobbie(char *hobbies, char commonHobbie)
  967. {
  968.     int count = 0, commaIndicator = 0;
  969.     int digitCommon;
  970.     while (count != 8)
  971.     {
  972.         digitCommon = commonHobbie & 1;
  973.         count++;
  974.         if (digitCommon == 1)
  975.         {
  976.             switch (count)
  977.             {
  978.             case 1:
  979.                 strcat(hobbies, "Baseball");
  980.                 commaIndicator++;
  981.                 break;
  982.             case 2:
  983.                 strcat(hobbies, "Basketball");
  984.                 commaIndicator++;
  985.                 break;
  986.             case 3:
  987.                 strcat(hobbies, "Bicycle");
  988.                 commaIndicator++;
  989.                 break;
  990.             case 4:
  991.                 strcat(hobbies, "Books");
  992.                 commaIndicator++;
  993.                 break;
  994.             case 5:
  995.                 strcat(hobbies, "Drawing");
  996.                 commaIndicator++;
  997.                 break;
  998.             case 6:
  999.                 strcat(hobbies, "Gym");
  1000.                 commaIndicator++;
  1001.                 break;
  1002.             case 7:
  1003.                 strcat(hobbies, "Movies");
  1004.                 commaIndicator++;
  1005.                 break;
  1006.             case 8:
  1007.                 strcat(hobbies, "Poetry");
  1008.                 commaIndicator++;
  1009.                 break;
  1010.            
  1011.             }// end of switch
  1012.             // don't add a comma after the last element
  1013.             if (commaIndicator <= 3)
  1014.                 strcat(hobbies, ", ");
  1015.         }
  1016.         commonHobbie = commonHobbie >> 1;
  1017.     }//end of while
  1018. }
  1019.  
  1020. /************************************************************************
  1021. * function name: main *
  1022. * The Input: The user should enter number of task 1/2/3 and for each case:
  1023.              case 1 - 4 characters
  1024.              case 2 - 2 numbers and an operator
  1025.              case 3 - 4 average temperature for each season *
  1026. * The output:  case 1 - returns valid or invalid output
  1027.                case 2 - returns a result after an arithmetic action
  1028.                case 3 - returns the weather condition during the year *
  1029. * The Function operation: case 1 - checks validity of input according to some criterias
  1030.                           case 2 - operates as a calculator
  1031.                           case 3 - calculates weather average temperature *
  1032. *************************************************************************/
  1033. void writeFile(user **arrMen, womanUser *womHead, int *menSize)
  1034. {
  1035.     int i;
  1036.     womanUser *current;
  1037.     FILE *fptr;
  1038.     fptr = fopen("output.txt", "w");
  1039.     char *line;
  1040.     line = (char*)malloc(sizeof(char) * 300);
  1041.     if (line == NULL)
  1042.         exit1(1);
  1043.     // write men data to the file
  1044.     for (i = 0; i < *menSize; i++)
  1045.     {
  1046.         strcpy(line,"\0");
  1047.         strcat(line, arrMen[i]->id);
  1048.         strcat(line, ";");
  1049.         strcat(line, arrMen[i]->name);
  1050.         strcat(line, ";");
  1051.         strcat(line, arrMen[i]->lastName);
  1052.         strcat(line, ";");
  1053.         strcat(line, arrMen[i]->age);
  1054.         strcat(line, ";");
  1055.         // add a char so we cannot use strcat as usual
  1056.         strcat(line, " ");
  1057.         line[strlen(line)-1] = arrMen[i]->gender;
  1058.         strcat(line, ";");
  1059.         strcat(line, arrMen[i]->userName);
  1060.         strcat(line, ";");
  1061.         strcat(line, arrMen[i]->pass);
  1062.         strcat(line, ";");
  1063.         // add a char so we cannot use strcat as usual
  1064.         strcat(line, " ");
  1065.         line[strlen(line) - 1] = arrMen[i]->hobbies;
  1066.         strcat(line, ";");
  1067.         strcat(line, arrMen[i]->desc);
  1068.         fputs(line, fptr);
  1069.     }
  1070.     //write woman data to the file
  1071.     current = womHead;
  1072.     while (current!=NULL)
  1073.     {
  1074.         strcpy(line,"\0");
  1075.         strcat(line, current->womData->id);
  1076.         strcat(line, ";");
  1077.         strcat(line, current->womData->name);
  1078.         strcat(line, ";");
  1079.         strcat(line, current->womData->lastName);
  1080.         strcat(line, ";");
  1081.         strcat(line, current->womData->age);
  1082.         strcat(line, ";");
  1083.         strcat(line, " ");
  1084.         line[strlen(line) - 1] = current->womData->gender;
  1085.         strcat(line, ";");
  1086.         strcat(line, current->womData->userName);
  1087.         strcat(line, ";");
  1088.         strcat(line, current->womData->pass);
  1089.         strcat(line, ";");
  1090.         strcat(line, " ");
  1091.         line[strlen(line) - 1] = current->womData->hobbies;
  1092.         strcat(line, ";");
  1093.         strcat(line, current->womData->desc);
  1094.         fputs(line, fptr);
  1095.         current = current->next;
  1096.     }
  1097.  
  1098.     fclose(fptr);
  1099.     free(line);
  1100.     line = NULL;
  1101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement