Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* rubrica.c
- * Si realizzi un programma in linguaggio C in grado di gestire una rubrica di nomi e numeritelefonici.
- * La rubrica deve contenere fino a 100 voci diverse.
- * Ciascuna voce è composta daun nome (max 40 caratteri) e da un numero di telefono (max 20 caratteri).
- * Il programma deve fornire all’utente un menù di scelta, con le seguenti voci:
- * 1) Aggiungi nuova voce in rubrica
- * 2) Ricerca esatta per nome
- * 3) Ricerca approssimata per nome
- * 4) Stampa completa rubrica
- * 0) Esci dal programma
- * Una volta che l’utente ha scelto l’operazione desiderata (1-4),
- * il programma acquisirà idati necessari dall’utente ed eseguirà il comando.
- * Nota: nella rubrica non possono esistere due voci con lo stesso nome.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #define MAX_NAME 40 /* max char for name */
- #define MAX_PHONE 20 /* max char for phone */
- #define MAX_CONTACTS 100 /* max contacts */
- /* define for portability clear screen command */
- #if defined(_WIN32) || defined(_WIN64)
- #define CLEAR_CMD "cls"
- #elif defined(__unix__) || defined(__linux__)
- #define CLEAR_CMD "clear"
- #endif
- /* contact info */
- typedef struct info {
- char name[MAX_NAME];
- char phone[MAX_PHONE];
- }t_info;
- /* contact list */
- typedef struct contact {
- t_info info;
- struct contact *next;
- }t_contact;
- /* using in search_contact, define search type */
- typedef enum {
- EXACT = 0,
- APPROXIMATION
- }t_search_type;
- /* Command set */
- typedef enum {
- EXIT = 0,
- ADD_CONTACT,
- SEARCH_EXACT,
- SEARCH_STR,
- PRINT_ALL
- }t_cmd;
- /************************************/
- int show_menu(void);
- t_contact *add_contact(t_contact **c);
- t_info *search_name(t_contact *c, char *name);
- void search_contact(t_contact *c, t_search_type st);
- void print_all_contacts(t_contact *c);
- int count_contacts(t_contact *c);
- int contacts_is_empty(t_contact *c) {
- return (c == NULL) ? 1 : 0;
- }
- void get_key() {
- fflush(stdin);
- getchar();
- }
- /************************************/
- int main(int argc, char **argv)
- {
- int act;
- t_contact *contacts = NULL;
- while((act = show_menu()) != EXIT)
- {
- switch(act)
- {
- case ADD_CONTACT:
- contacts = add_contact(&contacts);
- break;
- case SEARCH_EXACT:
- search_contact(contacts, EXACT);
- break;
- case SEARCH_STR:
- search_contact(contacts, APPROXIMATION);
- break;
- case PRINT_ALL:
- print_all_contacts(contacts);
- break;
- }
- fflush(stdin);
- }
- return 0;
- }
- int show_menu(void)
- {
- int act;
- system(CLEAR_CMD);
- printf("\tRUBRICA TELEFONICA\n");
- printf("1) Aggiungi nuova voce in rubrica\n");
- printf("2) Ricerca esatta per nome\n");
- printf("3) Ricerca approssimata per nome\n");
- printf("4) Stampa completa rubrica\n");
- printf("0) Esci dal programma\n");
- scanf("%d", &act);
- system(CLEAR_CMD);
- return act;
- }
- t_contact *add_contact(t_contact **c)
- {
- t_contact *tmp_c = NULL;
- if(count_contacts(*c) == MAX_CONTACTS) {
- system(CLEAR_CMD);
- printf("\nErrore: la rubrica e' piena.\n\n");
- get_key();
- return *c;
- }
- tmp_c = (t_contact*)malloc(sizeof(struct contact));
- if(tmp_c == NULL) {
- system(CLEAR_CMD);
- printf("\nErrore: malloc in add_contact\n");
- get_key();
- return *c;
- }
- if (contacts_is_empty(*c)) {
- tmp_c->next = NULL;
- } else {
- tmp_c->next = *c;
- }
- system(CLEAR_CMD);
- fflush(stdin);
- printf("\nInserisci il NOME (lunghezza max. %d): ", MAX_NAME);
- gets(tmp_c->info.name);
- if (search_name(*c, tmp_c->info.name) != NULL) {
- system(CLEAR_CMD);
- printf("\nNome gia' presente, scegliere un altro nome\n\n");
- get_key();
- return *c;
- }
- fflush(stdin);
- printf("\nInserisci il TELEFONO (lunghezza max. %d): ", MAX_PHONE);
- gets(tmp_c->info.phone);
- return (*c = tmp_c);
- }
- t_info *search_name(t_contact *c, char *name)
- {
- t_contact *tmp = c;
- while(tmp != NULL) {
- if(strcmp(name, c->info.name) == 0) {
- return &(c->info);
- }
- tmp = tmp->next;
- }
- return NULL;
- }
- void search_contact(t_contact *c, t_search_type st)
- {
- char name[MAX_NAME];
- int i = 0;
- t_info *info;
- if(contacts_is_empty(c)){
- printf("\nLa rubrica e' vuota !\n\n");
- get_key();
- system(CLEAR_CMD);
- return;
- }
- if(APPROXIMATION == st)
- printf("\nInserire nome approssimato: ");
- else
- printf("\nInserire nome esatto: ");
- fflush(stdin);
- gets(name);
- system(CLEAR_CMD);
- if(strlen(name) > 0) {
- while(c != NULL) {
- if (st == APPROXIMATION) {
- if(strstr(c->info.name, name) != NULL) {
- printf("Contatto trovato: Nome: %s\tNumero: %s\n", c->info.name, c->info.phone);
- i++;
- }
- } else if (st == EXACT) {
- if((info = search_name(c, name)) != NULL) {
- printf("Contatto trovato: Nome: %s\tNumero: %s\n", info->name, info->phone);
- i++;
- }
- }
- c = c->next;
- }
- if (i == 0) {
- printf("\nNessuna corrispondenza trovata\n\n");
- }
- printf("\n");
- get_key();
- }
- }
- void print_all_contacts(t_contact *c)
- {
- int i = 1;
- t_contact *tmp = c;
- if(contacts_is_empty(tmp)){
- printf("\nLa rubrica e' vuota !\n\n");
- get_key();
- system(CLEAR_CMD);
- return;
- }
- system(CLEAR_CMD);
- while(tmp != NULL) {
- printf("Contatto numero %d", i++);
- printf("\tNome: %s", tmp->info.name);
- printf("\tTelefono: %s\n", tmp->info.phone);
- tmp = tmp->next;
- }
- printf("\nTOTALE CONTATTI: %d\n\n", i-1);
- get_key();
- }
- int count_contacts(t_contact *c)
- {
- t_contact *tmp = c;
- int len = 0;
- if(contacts_is_empty(tmp))
- return 0;
- while(tmp != NULL) {
- len = len + 1;
- tmp = tmp->next;
- }
- return len;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement