SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <stdio.h> | |
| 2 | #include <stdlib.h> | |
| 3 | #include <string.h> | |
| 4 | #define FLUSH while(getchar()!='\n') | |
| 5 | #define S 15 | |
| 6 | ||
| 7 | typedef short boolean; | |
| 8 | ||
| 9 | typedef struct {
| |
| 10 | char NomeMateria[S], codice[S]; | |
| 11 | unsigned int crediti, giorno, mese, anno, voto; | |
| 12 | } tipobaseABR; | |
| 13 | ||
| 14 | void LeggiStringa(char s[], unsigned int dim) {
| |
| 15 | unsigned int i; | |
| 16 | for(i=0; i<dim-1; i++) | |
| 17 | if((s[i]=getchar())=='\n') | |
| 18 | break; | |
| 19 | if(i==dim-1) while(getchar()!='\n'); | |
| 20 | s[i]='\0'; | |
| 21 | } | |
| 22 | ||
| 23 | void LeggiElementoABR(tipobaseABR *x) {
| |
| 24 | printf("\nInserisci il nome della materia: ");
| |
| 25 | LeggiStringa(x->NomeMateria, S); | |
| 26 | printf("\nInserisci il codice della materia: ");
| |
| 27 | LeggiStringa(x->codice, S); | |
| 28 | printf("\nInserisci il numero di crediti: ");
| |
| 29 | scanf("%u", &(x)->crediti);
| |
| 30 | FLUSH; | |
| 31 | printf("\nInserisci la data dell'esame:\n");
| |
| 32 | ||
| 33 | do {
| |
| 34 | printf("\nInserisci il giorno: ");
| |
| 35 | scanf("%u", &(x)->giorno);
| |
| 36 | FLUSH; | |
| 37 | } while(x->giorno>31||x->giorno<1); | |
| 38 | ||
| 39 | do {
| |
| 40 | printf("\nInserisci il mese: ");
| |
| 41 | scanf("%u", &(x)->mese);
| |
| 42 | FLUSH; | |
| 43 | } while(x->mese>12||x->mese<1); | |
| 44 | ||
| 45 | do {
| |
| 46 | printf("\nInserisci l'anno: ");
| |
| 47 | scanf("%u", &(x)->anno);
| |
| 48 | FLUSH; | |
| 49 | } while(x->anno<1); | |
| 50 | ||
| 51 | do {
| |
| 52 | printf("\nInserisci il voto: ");
| |
| 53 | scanf("%u", &(x)->voto);
| |
| 54 | FLUSH; | |
| 55 | } while(x->voto<18 || x->voto>30); | |
| 56 | } | |
| 57 | ||
| 58 | void VisualizzaElementoABR(tipobaseABR x) {
| |
| 59 | printf("\nNome Materia: %s\n", x.NomeMateria);
| |
| 60 | printf("\nCodice Materia: %s\n", x.codice);
| |
| 61 | printf("\nData: %u / %u / %u\n", x.giorno, x.mese, x.anno);
| |
| 62 | printf("\nCrediti: %u\n", x.crediti);
| |
| 63 | printf("\nVoto: %u\n", x.voto);
| |
| 64 | } | |
| 65 | ||
| 66 | int ConfrontaABR(tipobaseABR x, tipobaseABR y) {
| |
| 67 | return(strcmp(x.NomeMateria, y.NomeMateria)); | |
| 68 | } | |
| 69 | ||
| 70 | unsigned int EstraiVoto(tipobaseABR x) {
| |
| 71 | return(x.voto); | |
| 72 | } | |
| 73 | ||
| 74 | ||
| 75 | /* Implementazione_ABR */ | |
| 76 | ||
| 77 | #define ALBEROVUOTO NULL | |
| 78 | ||
| 79 | typedef struct nodoABR {
| |
| 80 | tipobaseABR info; | |
| 81 | struct nodoABR * leftchild, * rightchild; | |
| 82 | } * abr; | |
| 83 | ||
| 84 | void MakeNullABR(abr *a) {
| |
| 85 | *a=ALBEROVUOTO; | |
| 86 | } | |
| 87 | ||
| 88 | boolean EmptyABR(abr a) {
| |
| 89 | return(a==ALBEROVUOTO); | |
| 90 | } | |
| 91 | ||
| 92 | boolean FullABR(abr a) {
| |
| 93 | struct nodoABR * tmp; | |
| 94 | boolean full=0; | |
| 95 | if((tmp=(struct nodoABR *) malloc (sizeof(struct nodoABR)))==ALBEROVUOTO) | |
| 96 | full=1; | |
| 97 | else free(tmp); | |
| 98 | return full; | |
| 99 | } | |
| 100 | ||
| 101 | abr LeftChild(abr a) {
| |
| 102 | if(!EmptyABR(a)) | |
| 103 | return a->leftchild; | |
| 104 | } | |
| 105 | ||
| 106 | abr RightChild(abr a) {
| |
| 107 | if(!EmptyABR(a)) | |
| 108 | return a->rightchild; | |
| 109 | } | |
| 110 | ||
| 111 | tipobaseABR Label(abr a) {
| |
| 112 | if(!EmptyABR(a)) | |
| 113 | return(a->info); | |
| 114 | } | |
| 115 | ||
| 116 | boolean Member(abr a, tipobaseABR x) {
| |
| 117 | if(EmptyABR(a)) return 0; | |
| 118 | else {
| |
| 119 | if(!ConfrontaABR(a->info, x)) | |
| 120 | return 1; | |
| 121 | else if(ConfrontaABR(a->info, x)>0) | |
| 122 | return(Member(a->leftchild, x)); | |
| 123 | else return(Member(a->rightchild, x)); | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | void InsertABR(abr *a, tipobaseABR x) {
| |
| 128 | if(!FullABR(*a)) {
| |
| 129 | if(EmptyABR(*a)) {
| |
| 130 | *a=(struct nodoABR *) malloc (sizeof(struct nodoABR)); | |
| 131 | (*a)->info=x; | |
| 132 | (*a)->rightchild=(*a)->leftchild=ALBEROVUOTO; | |
| 133 | } | |
| 134 | else if(ConfrontaABR((*a)->info, x)>0) | |
| 135 | InsertABR(&(*a)->leftchild, x); | |
| 136 | else | |
| 137 | InsertABR(&(*a)->rightchild, x); | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | /* tipobaseList.h */ | |
| 142 | #define STR 20 | |
| 143 | typedef struct {
| |
| 144 | char cognome[S], nome[S], CodiceFiscale[STR]; | |
| 145 | char residenza[S], matricola[S]; | |
| 146 | abr materie; | |
| 147 | } tipobaseList; | |
| 148 | ||
| 149 | void LeggiElementoList(tipobaseList *x) {
| |
| 150 | printf("\nInserisci il cognome: ");
| |
| 151 | LeggiStringa(x->cognome, S); | |
| 152 | printf("\nInserisci il nome: ");
| |
| 153 | LeggiStringa(x->nome, S); | |
| 154 | printf("\nInserisci il codice fiscale: ");
| |
| 155 | LeggiStringa(x->CodiceFiscale, STR); | |
| 156 | printf("\nInserisci la residenza: ");
| |
| 157 | LeggiStringa(x->residenza, S); | |
| 158 | printf("\nInserisci il numero di matricola: ");
| |
| 159 | LeggiStringa(x->matricola, S); | |
| 160 | MakeNullABR(&(x)->materie); | |
| 161 | } | |
| 162 | ||
| 163 | void CercaCognomeList(tipobaseList *x) {
| |
| 164 | printf("\nInserisci il cognome: ");
| |
| 165 | LeggiStringa(x->cognome, S); | |
| 166 | } | |
| 167 | ||
| 168 | void CercaElementoList(tipobaseList *x) {
| |
| 169 | printf("\nInserisci il cognome: ");
| |
| 170 | LeggiStringa(x->cognome, S); | |
| 171 | printf("\nInserisci il nome: ");
| |
| 172 | LeggiStringa(x->nome, S); | |
| 173 | printf("\nInserisci il codice fiscale: ");
| |
| 174 | LeggiStringa(x->CodiceFiscale, STR); | |
| 175 | } | |
| 176 | ||
| 177 | void VisualizzaElementoList(tipobaseList x) {
| |
| 178 | printf("\nCognome: %s\n", x.cognome);
| |
| 179 | printf("Nome: %s\n", x.nome);
| |
| 180 | printf("Codice Fiscale: %s\n", x.CodiceFiscale);
| |
| 181 | printf("Residenza: %s\n", x.residenza);
| |
| 182 | printf("Numero di matricola: %s\n", x.matricola);
| |
| 183 | } | |
| 184 | ||
| 185 | int ConfrontaList(tipobaseList x, tipobaseList y) {
| |
| 186 | if(!strcmp(x.cognome, y.cognome)) | |
| 187 | if(!strcmp(x.nome, y.nome)) | |
| 188 | return(strcmp(x.CodiceFiscale, y.CodiceFiscale)); | |
| 189 | else return(strcmp(x.nome, y.nome)); | |
| 190 | else return(strcmp(x.cognome, y.cognome)); | |
| 191 | } | |
| 192 | ||
| 193 | int ConfrontaCognome(tipobaseList x, tipobaseList y) {
| |
| 194 | return(strcmp(x.cognome, y.cognome)); | |
| 195 | } | |
| 196 | ||
| 197 | abr EstraiABR(tipobaseList x) {
| |
| 198 | return (x.materie); | |
| 199 | } | |
| 200 | ||
| 201 | void AggiornaABR(tipobaseList *x, abr a) {
| |
| 202 | x->materie=a; | |
| 203 | } | |
| 204 | ||
| 205 | unsigned int Indice(tipobaseList x) {
| |
| 206 | if(x.cognome[0]>='A'&&x.cognome[0]<='Z') | |
| 207 | return(x.cognome[0]-'A'); | |
| 208 | if(x.cognome[0]>='a'&&x.cognome[0]<='z') | |
| 209 | return(x.cognome[0]-'a'); | |
| 210 | return 0; | |
| 211 | } | |
| 212 | ||
| 213 | ||
| 214 | /* Implementazione_list.h */ | |
| 215 | ||
| 216 | #define LISTAVUOTA NULL | |
| 217 | ||
| 218 | typedef struct nodoList {
| |
| 219 | tipobaseList info; | |
| 220 | struct nodoList * next; | |
| 221 | } * list; | |
| 222 | ||
| 223 | typedef list position; | |
| 224 | ||
| 225 | void MakeNullList(list *l) {
| |
| 226 | *l=LISTAVUOTA; | |
| 227 | } | |
| 228 | ||
| 229 | boolean EmptyList(list l) {
| |
| 230 | return(l==LISTAVUOTA); | |
| 231 | } | |
| 232 | ||
| 233 | boolean FullList(list l) {
| |
| 234 | struct nodoList * tmp; | |
| 235 | boolean full=0; | |
| 236 | if((tmp=(struct nodoList *) malloc (sizeof(struct nodoList)))==LISTAVUOTA) | |
| 237 | full=1; | |
| 238 | else free(tmp); | |
| 239 | return full; | |
| 240 | } | |
| 241 | ||
| 242 | tipobaseList Retrieve(list l, position p) {
| |
| 243 | if(!EmptyList(l)) {
| |
| 244 | if(p==LISTAVUOTA) return l->info; | |
| 245 | return (p->next->info); | |
| 246 | } | |
| 247 | } | |
| 248 | ||
| 249 | position First(list l) {
| |
| 250 | return LISTAVUOTA; | |
| 251 | } | |
| 252 | ||
| 253 | position End(list l) {
| |
| 254 | if(EmptyList(l)) return LISTAVUOTA; | |
| 255 | while(l->next!=LISTAVUOTA) | |
| 256 | l=l->next; | |
| 257 | return l; | |
| 258 | } | |
| 259 | ||
| 260 | position Next(list l, position p) {
| |
| 261 | if(!EmptyList(l)) {
| |
| 262 | if(p==LISTAVUOTA) return l; | |
| 263 | return (p->next); | |
| 264 | } | |
| 265 | } | |
| 266 | ||
| 267 | position Locate(list l, tipobaseList x) {
| |
| 268 | if(EmptyList(l)) return LISTAVUOTA; | |
| 269 | if(!ConfrontaList(l->info, x)) return LISTAVUOTA; | |
| 270 | while(l->next!=LISTAVUOTA) | |
| 271 | if(!ConfrontaList(l->next->info, x)) | |
| 272 | break; | |
| 273 | else l=l->next; | |
| 274 | ||
| 275 | return l; | |
| 276 | } | |
| 277 | ||
| 278 | void InsertList(list *l, position p, tipobaseList x) {
| |
| 279 | struct nodoList * tmp; | |
| 280 | if(!FullList(*l)) {
| |
| 281 | tmp=(struct nodoList *) malloc (sizeof(struct nodoList)); | |
| 282 | tmp->info=x; | |
| 283 | if(p==LISTAVUOTA) {
| |
| 284 | tmp->next=*l; | |
| 285 | *l=tmp; | |
| 286 | } | |
| 287 | else {
| |
| 288 | tmp->next=p->next; | |
| 289 | p->next=tmp; | |
| 290 | } | |
| 291 | } | |
| 292 | } | |
| 293 | ||
| 294 | void DeleteList(list *l, position p) {
| |
| 295 | struct nodoList * tmp; | |
| 296 | if(!EmptyList(*l)) {
| |
| 297 | if(p==LISTAVUOTA) {
| |
| 298 | tmp=*l; | |
| 299 | *l=tmp->next; | |
| 300 | } | |
| 301 | else {
| |
| 302 | tmp=p->next; | |
| 303 | p->next=tmp->next; | |
| 304 | } | |
| 305 | free(tmp); | |
| 306 | } | |
| 307 | } | |
| 308 | ||
| 309 | /* Svolgimento Compito */ | |
| 310 | #define DIM 26 | |
| 311 | ||
| 312 | list archivio[DIM]; | |
| 313 | ||
| 314 | void Media(abr a, unsigned int *x, unsigned int *numero) {
| |
| 315 | ||
| 316 | tipobaseABR y; | |
| 317 | if(!EmptyABR(a)) {if(!EmptyABR(LeftChild(a)))
| |
| 318 | Media(LeftChild(a), x, numero); | |
| 319 | y=Label(a); | |
| 320 | *x=*x+EstraiVoto(y); | |
| 321 | (*numero)=(*numero)+1; | |
| 322 | if(!EmptyABR(RightChild(a))) | |
| 323 | Media(RightChild(a), x, numero); | |
| 324 | } | |
| 325 | } | |
| 326 | ||
| 327 | void Visitainord(abr a) {
| |
| 328 | if(!EmptyABR(a)) {
| |
| 329 | if(!EmptyABR(LeftChild(a))) | |
| 330 | Visitainord(LeftChild(a)); | |
| 331 | VisualizzaElementoABR(Label(a)); | |
| 332 | if(!EmptyABR(RightChild(a))) | |
| 333 | Visitainord(RightChild(a)); | |
| 334 | } | |
| 335 | } | |
| 336 | ||
| 337 | ||
| 338 | void Insord(list *l, tipobaseList x) {
| |
| 339 | tipobaseList y; | |
| 340 | position p, u; | |
| 341 | ||
| 342 | if(!FullList(*l)) { p=First(*l);
| |
| 343 | if(EmptyList(*l)) InsertList(l, p, x); | |
| 344 | else {
| |
| 345 | u=End(*l); | |
| 346 | if(Locate(*l, x)!=u) printf("\nStudente gia' presente\n");
| |
| 347 | else {
| |
| 348 | while(p!=u) {
| |
| 349 | y=Retrieve(*l, p); | |
| 350 | if(ConfrontaList(y, x)>0) | |
| 351 | break; | |
| 352 | else p=Next(*l, p); | |
| 353 | } | |
| 354 | InsertList(l, p, x); | |
| 355 | } | |
| 356 | } | |
| 357 | } | |
| 358 | } | |
| 359 | ||
| 360 | ||
| 361 | ||
| 362 | void InsMateria(list *l, tipobaseList x, tipobaseABR y) {
| |
| 363 | abr a; | |
| 364 | position p, u; | |
| 365 | ||
| 366 | if(EmptyList(*l)) { printf("\nLa lista e' vuota. Prima di inserire la materia "
| |
| 367 | "sara' inserito lo studente\n"); | |
| 368 | LeggiElementoList(&x); | |
| 369 | Insord(l, x); | |
| 370 | p=First(*l); | |
| 371 | } | |
| 372 | else { p=Locate(*l, x);
| |
| 373 | if(p==End(*l)) { printf("\nStudente non trovato. Prima di continuare con l'inserimento\n"
| |
| 374 | "della materia, verra' inserito lo studente in archivio\n"); | |
| 375 | LeggiElementoList(&x); | |
| 376 | Insord(l, x); | |
| 377 | p=Locate(*l, x); | |
| 378 | } | |
| 379 | else | |
| 380 | x=Retrieve(*l, p); | |
| 381 | } | |
| 382 | a=EstraiABR(x); | |
| 383 | if(!FullABR(a)) {
| |
| 384 | InsertABR(&a, y); | |
| 385 | AggiornaABR(&x, a); | |
| 386 | DeleteList(l, p); | |
| 387 | InsertList(l, p, x); | |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 391 | ||
| 392 | ||
| 393 | ||
| 394 | void VisualizzaMaterie(list l, tipobaseList x) {
| |
| 395 | position p, u; | |
| 396 | tipobaseList y; | |
| 397 | abr a; | |
| 398 | unsigned int somma=0, materie=0; | |
| 399 | unsigned int flag=0; | |
| 400 | float media; | |
| 401 | if(EmptyList(l)) printf("\nLista vuota\n");
| |
| 402 | else {
| |
| 403 | p=First(l); | |
| 404 | u=End(l); | |
| 405 | while(p!=u) {
| |
| 406 | y=Retrieve(l, p); | |
| 407 | if(!ConfrontaCognome(y, x)) {
| |
| 408 | VisualizzaElementoList(y); | |
| 409 | flag=1; | |
| 410 | } | |
| 411 | p=Next(l, p); | |
| 412 | } | |
| 413 | if(!flag) printf("\nNon esiste alcuno studente con questo cognome in archivio\n");
| |
| 414 | else {
| |
| 415 | printf("\nQual e' lo studente cercato?\n");
| |
| 416 | CercaElementoList(&x); | |
| 417 | p=Locate(l, x); | |
| 418 | if(p==u) printf("\nStudente non presente\n");
| |
| 419 | else {
| |
| 420 | x=Retrieve(l, p); | |
| 421 | a=EstraiABR(x); | |
| 422 | if(EmptyABR(a)) printf("\nLo studente non ha dato nessuna materia\n");
| |
| 423 | else{
| |
| 424 | Media(a, &somma, &materie); | |
| 425 | Visitainord(a); | |
| 426 | media = (float) somma / (float ) materie; | |
| 427 | printf("\nMedia: %g\n", media);
| |
| 428 | } | |
| 429 | } | |
| 430 | } | |
| 431 | } | |
| 432 | } | |
| 433 | ||
| 434 | main() {
| |
| 435 | unsigned int scelta, i, indice, materie; | |
| 436 | tipobaseList x; | |
| 437 | tipobaseABR y; | |
| 438 | ||
| 439 | for(i=0; i<DIM; i++) | |
| 440 | MakeNullList(archivio+i); | |
| 441 | ||
| 442 | do {
| |
| 443 | printf("\n\n\t\t\tMENU'\n");
| |
| 444 | printf("1) Inserisci un nuovo studente\n");
| |
| 445 | printf("2) Registra una nuova materia\n");
| |
| 446 | printf("3) Visualizza le materie date da uno studente\n");
| |
| 447 | printf("4) Esci\n");
| |
| 448 | scanf("%u", &scelta);
| |
| 449 | FLUSH; | |
| 450 | ||
| 451 | switch(scelta) {
| |
| 452 | case 1: LeggiElementoList(&x); | |
| 453 | indice=Indice(x); | |
| 454 | Insord(&(archivio[indice]), x); | |
| 455 | printf("\nLo studente ha gia' dato delle materie?\n"
| |
| 456 | "Scrivere il numero di materie date: "); | |
| 457 | scanf("%u", &materie);
| |
| 458 | FLUSH; | |
| 459 | if(materie>0) | |
| 460 | for(i=0; i<materie; i++) {
| |
| 461 | LeggiElementoABR(&y); | |
| 462 | InsMateria(&(archivio[indice]), x, y); | |
| 463 | } | |
| 464 | break; | |
| 465 | ||
| 466 | case 2: CercaElementoList(&x); | |
| 467 | indice=Indice(x); | |
| 468 | LeggiElementoABR(&y); | |
| 469 | InsMateria(&(archivio[indice]), x, y); | |
| 470 | break; | |
| 471 | ||
| 472 | case 3: CercaCognomeList(&x); | |
| 473 | indice=Indice(x); | |
| 474 | VisualizzaMaterie(archivio[indice], x); | |
| 475 | break; | |
| 476 | } | |
| 477 | } while(scelta!=4); | |
| 478 | } |