Advertisement
Guest User

Untitled

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