Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define MIN_DIGIT '0'
- #define MAX_DIGIT '9'
- #define MOBILE_LEGNTH 10
- #define HOME_LENGTH 9
- #define MOBILE_DIGIT '5'
- #define HOME_DIGIT '4'
- #define COUNTRY_START_DIGIT '0'
- #define MALLOC_MAX_DIGITS 11
- char** add_number(char* phonebook[], int* phonebook_len);
- int get_exists(char* phonebook[], char* phone_num, int* phonebook_len);
- int valid(char* phone_num);
- char** get_bigger_phonebook(char* old_phonebook[], int* phonebook_len);
- void delete_phonebook(char* phonebook[], int* phonebook_len) {
- char res = 0;
- printf("Are you sure you want to delete the phonebook [Y/N]: ");
- scanf(" %c", &res);
- if ((res != 'Y') && (res != 'y'))
- return;
- for (int i = 0; i < (*phonebook_len) + 1 - 1; i++)
- //if (valid(phonebook[i])) // check if phone number is valid to be free'd.
- free(phonebook[i]);
- printf("The phonebook has been successfully cleared.\n");
- }
- void delete_number(char* phonebook[], int *phonebook_len) {
- char new_phone_num[11];
- scanf("%s", new_phone_num); // will scan string to new_phone_num.
- int exists_indx = get_exists(phonebook, new_phone_num, phonebook_len); // will go to get_exists to get existing phone number index.
- if (exists_indx != -1) {
- phonebook[exists_indx] = NULL;
- printf("The number has been successfully removed from the phone book.\n");
- }
- else
- printf("The number does not exist in the phone book.\n");
- }
- int get_exists(char* phonebook[], char* phone_num, int* phonebook_len) {
- for (int i = 0; i < (*phonebook_len); i++)
- if (!(strcmp(phonebook[i], phone_num)))//if the same then return index in phonebook.
- return i;
- return -1;
- }
- int valid(char* phone_num) {
- if (phone_num[0] != COUNTRY_START_DIGIT) // phone number must start at 0
- return 0;
- int phone_len = (phone_num[1] == MOBILE_DIGIT) ? MOBILE_LEGNTH : HOME_LENGTH; // if 05 then mobile, else its home.
- for (int i = 0; i < phone_len; i++)
- if (!((MIN_DIGIT <= phone_num[i]) && (phone_num[i] <= MAX_DIGIT))) // if temp[i] is not in range.
- return 0;
- return 1;
- }
- int check_number(char* phonebook[], int* phonebook_len, char* phone_num) {
- if (phone_num[0] != COUNTRY_START_DIGIT)
- return -1;
- int phone_len = (phone_num[1] == MOBILE_DIGIT) ? MOBILE_LEGNTH : HOME_LENGTH;
- for (int i = 0; i < phone_len; i++)
- if (!((MIN_DIGIT <= phone_num[i]) && (phone_num[i] <= MAX_DIGIT))) // if temp[i] is not in range.
- return -1;
- for (int i = 0; i < (*phonebook_len); i++)
- if (!(strcmp(phonebook[i], phone_num)))
- return 1;
- return 0;
- }
- void printError(int error) {
- if (error == 1)
- printf("The number already exists\n");
- else
- if (error == -1)
- printf("The number is invalid\n");
- }
- char** add_number(char* phonebook[], int* phonebook_len) {
- printf("Please enter a number: ");
- char new_phone_num[11];
- scanf("%s", new_phone_num);
- int res = check_number(phonebook, phonebook_len, new_phone_num);
- if (res) {
- printError(res);
- return NULL;
- }
- char** bigger_phonebook = get_bigger_phonebook(phonebook, phonebook_len);
- strcpy(bigger_phonebook[*phonebook_len - 1], new_phone_num);
- return bigger_phonebook;
- }
- char** get_bigger_phonebook(char* old_phonebook[], int* phonebook_len) {
- (*phonebook_len)++;
- char** new_phonebook = (char**)malloc((*phonebook_len) * sizeof(char*));
- for (int i = 0; i < (*phonebook_len) - 1; i++) {
- new_phonebook[i] = (char**)malloc(MALLOC_MAX_DIGITS*sizeof(char*));
- strcpy(new_phonebook[i], old_phonebook[i]);
- free(old_phonebook[i]);
- }
- new_phonebook[(*phonebook_len) - 1] = (char**)malloc(MALLOC_MAX_DIGITS * sizeof(char*));
- return new_phonebook;
- }
- void main() {
- int user_choice = 0;
- int phonebook_len = 0;
- int* p_len = &phonebook_len;
- char** phonebook = 0;
- char** flag = 0;
- printf("Weolcome to the Phone Book!\n");
- while (1) {
- printf("1. Add number\n2. Remove number\n3. Statistics\n4. Clear\nPlease select an action: ");
- scanf("%d", &user_choice);
- switch (user_choice) {
- case 1:
- flag = add_number(phonebook, p_len);
- if (flag)
- phonebook = flag;
- break;
- case 2:
- delete_number(phonebook, p_len);
- break;
- case 3:
- break;
- case 4:
- delete_phonebook(phonebook, p_len);
- break;
- default:
- if (user_choice == 'q' || user_choice == 'Q')
- break;
- else;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement