Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define MIN_DIGIT '0'
  5. #define MAX_DIGIT '9'
  6. #define MOBILE_LEGNTH 10
  7. #define HOME_LENGTH 9
  8. #define MOBILE_DIGIT '5'
  9. #define HOME_DIGIT '4'
  10. #define COUNTRY_START_DIGIT '0'
  11. #define MALLOC_MAX_DIGITS 11
  12. char** add_number(char* phonebook[], int* phonebook_len);
  13. int get_exists(char* phonebook[], char* phone_num, int* phonebook_len);
  14. int valid(char* phone_num);
  15. char** get_bigger_phonebook(char* old_phonebook[], int* phonebook_len);
  16.  
  17.  
  18.  
  19. void delete_phonebook(char* phonebook[], int* phonebook_len) {
  20.     char res = 0;
  21.     printf("Are you sure you want to delete the phonebook [Y/N]: ");
  22.     scanf(" %c", &res);
  23.     if ((res != 'Y') && (res != 'y'))
  24.         return;
  25.     for (int i = 0; i < (*phonebook_len) + 1 - 1; i++)
  26.         //if (valid(phonebook[i])) // check if phone number is valid to be free'd.
  27.             free(phonebook[i]);
  28.     printf("The phonebook has been successfully cleared.\n");
  29. }
  30.  
  31. void delete_number(char* phonebook[], int *phonebook_len) {
  32.     char new_phone_num[11];
  33.     scanf("%s", new_phone_num); // will scan string to new_phone_num.
  34.     int exists_indx = get_exists(phonebook, new_phone_num, phonebook_len); // will go to get_exists to get existing phone number index.
  35.     if (exists_indx != -1) {
  36.         phonebook[exists_indx] = NULL;
  37.         printf("The number has been successfully removed from the phone book.\n");
  38.     }
  39.     else
  40.         printf("The number does not exist in the phone book.\n");
  41. }
  42.  
  43. int get_exists(char* phonebook[], char* phone_num, int* phonebook_len) {
  44.     for (int i = 0; i < (*phonebook_len); i++)
  45.         if (!(strcmp(phonebook[i], phone_num)))//if the same then return index in phonebook.
  46.             return i;
  47.     return -1;
  48. }
  49.  
  50. int valid(char* phone_num) {
  51.     if (phone_num[0] != COUNTRY_START_DIGIT) // phone number must start at 0
  52.         return 0;
  53.     int phone_len = (phone_num[1] == MOBILE_DIGIT) ? MOBILE_LEGNTH : HOME_LENGTH; // if 05 then mobile, else its home.
  54.     for (int i = 0; i < phone_len; i++)
  55.         if (!((MIN_DIGIT <= phone_num[i]) && (phone_num[i] <= MAX_DIGIT))) // if temp[i] is not in range.
  56.             return 0;
  57.     return 1;
  58. }
  59.  
  60. int check_number(char* phonebook[], int* phonebook_len, char* phone_num) {
  61.     if (phone_num[0] != COUNTRY_START_DIGIT)
  62.         return -1;
  63.     int phone_len = (phone_num[1] == MOBILE_DIGIT) ? MOBILE_LEGNTH : HOME_LENGTH;
  64.     for (int i = 0; i < phone_len; i++)
  65.         if (!((MIN_DIGIT <= phone_num[i]) && (phone_num[i] <= MAX_DIGIT))) // if temp[i] is not in range.
  66.             return -1;
  67.     for (int i = 0; i < (*phonebook_len); i++)
  68.         if (!(strcmp(phonebook[i], phone_num)))
  69.             return 1;
  70.     return 0;
  71. }
  72.  
  73. void printError(int error) {
  74.     if (error == 1)
  75.         printf("The number already exists\n");
  76.     else
  77.         if (error == -1)
  78.             printf("The number is invalid\n");
  79. }
  80.  
  81. char** add_number(char* phonebook[], int* phonebook_len) {
  82.     printf("Please enter a number: ");
  83.     char new_phone_num[11];
  84.     scanf("%s", new_phone_num);
  85.     int res = check_number(phonebook, phonebook_len, new_phone_num);
  86.     if (res) {
  87.         printError(res);
  88.         return NULL;
  89.     }
  90.     char** bigger_phonebook = get_bigger_phonebook(phonebook, phonebook_len);
  91.     strcpy(bigger_phonebook[*phonebook_len - 1], new_phone_num);
  92.     return bigger_phonebook;
  93. }
  94.  
  95. char** get_bigger_phonebook(char* old_phonebook[], int* phonebook_len) {
  96.     (*phonebook_len)++;
  97.     char** new_phonebook = (char**)malloc((*phonebook_len) * sizeof(char*));
  98.        
  99.     for (int i = 0; i < (*phonebook_len) - 1; i++) {
  100.         new_phonebook[i] = (char**)malloc(MALLOC_MAX_DIGITS*sizeof(char*));
  101.         strcpy(new_phonebook[i], old_phonebook[i]);
  102.         free(old_phonebook[i]);
  103.     }
  104.     new_phonebook[(*phonebook_len) - 1] = (char**)malloc(MALLOC_MAX_DIGITS * sizeof(char*));
  105.  
  106.     return new_phonebook;
  107. }
  108.  
  109.  
  110. void main() {
  111.     int user_choice = 0;
  112.     int phonebook_len = 0;
  113.     int* p_len = &phonebook_len;
  114.     char** phonebook = 0;
  115.     char** flag = 0;
  116.     printf("Weolcome to the Phone Book!\n");
  117.     while (1) {
  118.         printf("1. Add number\n2. Remove number\n3. Statistics\n4. Clear\nPlease select an action: ");
  119.         scanf("%d", &user_choice);
  120.         switch (user_choice) {
  121.         case 1:
  122.             flag = add_number(phonebook, p_len);
  123.             if (flag)
  124.                 phonebook = flag;
  125.             break;
  126.         case 2:
  127.             delete_number(phonebook, p_len);
  128.             break;
  129.         case 3:
  130.             break;
  131.         case 4:
  132.             delete_phonebook(phonebook, p_len);
  133.             break;
  134.         default:
  135.             if (user_choice == 'q' || user_choice == 'Q')
  136.                 break;
  137.             else;
  138.             }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement