Advertisement
Guest User

rubrica.c

a guest
Jan 17th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.30 KB | None | 0 0
  1. /* rubrica.c
  2.  * Si realizzi un programma in linguaggio C in grado di gestire una rubrica di nomi e numeritelefonici.
  3.  * La rubrica deve contenere fino a 100 voci diverse.
  4.  * Ciascuna voce è composta daun nome (max 40 caratteri) e da un numero di telefono (max 20 caratteri).
  5.  * Il programma deve fornire all’utente un menù di scelta, con le seguenti voci:
  6.  *  1) Aggiungi nuova voce in rubrica
  7.  *  2) Ricerca esatta per nome
  8.  *  3) Ricerca approssimata per nome
  9.  *  4) Stampa completa rubrica
  10.  *  0) Esci dal programma
  11.  *  Una volta che l’utente ha scelto l’operazione desiderata (1-4),
  12.  *  il programma acquisirà idati necessari dall’utente ed eseguirà il comando.
  13.  *  Nota: nella rubrica non possono esistere due voci con lo stesso nome.
  14.  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <malloc.h>
  19.  
  20. #define MAX_NAME 40 /* max char for name */
  21. #define MAX_PHONE 20 /* max char for phone */
  22. #define MAX_CONTACTS 100 /* max contacts */
  23.  
  24.  
  25. /* define for portability clear screen command */
  26. #if defined(_WIN32) || defined(_WIN64)
  27.     #define CLEAR_CMD "cls"
  28. #elif defined(__unix__) || defined(__linux__)
  29.     #define CLEAR_CMD "clear"
  30. #endif
  31.  
  32. /* contact info */
  33. typedef struct info {
  34.     char name[MAX_NAME];
  35.     char phone[MAX_PHONE];
  36. }t_info;
  37.  
  38. /* contact list */
  39. typedef struct contact {
  40.     t_info info;
  41.     struct contact *next;
  42. }t_contact;
  43.  
  44. /* using in search_contact, define search type */
  45. typedef enum {
  46.     EXACT = 0,
  47.     APPROXIMATION
  48. }t_search_type;
  49.  
  50. /* Command set */
  51. typedef enum {
  52.     EXIT = 0,
  53.     ADD_CONTACT,
  54.     SEARCH_EXACT,
  55.     SEARCH_STR,
  56.     PRINT_ALL
  57. }t_cmd;
  58.  
  59. /************************************/
  60. int show_menu(void);
  61. t_contact *add_contact(t_contact **c);
  62. t_info *search_name(t_contact *c, char *name);
  63. void search_contact(t_contact *c, t_search_type st);
  64. void print_all_contacts(t_contact *c);
  65. int count_contacts(t_contact *c);
  66.  
  67. int contacts_is_empty(t_contact *c) {
  68.     return (c == NULL) ? 1 : 0;
  69. }
  70.  
  71. void get_key() {
  72.     fflush(stdin);
  73.     getchar();
  74. }
  75. /************************************/
  76.  
  77. int main(int argc, char **argv)
  78. {
  79.     int act;
  80.     t_contact *contacts = NULL;
  81.  
  82.     while((act = show_menu()) != EXIT)
  83.     {
  84.         switch(act)
  85.         {
  86.         case ADD_CONTACT:
  87.             contacts = add_contact(&contacts);
  88.             break;
  89.         case SEARCH_EXACT:
  90.             search_contact(contacts, EXACT);
  91.             break;
  92.         case SEARCH_STR:
  93.             search_contact(contacts, APPROXIMATION);
  94.             break;
  95.         case PRINT_ALL:
  96.             print_all_contacts(contacts);
  97.             break;
  98.         }
  99.         fflush(stdin);
  100.     }
  101.     return 0;
  102. }
  103.  
  104. int show_menu(void)
  105. {
  106.     int act;
  107.     system(CLEAR_CMD);
  108.     printf("\tRUBRICA TELEFONICA\n");
  109.     printf("1) Aggiungi nuova voce in rubrica\n");
  110.     printf("2) Ricerca esatta per nome\n");
  111.     printf("3) Ricerca approssimata per nome\n");
  112.     printf("4) Stampa completa rubrica\n");
  113.     printf("0) Esci dal programma\n");
  114.     scanf("%d", &act);
  115.     system(CLEAR_CMD);
  116.     return act;
  117. }
  118.  
  119. t_contact *add_contact(t_contact **c)
  120. {
  121.     t_contact *tmp_c = NULL;
  122.  
  123.     if(count_contacts(*c) == MAX_CONTACTS) {
  124.         system(CLEAR_CMD);
  125.         printf("\nErrore: la rubrica e' piena.\n\n");
  126.         get_key();
  127.         return *c;
  128.     }
  129.  
  130.     tmp_c = (t_contact*)malloc(sizeof(struct contact));
  131.  
  132.     if(tmp_c == NULL) {
  133.         system(CLEAR_CMD);
  134.         printf("\nErrore: malloc in add_contact\n");
  135.         get_key();
  136.         return *c;
  137.     }
  138.  
  139.     if (contacts_is_empty(*c)) {
  140.         tmp_c->next = NULL;
  141.     } else {
  142.         tmp_c->next = *c;
  143.     }
  144.     system(CLEAR_CMD);
  145.     fflush(stdin);
  146.     printf("\nInserisci il NOME (lunghezza max. %d):  ", MAX_NAME);
  147.     gets(tmp_c->info.name);
  148.  
  149.     if (search_name(*c, tmp_c->info.name) != NULL) {
  150.         system(CLEAR_CMD);
  151.         printf("\nNome gia' presente, scegliere un altro nome\n\n");
  152.         get_key();
  153.         return *c;
  154.     }
  155.     fflush(stdin);
  156.     printf("\nInserisci il TELEFONO (lunghezza max. %d):  ", MAX_PHONE);
  157.     gets(tmp_c->info.phone);
  158.  
  159.     return (*c = tmp_c);
  160. }
  161.  
  162. t_info *search_name(t_contact *c, char *name)
  163. {
  164.     t_contact *tmp = c;
  165.     while(tmp != NULL) {
  166.         if(strcmp(name, c->info.name) == 0) {
  167.             return &(c->info);
  168.         }
  169.         tmp = tmp->next;
  170.     }
  171.     return NULL;
  172. }
  173.  
  174. void search_contact(t_contact *c, t_search_type st)
  175. {
  176.     char name[MAX_NAME];
  177.     int i = 0;
  178.     t_info *info;
  179.  
  180.     if(contacts_is_empty(c)){
  181.         printf("\nLa rubrica e' vuota !\n\n");
  182.         get_key();
  183.         system(CLEAR_CMD);
  184.         return;
  185.     }
  186.  
  187.     if(APPROXIMATION == st)
  188.         printf("\nInserire nome approssimato: ");
  189.     else
  190.         printf("\nInserire nome esatto: ");
  191.  
  192.     fflush(stdin);
  193.     gets(name);
  194.     system(CLEAR_CMD);
  195.  
  196.     if(strlen(name) > 0) {
  197.         while(c != NULL) {
  198.             if (st == APPROXIMATION) {
  199.                 if(strstr(c->info.name, name) != NULL) {
  200.                     printf("Contatto trovato: Nome: %s\tNumero: %s\n", c->info.name, c->info.phone);
  201.                     i++;
  202.                 }
  203.             } else if (st == EXACT) {
  204.                 if((info = search_name(c, name)) != NULL) {
  205.                     printf("Contatto trovato: Nome: %s\tNumero: %s\n", info->name, info->phone);
  206.                     i++;
  207.                 }
  208.             }
  209.             c = c->next;
  210.         }
  211.         if (i == 0) {
  212.             printf("\nNessuna corrispondenza trovata\n\n");
  213.         }
  214.         printf("\n");
  215.         get_key();
  216.     }
  217. }
  218.  
  219. void print_all_contacts(t_contact *c)
  220. {
  221.     int i = 1;
  222.     t_contact *tmp = c;
  223.  
  224.     if(contacts_is_empty(tmp)){
  225.         printf("\nLa rubrica e' vuota !\n\n");
  226.         get_key();
  227.         system(CLEAR_CMD);
  228.         return;
  229.     }
  230.     system(CLEAR_CMD);
  231.  
  232.     while(tmp != NULL) {
  233.         printf("Contatto numero %d", i++);
  234.         printf("\tNome: %s", tmp->info.name);
  235.         printf("\tTelefono: %s\n", tmp->info.phone);
  236.         tmp = tmp->next;
  237.     }
  238.     printf("\nTOTALE CONTATTI: %d\n\n", i-1);
  239.     get_key();
  240. }
  241.  
  242. int count_contacts(t_contact *c)
  243. {
  244.     t_contact *tmp = c;
  245.     int len = 0;
  246.  
  247.     if(contacts_is_empty(tmp))
  248.         return 0;
  249.  
  250.     while(tmp != NULL) {
  251.         len = len + 1;
  252.         tmp = tmp->next;
  253.     }
  254.     return len;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement