Advertisement
arfin97

DS Project - Phonebook

Dec 11th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9. void welcome(){
  10. printf("\n\n\n\n\n\n\n\n\n\n\n\n");
  11. printf("-------------------------------------------------------------------------------------------------------------\n");
  12. printf("\t\t\t\t\t\ Welcome to The Phone Book \n");
  13. printf("\t\t\t\t\t\ Name: Md. Hafizur Rahman Arfin\n");
  14. printf("\t\t\t\t\t\ ID: 161-15-something\n");
  15. printf("-------------------------------------------------------------------------------------------------------------\n");
  16. }
  17.  
  18. struct Node{
  19.     char name[100];
  20.     char phone[100];
  21.     char email[100];
  22.     struct Node *next;
  23. };
  24. typedef struct Node node;
  25. int speed[10]; int speedid = 0;
  26. //Functions prototypes;
  27. void printnode(node *temp);
  28. node *nameorlist(node *list);
  29. void add(node *list, char name[], char phone[], char email[]);
  30. void addtolog(node *list, char name[], char phone[], char email[]);
  31. node *searchbyid(node *list, int n);
  32. node *searchbyname(node *list, char name[]);
  33. //menu function - only to print the menu;
  34.  
  35. void menu(){
  36.     printf("\t\t\t\t\t\ 1. Show contacts list\n");
  37.     printf("\t\t\t\t\t\ 2. Call\n");
  38.     printf("\t\t\t\t\t\ 3. Call log\n");
  39.     printf("\t\t\t\t\t\ 4. Speed dial\n");
  40.     printf("\t\t\t\t\t\ 5. Add a new contact\n");
  41.     printf("\t\t\t\t\t\ 6. Update contact\n");
  42.     printf("\t\t\t\t\t\ 7. Delete contact\n");
  43.     printf("\t\t\t\t\t\ 0. Exit\n");
  44.     printf(">> ");
  45. }
  46.  
  47. //1. Show contacts list; = show();
  48. void show(node *list){
  49.     printf("\n-------------------------------------------------------------------------------------------------------------\n");
  50.     list = list->next;
  51.     if(list == NULL){
  52.         printf("\nThere is no contract in the list\n\n");
  53.         printf("\n-------------------------------------------------------------------------------------------------------------\n\n");
  54.         return;
  55.     }
  56.     int i = 1;
  57.     while(list != NULL){
  58.         printf("\t\t [%d]----------\n", i);
  59.         printf("\t\t Name: %s\n", list->name);
  60.         printf("\t\t Phone: %s\n", list->phone);
  61.         printf("\t\t Email: %s\n", list->email);
  62.         list = list->next;
  63.         i++;
  64.     }
  65.     printf("\n-------------------------------------------------------------------------------------------------------------\n\n");
  66.     return;
  67. }
  68.  
  69. //3. Call log
  70. void call(node *list, node *log){
  71.     node *temp = nameorlist(list);
  72.     //Calling...
  73.  
  74.     printf("\t Calling %s", temp->name);
  75.     printf(" -%s", temp->phone); Sleep(300);
  76.     printf("."); Sleep(300);
  77.     printf("."); Sleep(300);
  78.     printf("."); Sleep(300);
  79.     printf("\n\n");
  80.  
  81.     //Call log linked list;
  82.     addtolog(log, temp->name, temp->phone, temp->email);
  83.     printf("\t Number busy :(\n\n");
  84.     Sleep(500);
  85. }
  86.  
  87. //4. Speed dial;
  88. void speeddial(node *list, node *log){
  89.     speedid = 0;
  90.     printf("\t 1. Call by speed dial\n");
  91.     printf("\t 2. Add contact to speed dial\n");
  92.     printf("\t >> ");
  93.     int choice; int n;
  94.     node *temp;
  95.     scanf("%d", &choice);
  96.     switch(choice){
  97.         case 1:{
  98.             printf("\t Press the key\n");
  99.             printf("\t >> ");
  100.             scanf("%d", &n);
  101.             if(speed[n] != 0){
  102.                 temp = searchbyid(list, speed[n]);
  103.                 printf("\t %s -", temp->name); Sleep(300);
  104.                 printf(" %s\n\n", temp->phone);
  105.                 printf("\t Calling %s -", temp->name); Sleep(300);
  106.                 printf("."); Sleep(300); printf("."); Sleep(300); printf("."); Sleep(300);
  107.                 //Call log linked list;
  108.                 addtolog(log, temp->name, temp->phone, temp->email);
  109.                 printf("\t Number busy :(\n\n");
  110.                 Sleep(500);
  111.             }
  112.             else{
  113.                 printf("\t No number was assigned with this key\n\n");
  114.             }
  115.             break;
  116.         }
  117.         case 2:{
  118.             printf("\t Please enter the key you want to assign number with(0-9)\n");
  119.             printf("\t >> ");
  120.             scanf("%d", &n);
  121.             temp = nameorlist(list);
  122.             speed[n] = speedid;
  123.             printf("\t %s is assinged to key %d\n\n", temp->name, n);
  124.             break;
  125.         }
  126.     }
  127.     speedid = 0;
  128. }
  129.  
  130. //5. Add a new contact = add;
  131. void add(node *list, char name[], char phone[], char email[]){
  132.     node *temp = (node *)malloc(sizeof(node));
  133.     strcpy(temp->name, name);
  134.     strcpy(temp->phone, phone);
  135.     strcpy(temp->email, email);
  136.     temp->next = NULL;
  137.  
  138.     while(list->next != NULL){
  139.         list = list->next;
  140.     }
  141.     list->next = temp;
  142.     return;
  143. }
  144.  
  145. //3. add to log
  146. void addtolog(node *list, char name[], char phone[], char email[]){
  147.     node *temp = (node *)malloc(sizeof(node));
  148.     strcpy(temp->name, name);
  149.     strcpy(temp->phone, phone);
  150.     strcpy(temp->email, email);
  151.     temp->next = NULL;
  152.  
  153.     temp->next = list->next;
  154.     list->next = temp;
  155.     return;
  156. }
  157. //search node by name;
  158. node *searchbyname(node *list, char name[]){
  159.     speedid--;
  160.     list = list->next;
  161.     if(list == NULL){
  162.         printf("\nThere is no contract in the list\n");
  163.         return NULL;
  164.     }
  165.     while(strcmp(list->name, name) != 0){
  166.         list = list->next;
  167.         speedid++;
  168.     }
  169.     return list;
  170. }
  171.  
  172. //search by id;
  173. node *searchbyid(node *list, int n){
  174.     for(int i = 0; i < n; i++){
  175.         list = list->next;
  176.         speedid++;
  177.     }
  178.     return list;
  179. }
  180.  
  181. //Search or show the list;
  182. node *nameorlist(node *list){
  183.     printf("\t 1. Search by name\n");
  184.     printf("\t 2. Show list\n");
  185.     printf("\t >> ");
  186.  
  187.     int choice;
  188.     node *temp;
  189.     scanf("%d", &choice);
  190.     switch(choice){
  191.         // if user choosed search by name update option;
  192.         case 1:{
  193.             char name[100];
  194.             printf("\tPlease enter the name: ");
  195.             scanf(" %[^\n]", name);
  196.             temp = searchbyname(list, name);
  197.             printf("\n");
  198.             break;
  199.         }
  200.         case 2:{
  201.             show(list);
  202.             printf("\tPlease choose the id of contact: ");
  203.             int n;
  204.             scanf("%d", &n);
  205.             temp = searchbyid(list, n);
  206.             printf("\n");
  207.             break;
  208.         }
  209.     }
  210.     return temp;
  211. }
  212.  
  213. //6. Update contact;
  214. void update(node *list){
  215.     node *temp = nameorlist(list);
  216.     if(temp == NULL){
  217.         printf("\t Contact could not found\n\n");
  218.     }
  219.     printf("\tContact To be edited\n\n");
  220.     printnode(temp);
  221.     printf("\t 1. Change name\n");
  222.     printf("\t 2. Change phone\n");
  223.     printf("\t 3. Change email\n");
  224.     printf("\t >> ");
  225.     int choice;
  226.     scanf("%d", &choice);
  227.     switch(choice){
  228.         case 1:{
  229.             printf("\t Enter new name: ");
  230.             char name[100]; scanf(" %[^\n]", name);
  231.             strcpy(temp->name, name);
  232.             break;
  233.         }
  234.         case 2:{
  235.             printf("\t Enter new phone number: ");
  236.             char phone[100]; scanf(" %[^\n]", phone);
  237.             strcpy(temp->phone, phone);
  238.             break;
  239.         }
  240.         case 3:{
  241.             printf("\t Enter new email: ");
  242.             char email[100]; scanf(" %[^\n]", email);
  243.             strcpy(temp->email, email);
  244.             break;
  245.         }
  246.     }
  247.     printf("\t Contract updated successfully...\n\n");
  248. }
  249.  
  250. void del(node *list){
  251.     node *temp = nameorlist(list);
  252.     printf("\tContact To be deleted\n\n");
  253.     printnode(temp);
  254.     printf("proceed? (y/n): ");
  255.     char ch; scanf(" %c", &ch);
  256.     if(ch == 'y'){
  257.         while(strcmp(list->next->name, temp->name) != 0){
  258.             list = list->next;
  259.         }
  260.         node *temp2 = list->next;
  261.         list->next = list->next->next;
  262.         free(temp2);
  263.         printf("\t Contract deleted successfully...\n\n");
  264.     }
  265.     else if(ch == 'n'){
  266.         printf("\t Contract was not deleted...\n\n");
  267.         return;
  268.     }
  269. }
  270.  
  271. //prints the node passed to it;
  272. void printnode(node *temp){
  273.     printf("\tName: %s\n", temp->name);
  274.     printf("\tPhone: %s\n", temp->phone);
  275.     printf("\tEmail: %s\n", temp->email);
  276.     printf("\n");
  277. }
  278.  
  279.  
  280. int main(){
  281.     system("color F9");
  282.     welcome();
  283.     Sleep(2000);
  284.     system("cls");
  285.     //creating dummy node;
  286.     node *list = (node *)malloc(sizeof(node));
  287.     list->next = NULL;
  288.     //crating dummy node for log list;
  289.     node *log = (node *)malloc(sizeof(node));
  290.     log->next = NULL;
  291.     //showing the menu;
  292.     while(true){
  293.         menu();
  294.         int choice;
  295.         scanf("%d", &choice);
  296.         switch(choice){
  297.         case 1:
  298.             show(list);
  299.             break;
  300.         case 2: //2. Call
  301.             call(list, log);
  302.             break;
  303.         case 3: //3. Call log
  304.             printf("Call log.\n");
  305.             show(log);
  306.             break;
  307.         case 4://4. Speed dial
  308.             speeddial(list, log);
  309.             break;
  310.         case 5: //Add new contract
  311.             char name[100]; char phone[100]; char email[100];
  312.             printf("\t Name: ");  scanf(" %[^\n]", name);
  313.             printf("\t Phone: "); scanf(" %[^\n]", phone);
  314.             printf("\t Email: "); scanf(" %[^\n]", email);
  315.             add(list, name, phone, email);
  316.             printf("\t Contact Saved Successfully...\n\n");
  317.             break;
  318.         case 6: //Update
  319.             update(list);
  320.             break;
  321.         case 7: //Delete contact
  322.             del(list);
  323.             break;
  324.         case 0:
  325.             return 0;
  326.             break;
  327.         }
  328.     }
  329.     return 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement