Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************
- * *Student name: Shaked Arbili
- * *Student ID: 206230880
- * *Exercise name: Exercise 6
- * ******************************************/
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct
- {
- char id[10];
- char *name;
- char *lastName;
- char *age;
- char gender;
- char *userName;
- char *pass;
- unsigned char hobbies;
- char *desc;
- }user;
- user* ptrUser;
- typedef struct womanUser
- {
- user *womData;
- struct womanUser *next;
- }womanUser;
- enum hobbies {BASEBALL = 1,BASKETBALL,BICYCLE,BOOKS,DRAWING,GYM,MOVIES,POETRY};
- enum memoryActions {MALLOC = 0,REALLOC = 1};
- //declaring functions
- void login(user **arrMen, womanUser *womHead, int *menSize);
- int usernameExist(user **arrMen, womanUser *womHead, char *username, int *menSize);
- int idExist(user **arrMen, womanUser *womHead, char id[10],int *menSize);
- void loadData(user ***arrMen, womanUser **womHead,int *menSize);
- void freeAll(user **arrMen, womanUser *womHead,int *menSize);
- void freeUserPointers(user* ptrUser);
- void newMember(user *ptrUser, user ***arrMen, womanUser *womHead, int *menSize);
- void addNew(user *ptrUser, user ***arrMen, womanUser **ptrWomHead, int *menSize);
- void mainMenu(user *ptrUser, user **arrMen, womanUser *womHead, int *menSize);
- void allocateMemory(user *ptrUser,int memoAction);
- void findMatch(user **arrMen, womanUser *womHead, user *ptrUser, int *menSize);
- void deleteUser(user ***arrMen, womanUser **womHead, char id[10], char gender, int *menSize);
- void findHobbie(char *hobbies, char commonHobbie);
- void writeFile(user **arrMen, womanUser *womHead, int *menSize);
- int isCorrectPass(user **arrMen, womanUser *womHead, char *username, char *pass, int *menSize, user *ptrUser);
- static user **arrMen = NULL;
- static womanUser *womHead = NULL;
- static int menSize = 0;
- void cleanup() // vcs
- {
- freeAll(arrMen, womHead, &menSize);
- //if (ptrUser != NULL)
- //{
- //if (ptrUser->name != NULL)
- // freeUserPointers(ptrUser); // vcs fix exception
- //free(ptrUser);
- //ptrUser = NULL;
- //}
- }
- void exit1(/*user* ptrUser, */int _Code) // vcs
- {
- cleanup();
- exit(_Code);
- }
- /************************************************************************
- * function name: main *
- * The Input: The user should enter a number of actions he wants to do:
- case 1 - login
- case 2 - register as a new member
- case 3 - write all the data to a file
- * The output: -*
- * The Function operation: This function is used as a login menu for the user *
- *************************************************************************/
- int main()
- {
- int option;
- //user **arrMen = NULL;
- //int menSize = 0;
- // pointer to the first element in women linked list
- //womanUser *womHead = NULL;
- loadData(&arrMen, &womHead, &menSize);
- printf("Welcome! please choose an option\n");
- printf("1 - Log in\n");
- printf("2 - New member\n");
- printf("3 - Exit\n");
- scanf("%d", &option);
- while(option)
- {
- switch (option)
- {
- case 1:
- login(arrMen, womHead, &menSize);
- break;
- case 2:
- newMember(ptrUser, &arrMen, womHead, &menSize);
- break;
- case 3:
- writeFile(arrMen, womHead, &menSize);
- cleanup();
- return 0;
- break;
- default:
- printf("Bad choice, please try again\n");
- break;
- }
- printf("Welcome! please choose an option\n");
- printf("1 - Log in\n");
- printf("2 - New member\n");
- printf("3 - Exit\n");
- scanf("%d", &option);
- }// end of while
- return 0;
- }
- /************************************************************************
- * function name: login *
- * The Input: The user should enter username and password*
- * The output: If user exists this function sends him to the main menu
- * The Function operation: The function checks if the user is exist in the system
- *************************************************************************/
- void login(user **arrMen, womanUser *womHead, int *menSize)
- {
- char username[15], pass[11];
- int exist, i;
- char dummy;
- womanUser *current;
- scanf("%c", &dummy);
- printf("Please enter your username:\n");
- scanf("%s", username);
- // check correctness of the username
- exist = usernameExist(arrMen, womHead, username, menSize);
- if (!exist)
- {
- // give the user second chance to enter his correct login details
- printf("User do not exist in the system, please try again\n");
- printf("Please enter your username:\n");
- scanf("%s", username);
- exist = usernameExist(arrMen, womHead, username, menSize);
- if (!exist)
- return;
- }
- printf("Please enter your password:\n");
- scanf("%s", pass);
- //search in women list
- current = womHead;
- while (current != NULL)
- {
- if (strcmp(current->womData->userName, current->womData->userName) == 0)
- if (strcmp(current->womData->pass, pass) == 0)
- {
- printf("Hello %s!\n", current->womData->name);
- mainMenu(current->womData, arrMen, womHead, menSize);
- return;
- }
- current = current->next;
- }
- if (arrMen != NULL)
- {
- //search in men array
- for (i = 0; i < *menSize; i++)
- {
- if (strcmp(arrMen[i]->userName, username) == 0)
- if (strcmp(arrMen[i]->pass, pass) == 0)
- {
- printf("Hello %s!\n", arrMen[i]->name);
- mainMenu(arrMen[i], arrMen, womHead, menSize);
- return;
- }
- }
- }
- printf("Wrong password\n");
- return;
- }
- /************************************************************************
- * function name: usernameExist *
- * The Input: Ths function gets username *
- * The output: Returns 1 if username exist in the system, else 0 *
- * The Function operation: Returns True ot False - 1 or 0 according to the user existness *
- *************************************************************************/
- int usernameExist(user **arrMen, womanUser *womHead, char *username, int *menSize)
- {
- womanUser *current;
- int i;
- //search in women linked list if username is exist
- current = womHead;
- while (current != NULL)
- {
- if (strcmp(current->womData->userName,username) == 0)
- return 1;
- current = current->next;
- }
- if (arrMen != NULL)
- {
- //search in men array if username is exist
- for (i = 0; i < *menSize; i++)
- {
- if (strcmp(arrMen[i]->userName,username) == 0)
- return 1;
- }
- }
- return 0;
- }
- /************************************************************************
- * function name: idExist *
- * The Input: Ths function gets id *
- * The output: Returns 1 if id exist in the system, else 0 *
- * The Function operation: Returns True ot False - 1 or 0 according to the id existness *
- *************************************************************************/
- int idExist(user **arrMen, womanUser *womHead,char id[10],int *menSize)
- {
- womanUser *current;
- int i;
- //search in women list
- current = womHead;
- while (current != NULL)
- {
- if (strcmp(current->womData->id,id) == 0)
- return 1;
- current = current->next;
- }
- if (arrMen != NULL)
- {
- //search in men array
- for (i = 0; i < *menSize; i++)
- {
- if (strcmp(arrMen[i]->id,id) == 0)
- return 1;
- }
- }
- return 0;
- }
- /************************************************************************
- * function name: loadData *
- * The Input: The function gets a file with data about users*
- * The output: - *
- * The Function operation: The function loads data from a file and enters it to the right structre
- *************************************************************************/
- void loadData(user ***arrMen, womanUser **ptrWomHead, int *menSize)
- {
- FILE *fptr;
- char line[300];
- int i, countLines = 0, tmpHobbie, usersSize = 0;
- char *detail;
- womanUser *previousPtr = NULL,*currentPtr = NULL;
- currentPtr = *ptrWomHead;
- fptr = fopen("input.txt", "r");
- if (fptr == NULL)
- return;
- while (fgets(line, 300, fptr))
- usersSize++;
- fclose(fptr);
- user users[100];
- fptr = fopen("input.txt", "r");
- if (fptr == NULL)
- return;
- while (fgets(line, 300, fptr))
- {
- tmpHobbie = 0;
- // allocate enough memory for features of user
- users[countLines].name = (char *)malloc(15 * sizeof(char));
- if (users[countLines].name == NULL)
- exit1(1);
- users[countLines].lastName = (char *)malloc(15 * sizeof(char));
- if (users[countLines].lastName == NULL)
- exit1(1);
- users[countLines].age = (char *)malloc(3 * sizeof(char));
- if (users[countLines].age == NULL)
- exit1(1);
- users[countLines].userName = (char *)malloc(10 * sizeof(char));
- if (users[countLines].name == NULL)
- exit1(1);
- users[countLines].pass = (char *)malloc(15 * sizeof(char));
- if (users[countLines].pass == NULL)
- exit1(1);
- users[countLines].desc = (char *)malloc(250 * sizeof(char));
- if (users[countLines].desc == NULL)
- exit1(1);
- detail = strtok(line, ";");
- strcpy(users[countLines].id ,detail);
- detail = strtok(NULL, ";");
- strcpy(users[countLines].name,detail);
- detail = strtok(NULL, ";");
- strcpy(users[countLines].lastName ,detail);
- detail = strtok(NULL, ";");
- strcpy(users[countLines].age,detail);
- detail = strtok(NULL, ";");
- users[countLines].gender = *detail;
- detail = strtok(NULL, ";");
- strcpy(users[countLines].userName,detail);
- detail = strtok(NULL, ";");
- strcpy(users[countLines].pass, detail);
- detail = strtok(NULL, ";");
- for (i = 0; i <= (int)strlen(detail) - 1; i += 2)
- {
- tmpHobbie = tmpHobbie | (1 << ((int)(detail[i] - '0') - 1));
- }
- users[countLines].hobbies = tmpHobbie;
- detail = strtok(NULL, ";");
- strcpy(users[countLines].desc ,detail);
- // allocate the exact memory for each feature
- users[countLines].name = (char*)realloc(users[countLines].name, strlen(users[countLines].name) * sizeof(char) + 1);
- if (users[countLines].name == NULL)
- exit1(1);
- users[countLines].lastName = (char*)realloc(users[countLines].lastName, strlen(users[countLines].lastName) * sizeof(char) + 1);
- if (users[countLines].lastName == NULL)
- exit1(1);
- users[countLines].age = (char*)realloc(users[countLines].age, strlen(users[countLines].age) * sizeof(char) + 1);
- if (users[countLines].age == NULL)
- exit1(1);
- users[countLines].userName = (char*)realloc(users[countLines].userName, strlen(users[countLines].userName) * sizeof(char) + 1);
- if (users[countLines].userName == NULL)
- exit1(1);
- users[countLines].pass = (char*)realloc(users[countLines].pass, strlen(users[countLines].pass) * sizeof(char) + 1);
- if (users[countLines].pass == NULL)
- exit1(1);
- users[countLines].desc = (char*)realloc(users[countLines].desc, strlen(users[countLines].desc) * sizeof(char) + 1);
- if (users[countLines].desc == NULL)
- exit1(1);
- countLines++;
- }
- fclose(fptr);
- // classify users to the right group and add them to the right structure
- for (i = 0; i < usersSize; i++)
- {
- addNew(&users[i], arrMen, ptrWomHead, menSize);
- }
- }
- /************************************************************************
- * function name: freeAll *
- * The Input: The function gets the data structues *
- * The output: - *
- * The Function operation: The function free all the pointers which were allocated in the program*
- *************************************************************************/
- void freeAll(user **arrMen, womanUser *womHead, int *menSize)
- {
- womanUser *current = womHead;
- womanUser *previous = current;
- int i;
- //free all pointers in woman list
- while (current != NULL)
- {
- if (current->womData->name != NULL)
- {
- free(current->womData->name);
- current->womData->name = NULL;
- }
- if (current->womData->lastName != NULL)
- {
- free(current->womData->lastName);
- current->womData->lastName = NULL;
- }
- if (current->womData->age != NULL)
- {
- free(current->womData->age);
- current->womData->age = NULL;
- }
- if (current->womData->userName != NULL)
- {
- free(current->womData->userName);
- current->womData->userName = NULL;
- }
- if (current->womData->pass != NULL)
- {
- free(current->womData->pass);
- current->womData->pass = NULL;
- }
- if (current->womData->desc != NULL)
- {
- free(current->womData->desc);
- current->womData->desc = NULL;
- }
- previous = current;
- current = current->next;
- if (previous != NULL)
- {
- free(previous);
- previous = NULL;
- }
- }
- //free all pointers in men array
- for (i = 0; i < *menSize; i++)
- {
- if (arrMen[i]->name != NULL)
- {
- free(arrMen[i]->name);
- arrMen[i]->name = NULL;
- }
- if (arrMen[i]->lastName != NULL)
- {
- free(arrMen[i]->lastName);
- arrMen[i]->lastName = NULL;
- }
- if (arrMen[i]->age != NULL)
- {
- free(arrMen[i]->age);
- arrMen[i]->age = NULL;
- }
- if (arrMen[i]->userName != NULL)
- {
- free(arrMen[i]->userName);
- arrMen[i]->userName = NULL;
- }
- if (arrMen[i]->pass != NULL)
- {
- free(arrMen[i]->pass);
- arrMen[i]->pass = NULL;
- }
- if (arrMen[i]->desc != NULL)
- {
- free(arrMen[i]->desc);
- arrMen[i]->desc = NULL;
- }
- }
- if (arrMen != NULL)
- {
- free(arrMen);
- arrMen = NULL;
- }
- }
- /************************************************************************
- * function name: freeUserPointers *
- * The Input: The function gets a specific pointer to a user struct*
- * The output: - *
- * The Function operation: The function frees all the pointers of user*
- *************************************************************************/
- void freeUserPointers(user *ptrUser)
- {
- if (ptrUser->name != NULL)
- {
- free(ptrUser->name);
- ptrUser->name = NULL;
- }
- if (ptrUser->lastName != NULL)
- {
- free(ptrUser->lastName);
- ptrUser->lastName = NULL;
- }
- if (ptrUser->age != NULL)
- {
- free(ptrUser->age);
- ptrUser->age = NULL;
- }
- if (ptrUser->userName != NULL)
- {
- free(ptrUser->userName);
- ptrUser->userName = NULL;
- }
- if (ptrUser->pass != NULL)
- {
- free(ptrUser->pass);
- ptrUser->pass = NULL;
- }
- if (ptrUser->desc != NULL)
- {
- free(ptrUser->desc);
- ptrUser->desc = NULL;
- }
- }
- /************************************************************************
- * function name: newMember *
- * The Input: The *
- * The output: - *
- * The Function operation: *
- *************************************************************************/
- void newMember(user *ptrUser, user ***arrMen, womanUser *womHead, int *menSize)
- {
- int exist;
- char tmpHobbies[8], dummy;;
- int i;
- int hobbie;
- ptrUser = (user *)malloc(sizeof(user));
- if (ptrUser == NULL)
- exit1(1);
- allocateMemory(ptrUser, MALLOC);
- printf("Please enter your ID:\n");
- scanf("%s", ptrUser->id);
- //check that id doesnt exist
- exist = idExist(*arrMen, womHead, ptrUser->id, menSize);
- if (exist)
- {
- printf("Error: User already exists\n");
- freeUserPointers(ptrUser);
- return;
- }
- printf("Please enter your first name:\n");
- scanf("%s", ptrUser->name);
- printf("Please enter your last name:\n");
- scanf("%s", ptrUser->lastName);
- printf("Please enter your age (18 to 100):\n");
- scanf("%s", ptrUser->age);
- if (strcmp(ptrUser->age, "18") < 0 || (strcmp(ptrUser->age, "100") > 0 \
- && (strlen(ptrUser->age) >= 3) || strlen(ptrUser->age) != 2 && strlen(ptrUser->age) != 3))
- {
- freeUserPointers(ptrUser);
- return;
- }
- printf("Please enter your gender (F-female, M-male):\n");
- scanf("%s", &ptrUser->gender);
- printf("Choose a username (3-10 characters):\n");
- scanf("%s", ptrUser->userName);
- if (strlen(ptrUser->userName) < 3)
- {
- freeUserPointers(ptrUser);
- return;
- }
- if ((ptrUser->userName[0] < 'A' || ptrUser->userName[0] > 'z') || (ptrUser->userName[0] > 'Z') \
- && ptrUser->userName[0] < 'a')
- {
- freeUserPointers(ptrUser);
- return;
- }
- printf("please choose 4 hobbies: Baseball=1, Basketball=2, Bicycle=3, Books=4, Drawing=5, Gym=6, Movies=7, \
- Poetry=8\n");
- scanf("%c", &dummy);
- fgets(tmpHobbies, 8, stdin);
- hobbie = 0;
- for (i = 0; i < sizeof(tmpHobbies); i += 2)
- hobbie = hobbie | (1 << ((int)(tmpHobbies[i] - '0') - 1));
- ptrUser->hobbies = hobbie;
- printf("Choose a password (attention-minimum 3 characters):\n");
- scanf("%s", ptrUser->pass);
- if (strlen(ptrUser->pass) < 3)
- {
- freeUserPointers(ptrUser);
- return;
- }
- printf("Some words about yourself:\n");
- scanf("%c", &dummy);
- fgets(ptrUser->desc, 250, stdin);
- allocateMemory(ptrUser, REALLOC);
- addNew(ptrUser,arrMen, &womHead, menSize);
- printf("Hi %s, lets find love!\n", ptrUser->name);
- mainMenu(ptrUser, *arrMen, womHead, menSize);
- }
- void addNew(user *ptrUser, user ***ptrArrMen, womanUser **ptrWomHead, int *menSize)
- {
- typedef int bool;
- bool inserted = 0;
- womanUser *currentPtr = NULL, *previousPtr = NULL;
- womanUser *newWoman;
- currentPtr = *ptrWomHead;
- if (ptrUser->gender == 'F')
- {
- if (*ptrWomHead == NULL)
- {
- *ptrWomHead = (womanUser *)malloc(sizeof(womanUser));
- if (*ptrWomHead == NULL)
- exit1(1);
- (*ptrWomHead)->womData = ptrUser;
- (*ptrWomHead)->next = NULL;
- }
- else
- {
- //check where we should put this woman according to ABC order
- newWoman = (womanUser*)malloc(sizeof(womanUser));
- if (newWoman == NULL)
- exit1(1);
- newWoman->womData = ptrUser;
- while (!inserted)
- {
- if (strcmp(ptrUser->lastName, currentPtr->womData->lastName) < 0)
- {
- // insert the new woman at the first place
- if (currentPtr == *ptrWomHead)
- {
- newWoman->next = *ptrWomHead;
- *ptrWomHead = newWoman;
- inserted = 1;
- }
- else
- {
- //should enter the new women between two elements
- newWoman->next = previousPtr;
- previousPtr = newWoman;
- inserted = 1;
- }
- }
- else
- {
- if (currentPtr->next == NULL)
- {
- // insert the new woman at the last place
- newWoman->womData = NULL;
- currentPtr->next = newWoman;
- inserted = 1;
- }
- else
- {
- previousPtr = currentPtr;
- currentPtr = currentPtr->next;
- }
- }
- }// end of while
- }
- }
- else
- {
- (*menSize)++;
- if (*ptrArrMen == NULL)
- {
- *ptrArrMen = (user **)malloc(*menSize * sizeof(user *));
- if (ptrArrMen == NULL)
- exit1(1);
- }
- else
- {
- *ptrArrMen = (user **)realloc(*ptrArrMen, (*menSize) * sizeof(user*));
- if (ptrArrMen == NULL)
- exit1(1);
- }
- (*ptrArrMen)[*menSize - 1] = ptrUser;
- }
- }
- //*/
- /************************************************************************
- * function name: mainMenu*
- * The Input: The user should enter number of task 1/2/3 and for each case:
- case 1 - 4 characters
- case 2 - 2 numbers and an operator
- case 3 - 4 average temperature for each season *
- * The output: case 1 - returns valid or invalid output
- case 2 - returns a result after an arithmetic action
- case 3 - returns the weather condition during the year *
- * The Function operation: *
- *************************************************************************/
- void mainMenu(user *ptrUser, user **arrMen, womanUser *womHead, int *menSize)
- {
- int option;
- printf("Please choose an option:\n");
- printf("1. Find a match\n");
- printf("2. I found love, DELETE me\n");
- printf("3. Log out\n");
- scanf("%d", &option);
- while (option >= 1 && option <= 3)
- {
- switch (option)
- {
- case 1:
- findMatch(arrMen, womHead, ptrUser,menSize);
- break;
- case 2:
- deleteUser(&arrMen, &womHead, ptrUser->id, ptrUser->gender, menSize);
- return;
- break;
- case 3:
- return;
- break;
- default:
- printf("Bad choice, please try again\n");
- return;
- }
- printf("Please choose an option:\n");
- printf("1. Find a match\n");
- printf("2. I found love, DELETE me\n");
- printf("3. Log out\n");
- scanf("%d", &option);
- }// end of while
- }
- /************************************************************************
- * function name: allocateMemory *
- * The Input: The function gets a pointer to user struct and a memory actions we would like to do -
- malloc or realloc *
- * The output: - *
- * The Function operation: The function does malloc or realloc to ptrUser params*
- *************************************************************************/
- void allocateMemory(user *ptrUser, int memoAction)
- {
- switch (memoAction)
- {
- case MALLOC:
- ptrUser->name = (char *)malloc(15 * sizeof(char));
- if (ptrUser->name == NULL)
- exit1(1);
- ptrUser->lastName = (char *)malloc(15 * sizeof(char));
- if (ptrUser->lastName == NULL)
- exit1(1);
- ptrUser->age = (char *)malloc(3 * sizeof(char));
- if (ptrUser->age == NULL)
- exit1(1);
- ptrUser->userName = (char *)malloc(10 * sizeof(char));
- if (ptrUser->userName == NULL)
- exit1(1);
- ptrUser->pass = (char *)malloc(15 * sizeof(char));
- if (ptrUser->pass == NULL)
- exit1(1);
- ptrUser->desc = (char *)malloc(250 * sizeof(char));
- if (ptrUser->desc == NULL)
- exit1(1);
- break;
- case REALLOC:
- ptrUser->name = (char*)realloc(ptrUser->name, strlen(ptrUser->name) * sizeof(char) + 1);
- if (ptrUser->name == NULL)
- exit1(1);
- ptrUser->lastName = (char*)realloc(ptrUser->lastName, strlen(ptrUser->lastName) * sizeof(char) + 1);
- if (ptrUser->lastName == NULL)
- exit1(1);
- ptrUser->age = (char*)realloc(ptrUser->age, strlen(ptrUser->age) * sizeof(char) + 1);
- if (ptrUser->age == NULL)
- exit1(1);
- ptrUser->userName = (char*)realloc(ptrUser->userName, strlen(ptrUser->userName) * sizeof(char) + 1);
- if (ptrUser->userName == NULL)
- exit1(1);
- ptrUser->pass = (char*)realloc(ptrUser->pass, strlen(ptrUser->pass) * sizeof(char) + 1);
- if (ptrUser->pass == NULL)
- exit1(1);
- ptrUser->desc = (char*)realloc(ptrUser->desc, strlen(ptrUser->desc) * sizeof(char) + 1);
- if (ptrUser->desc == NULL)
- exit1(1);
- break;
- default:
- break;
- }
- }
- /************************************************************************
- * function name: main *
- * The Input: The user should enter number of task 1/2/3 and for each case:
- case 1 - 4 characters
- case 2 - 2 numbers and an operator
- case 3 - 4 average temperature for each season *
- * The output: case 1 - returns valid or invalid output
- case 2 - returns a result after an arithmetic action
- case 3 - returns the weather condition during the year *
- * The Function operation: case 1 - checks validity of input according to some criterias
- case 2 - operates as a calculator
- case 3 - calculates weather average temperature *
- *************************************************************************/
- void deleteUser(user ***ptrArrMen, womanUser **ptrWomHead, char id[10], char gender, int *menSize)
- {
- int i;
- typedef int bool;
- bool deleted = 0;
- womanUser *current,*prev, *tmp;
- if (gender == 'M')
- {
- //delete from men array
- for (i = 0; i < *menSize; i++)
- {
- if (strcmp((*ptrArrMen)[i]->id, id) == 0)
- {
- freeUserPointers((*ptrArrMen)[i]);
- deleted = 1;
- }
- // move all the other users left
- if (deleted)
- (*ptrArrMen)[i] = (*ptrArrMen)[i + 1];
- }
- (*menSize)--;
- *ptrArrMen = (user **)realloc(*ptrArrMen, (*menSize) * sizeof(*ptrArrMen));
- if (*ptrArrMen == NULL)
- exit1(1);
- }
- else
- {
- //delete from woman list
- current = *ptrWomHead;
- prev = *ptrWomHead;
- // delete the first women in the list
- if (current != NULL && strcmp(current->womData->id, id) == 0)
- {
- tmp = (*ptrWomHead)->next;
- // free old head
- if (*ptrWomHead != NULL)
- {
- free(*ptrWomHead);
- }
- // change head
- *ptrWomHead = tmp;
- return;
- }
- while (current != NULL)
- {
- if (strcmp(current->womData->id,id) == 0)
- {
- //item in the middle of the list
- tmp = prev->next;
- prev->next = current->next;
- freeUserPointers(tmp->womData);
- if (tmp != NULL)
- {
- free(tmp);
- tmp = NULL;
- }
- }
- prev = current;
- current = current->next;
- }
- }
- }
- /************************************************************************
- * function name: main *
- * The Input: The user should enter number of task 1/2/3 and for each case:
- case 1 - 4 characters
- case 2 - 2 numbers and an operator
- case 3 - 4 average temperature for each season *
- * The output: case 1 - returns valid or invalid output
- case 2 - returns a result after an arithmetic action
- case 3 - returns the weather condition during the year *
- * The Function operation: case 1 - checks validity of input according to some criterias
- case 2 - operates as a calculator
- case 3 - calculates weather average temperature *
- *************************************************************************/
- void findMatch(user **arrMen, womanUser *womHead, user *ptrUser,int *menSize)
- {
- int i, j, countOnes = 0;
- char commonHobbie, origCommonHobbie;
- char *hobbies;
- typedef int bool;
- bool matched = 0;
- womanUser *current = womHead;
- char *minAge, *maxAge;
- minAge = (char*)malloc(3 * sizeof(char));
- if (minAge == NULL)
- exit1(1);
- maxAge = (char*)malloc(4 * sizeof(char));
- if (maxAge == NULL)
- exit1(1);
- //initialize hobbies so we can use strcat
- hobbies = (char*)malloc(250 * sizeof(char));
- if (hobbies == NULL)
- exit1(1);
- printf("Please choose ages range:\n");
- scanf("%s %s", minAge,maxAge);
- if (ptrUser->gender == 'F')
- {
- //if the user is a woman ,search a match in men array
- for (i = 0; i < *menSize ; i++)
- {
- strcpy(hobbies, "\0");
- if (strcmp(arrMen[i]->age, minAge) >= 0 && strcmp(arrMen[i]->age, maxAge) <= 0)
- {
- origCommonHobbie = arrMen[i]->hobbies & ptrUser->hobbies;
- commonHobbie = origCommonHobbie;
- for (j = 0; j < 8; j++)
- {
- countOnes += commonHobbie & 1;
- commonHobbie = commonHobbie >> 1;
- }
- if (2 <= countOnes)
- {
- findHobbie(hobbies, arrMen[i]->hobbies);
- printf("Name: %s %s Age: %s Hobbies: %s Description: %s \n", \
- arrMen[i]->name, arrMen[i]->lastName, arrMen[i]->age, hobbies, arrMen[i]->desc);
- matched = 1;
- }
- }
- } //end of for
- if (minAge != NULL) {
- free(minAge);
- minAge = NULL;
- }
- if (maxAge != NULL) {
- free(maxAge);
- maxAge = NULL;
- }
- if (hobbies != NULL) {
- free(hobbies);
- hobbies = NULL;
- }
- if (!matched)
- return;
- }
- else
- {
- //if the user is a woman ,search a match in man array
- while (current != NULL)
- {
- strcpy(hobbies, "\0");
- if (strcmp(current->womData->age,minAge) >= 0 && strcmp(current->womData->age,maxAge) <= 0)
- {
- origCommonHobbie = current->womData->hobbies & ptrUser->hobbies;
- commonHobbie = origCommonHobbie;
- for (j = 0; j < 8; j++)
- {
- countOnes += commonHobbie & 1;
- commonHobbie = commonHobbie >> 1;
- }
- if (2 <= countOnes)
- {
- findHobbie(hobbies, current->womData->hobbies);
- printf("Name: %s %s Age: %s Hobbies: %s Description: %s \n", \
- current->womData->name, current->womData->lastName, current->womData->age, \
- hobbies, current->womData->desc);
- matched = 1;
- }
- }
- current = current->next;
- } // end of while
- if (minAge != NULL) {
- free(minAge);
- minAge = NULL;
- }
- if (maxAge != NULL) {
- free(maxAge);
- maxAge = NULL;
- }
- if (hobbies != NULL) {
- free(hobbies);
- hobbies = NULL;
- }
- if (!matched)
- return;
- }
- }
- /************************************************************************
- * function name: main *
- * The Input: The user should enter number of task 1/2/3 and for each case:
- case 1 - 4 characters
- case 2 - 2 numbers and an operator
- case 3 - 4 average temperature for each season *
- * The output: case 1 - returns valid or invalid output
- case 2 - returns a result after an arithmetic action
- case 3 - returns the weather condition during the year *
- * The Function operation: case 1 - checks validity of input according to some criterias
- case 2 - operates as a calculator
- case 3 - calculates weather average temperature *
- *************************************************************************/
- void findHobbie(char *hobbies, char commonHobbie)
- {
- int count = 0, commaIndicator = 0;
- int digitCommon;
- while (count != 8)
- {
- digitCommon = commonHobbie & 1;
- count++;
- if (digitCommon == 1)
- {
- switch (count)
- {
- case 1:
- strcat(hobbies, "Baseball");
- commaIndicator++;
- break;
- case 2:
- strcat(hobbies, "Basketball");
- commaIndicator++;
- break;
- case 3:
- strcat(hobbies, "Bicycle");
- commaIndicator++;
- break;
- case 4:
- strcat(hobbies, "Books");
- commaIndicator++;
- break;
- case 5:
- strcat(hobbies, "Drawing");
- commaIndicator++;
- break;
- case 6:
- strcat(hobbies, "Gym");
- commaIndicator++;
- break;
- case 7:
- strcat(hobbies, "Movies");
- commaIndicator++;
- break;
- case 8:
- strcat(hobbies, "Poetry");
- commaIndicator++;
- break;
- }// end of switch
- // don't add a comma after the last element
- if (commaIndicator <= 3)
- strcat(hobbies, ", ");
- }
- commonHobbie = commonHobbie >> 1;
- }//end of while
- }
- /************************************************************************
- * function name: main *
- * The Input: The user should enter number of task 1/2/3 and for each case:
- case 1 - 4 characters
- case 2 - 2 numbers and an operator
- case 3 - 4 average temperature for each season *
- * The output: case 1 - returns valid or invalid output
- case 2 - returns a result after an arithmetic action
- case 3 - returns the weather condition during the year *
- * The Function operation: case 1 - checks validity of input according to some criterias
- case 2 - operates as a calculator
- case 3 - calculates weather average temperature *
- *************************************************************************/
- void writeFile(user **arrMen, womanUser *womHead, int *menSize)
- {
- int i;
- womanUser *current;
- FILE *fptr;
- fptr = fopen("output.txt", "w");
- char *line;
- line = (char*)malloc(sizeof(char) * 300);
- if (line == NULL)
- exit1(1);
- // write men data to the file
- for (i = 0; i < *menSize; i++)
- {
- strcpy(line,"\0");
- strcat(line, arrMen[i]->id);
- strcat(line, ";");
- strcat(line, arrMen[i]->name);
- strcat(line, ";");
- strcat(line, arrMen[i]->lastName);
- strcat(line, ";");
- strcat(line, arrMen[i]->age);
- strcat(line, ";");
- // add a char so we cannot use strcat as usual
- strcat(line, " ");
- line[strlen(line)-1] = arrMen[i]->gender;
- strcat(line, ";");
- strcat(line, arrMen[i]->userName);
- strcat(line, ";");
- strcat(line, arrMen[i]->pass);
- strcat(line, ";");
- // add a char so we cannot use strcat as usual
- strcat(line, " ");
- line[strlen(line) - 1] = arrMen[i]->hobbies;
- strcat(line, ";");
- strcat(line, arrMen[i]->desc);
- fputs(line, fptr);
- }
- //write woman data to the file
- current = womHead;
- while (current!=NULL)
- {
- strcpy(line,"\0");
- strcat(line, current->womData->id);
- strcat(line, ";");
- strcat(line, current->womData->name);
- strcat(line, ";");
- strcat(line, current->womData->lastName);
- strcat(line, ";");
- strcat(line, current->womData->age);
- strcat(line, ";");
- strcat(line, " ");
- line[strlen(line) - 1] = current->womData->gender;
- strcat(line, ";");
- strcat(line, current->womData->userName);
- strcat(line, ";");
- strcat(line, current->womData->pass);
- strcat(line, ";");
- strcat(line, " ");
- line[strlen(line) - 1] = current->womData->hobbies;
- strcat(line, ";");
- strcat(line, current->womData->desc);
- fputs(line, fptr);
- current = current->next;
- }
- fclose(fptr);
- free(line);
- line = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement