| 1 | /* mudruto1 DU4 |
| 2 | * sestaveni: gcc tel.c -o tel; nebo: make tel; |
| 3 | * spusteni : ./tel |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | //LinkedList + Funkce |
| 10 | typedef struct { |
| 11 | struct ll *next; |
| 12 | void *data; |
| 13 | } ll; |
| 14 | |
| 15 | ll *ll_new() { |
| 16 | ll *list = (ll*) malloc(sizeof(ll)); |
| 17 | if(!list) exit(1); |
| 18 | list->next = 0; |
| 19 | list->data = 0; |
| 20 | return list; |
| 21 | } |
| 22 | |
| 23 | void ll_add(ll *list, void *ptr) { |
| 24 | for(;list->next != 0;list = (ll *) list->next); |
| 25 | list->next = (struct ll *) ll_new(); |
| 26 | list->data = (void *) ptr; |
| 27 | } |
| 28 | |
| 29 | void ll_rm(ll **llptr) { |
| 30 | ll *freeptr = *llptr; |
| 31 | *llptr = (ll *) (*llptr)->next; |
| 32 | free(freeptr); |
| 33 | } |
| 34 | |
| 35 | void ll_walk(ll *list, void (*function)(ll *ptr)) { |
| 36 | for(;list->next != 0;list = (ll *) list->next) function(list); |
| 37 | } |
| 38 | |
| 39 | void ll_walk_data(ll *list, void (*function)(void *ptr)) { |
| 40 | for(;list->next != 0;list = (ll *) list->next) if(list->data != 0) function(list->data); |
| 41 | } |
| 42 | |
| 43 | //Kontakt + Funkce |
| 44 | typedef struct { |
| 45 | char name[16]; |
| 46 | char sname[16]; |
| 47 | char telno[16]; |
| 48 | } contact; |
| 49 | |
| 50 | contact *contact_new() { |
| 51 | contact *c = (contact*) malloc(sizeof(contact)); |
| 52 | if(!c) exit(1); |
| 53 | scanf("%15s", c->name); |
| 54 | if(c->name[0] == '.') return 0; |
| 55 | scanf("%15s", c->sname); |
| 56 | scanf("%15s", c->telno); |
| 57 | return c; |
| 58 | } |
| 59 | |
| 60 | void contact_print(contact *c) { |
| 61 | printf("%-15s %-15s %-15s\n", c->name, c->sname, c->telno); |
| 62 | } |
| 63 | |
| 64 | char contact_match(contact *c, char *str) { //vrati true, kdyz kontakt odpovida |
| 65 | return !(strcmp(c->name, str) && strcmp(c->sname, str) && strcmp(c->telno, str)); |
| 66 | } |
| 67 | |
| 68 | char contact_compare(contact *a, contact *b) { //vrati true, kdyz jsou kontakty stejne |
| 69 | return !(strcmp(a->name, b->name) || strcmp(a->sname, b->sname) || strcmp(a->telno, b->telno)); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | char find_string[64] = ""; |
| 74 | void contact_find(ll *item) { |
| 75 | if(contact_match(item->data, find_string)) { |
| 76 | contact_print(item->data); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void contact_remove(ll **llptr) { |
| 81 | //bohuzel nejde pouzit contact_find + ll_walk protoze je treba mit referenci na prvni polozku pro pripad potreby jejiho odstraneni |
| 82 | while((*llptr)->next != 0) { |
| 83 | if(contact_match((*llptr)->data, find_string)) { |
| 84 | printf("DELETED: "); contact_print((*llptr)->data); |
| 85 | ll_rm(llptr); |
| 86 | return; |
| 87 | } |
| 88 | llptr = (ll **) &((*llptr)->next); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | int main(void) { |
| 95 | |
| 96 | /* |
| 97 | //ukazka pouziti linked-listu |
| 98 | ll *list = ll_new(); |
| 99 | ll_add(list, "foo"); |
| 100 | ll_add(list, "bar"); |
| 101 | ll_walk_data(list, puts); |
| 102 | ll_rm(&list); |
| 103 | puts("first item removed..."); |
| 104 | ll_walk_data(list, puts); |
| 105 | */ |
| 106 | |
| 107 | /* |
| 108 | //ukazka pouziti kontaktu |
| 109 | contact *c = contact_new(); |
| 110 | if(!c) exit(2); |
| 111 | contact_print(c); |
| 112 | */ |
| 113 | |
| 114 | puts( |
| 115 | "\n\tTelefonni seznam 0.1 (mudruto1)\n" |
| 116 | "Zadavejte kontakty ve formatu 'jmeno prijmeni cislo' kazdy na novou radku.\n" |
| 117 | "Po zadani posledniho kontaktu zadejte radku zacinajici teckou.\n" |
| 118 | "priklad:\n" |
| 119 | "tomas mudrunka 123456789\n" |
| 120 | "daniel novak 987654321\n" |
| 121 | "alice obrovska 456789123\n" |
| 122 | ".\n" |
| 123 | ); |
| 124 | |
| 125 | ll *seznam = (ll *) ll_new(); |
| 126 | contact *c; |
| 127 | while(c = contact_new()) ll_add(seznam, c); |
| 128 | puts( |
| 129 | "------------------------------------------\n" |
| 130 | "jmeno prijmeni cislo\n" |
| 131 | ); |
| 132 | ll_walk_data(seznam, contact_print); |
| 133 | |
| 134 | puts("\n\nCo hledat?"); |
| 135 | scanf("%15s", find_string); |
| 136 | ll_walk(seznam, contact_find); |
| 137 | |
| 138 | puts("\nCo smazat?"); |
| 139 | scanf("%15s", find_string); |
| 140 | contact_remove(&seznam); |
| 141 | |
| 142 | puts("\nTakto vypada seznam bez smazane polozky:\n"); |
| 143 | ll_walk_data(seznam, contact_print); |
| 144 | } |
| 145 | |