Advertisement
Guest User

Untitled

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