Advertisement
Guest User

Untitled

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