Advertisement
Dany1858

Esercizio 8.2 v2 (Listino con liste)

Nov 28th, 2014
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 KB | None | 0 0
  1. /*Si scriva un programma in grado di gestire un listino prezzi, ovvero deve
  2. gestire un elenco di prodotti e i loro relativi prezzi in €. Il programma
  3. utilizza le liste, ogni informazione ha lunghezza M.
  4.  
  5. Nel campo 'price' verrà memorizzato il costo.
  6.  
  7. Il programma deve permettere all'utente di invocare tramite menu le
  8. seguenti operazioni per manipolare il listino:
  9.  
  10. a. 'creaLista': è una funzione che permette di includere un nuovo
  11. prodotto nel listino; restituisce il valore 1 se il prodotto non era ancora
  12. presente, 0 se già inserito in precedenza.
  13.  
  14. b. 'printLista': è una funzione che permette di visualizzare a video il
  15. contenuto del listino. Inoltre, la funzione stampa 2 valori: il prezzo
  16. medio ed il prezzo massimo.
  17.  
  18. c. 'updateLista': è una funzione che permette di aggiornare il prezzo
  19. di uno specifico prodotto; informa se l’aggiornamento è avvenuto
  20. con successo o se il prodotto non esiste.
  21.  
  22. d. 'removeProduct': è una funzione che permette di rimuovere un
  23. prodotto dal listino; informa se la rimozione è avvenuto con
  24. successo o se il prodotto non esiste;
  25. */
  26.  
  27.                 /*Librerie*/
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #define N 20
  32.  
  33.                    /*strutture*/
  34. struct lista {
  35.        char inf[N];
  36.        float price;
  37.        struct lista *pun;
  38. };
  39.  
  40.                     /*funzioni*/
  41. int menuLista();
  42. struct lista *creaLista();
  43. void pushLista(struct lista *);
  44. void printLista(struct lista *);
  45. void updateLista(struct lista *);
  46. struct lista *confronta(struct lista *, int *, char *);
  47. struct lista *popProduct(struct lista *);
  48.              
  49.              /*main!*/
  50. main()
  51. {
  52.       int menu=-1;
  53.       struct lista *puntLista=NULL;
  54.       while(menu!=0){
  55.           if(menu==-1) menu=1;
  56.           else menu=menuLista();
  57.  
  58.       switch(menu){
  59.           case 1: {
  60.               if(puntLista==NULL) puntLista=creaLista();
  61.               pushLista(puntLista);
  62.               break;}
  63.           case 2: {
  64.               system("cls");
  65.               updateLista(puntLista); break;}
  66.           case 3: {
  67.               system("cls");
  68.               puntLista=popProduct(puntLista); break;}
  69.           case 4: {
  70.               system("cls");
  71.               printLista(puntLista);
  72.               printf("\n\n\nIn attesa...\t");
  73.               getchar(); break;}
  74.           case 5: menu=0; break;
  75.           default:{
  76.               printf("opzione non ammessa");
  77.               printf("\n\n\nIn attesa...\t");
  78.               getchar();}
  79.            }  
  80.       system("cls"); }
  81.       return 0;
  82. }
  83.  
  84.                    /*menu'*/
  85. int menuLista()
  86. {
  87.              int menu;
  88.              printf("\n------------- MENU'------------\n");
  89.              printf("\n1. Inserisci prodotto\n\n2. Aggiorna prezzo\n\n3. Rimuovi prodotto\n\n4. Stampa listino\n\n5. Exit\n\n ");
  90.              scanf("%d", &menu);
  91.              getchar();
  92.              return menu;
  93. }
  94.  
  95.                      /*crea lista*/
  96. struct lista *creaLista()
  97. {
  98.        struct lista *p;
  99.        p=(struct lista *)malloc(sizeof(struct lista));
  100.        printf("\nInserisci nome prodotto \t");
  101.        scanf("%20s", p->inf);
  102.        printf("\nInserisci prezzo prodotto\t");
  103.        scanf("%f", &p->price);
  104.        p->pun=NULL;
  105.        return p;
  106. }
  107.  
  108. void pushLista(struct lista *p)
  109. {
  110.        struct lista *paus, *paux=p;
  111.        char elem[N];
  112.        int j;
  113.        while(paux!=NULL) {
  114.             paus=paux;
  115.             paux=paux->pun;}
  116.  
  117.        while(elem[0]!='0') {
  118.           system("cls");
  119.           printLista(p);
  120.           printf("\n\nInserisci nome prodotto, 0 per uscire\t");
  121.           scanf("%20s", &elem);
  122.           if(elem[0]!='0') {
  123.               confronta(p, &j, elem);            
  124.               if(j!=1){
  125.                   paus->pun=(struct lista *)malloc(sizeof(struct lista));
  126.                   paus=paus->pun;
  127.                   printf("\nInserisci prezzo prodotto\t\t");
  128.                   scanf("%f", &paus->price);
  129.                   strcpy(paus->inf, elem);
  130.                   paus->pun=NULL;}
  131.               else {printf("\n\nProdotto gia' presente!\t");
  132.                    getchar(); getchar();}}}
  133. }
  134.  
  135.                               /*stampa lista*/
  136. void printLista(struct lista *p)
  137. {
  138.      float n=0, j=0, avg, max=0;
  139.      printf("\n\tNome Prodotto\t\tPrezzo");
  140.      while(p!=NULL) {
  141.          n++;
  142.          printf("\n\t%-20s", p->inf);
  143.          printf("\t%.2f", p->price);
  144.          j=j+p->price;
  145.          avg=j/n;
  146.          if(max<p->price) max=p->price;
  147.          p=p->pun;
  148.      }
  149.      printf("\n\n\nPrezzo medio: %.2f, prezzo massimo: %.2f", avg, max);
  150. }
  151.  
  152.                           /*aggiorna listino prezi*/
  153. void updateLista(struct lista *p)
  154. {
  155.      int j=0, b;
  156.      char update[N];
  157.      char nuovo='s';
  158.      struct lista x;
  159.      struct lista *paus;
  160.      
  161.      paus=p;
  162.      while(nuovo=='s') {
  163.         paus=p;
  164.         system("cls");
  165.         printLista(p);
  166.         printf("\n\nInserire oggetto da aggiornare\t");
  167.         scanf("%20s", update);
  168.         paus=confronta(p, &j, update);
  169.         if(j==1){
  170.             printf("\nNuovo prezzo?\t");
  171.             scanf("%f", &x.price);
  172.             paus->price=x.price;}
  173.         else printf("\n\nProdotto non presente!\n");
  174.         printf("\nAggiornare un altro prodotto? s o n\t");
  175.         scanf("%1s", &nuovo);
  176.      }
  177. }
  178.  
  179.                         /*funzione di appoggio, confronta elementi*/
  180. struct lista *confronta(struct lista *p, int *j, char *a)
  181. {
  182.     int x=0;
  183.     struct lista *paus;
  184.     *j=0;
  185.     while(p!=NULL) {
  186.          if(strcmp(p->inf, a)==0) *j=1;          //ritorna j=0 se l esito è negativo
  187.          if(*j==1 && x==0) {paus=p; x++;}       //j=1 se trova un elemento uguale
  188.          p=p->pun;}
  189.     return paus;
  190. }
  191.  
  192.                            /*rimuovi prodotto*/
  193. struct lista *popProduct(struct lista *pp)
  194. {
  195.      int j=0, b;
  196.      char update[N];
  197.      char nuovo='s', risp;
  198.      struct lista x;
  199.      struct lista *paus, *paux, *p;
  200.      
  201.      p=pp;
  202.      while(nuovo=='s') {
  203.         p=pp;
  204.         system("cls");
  205.         printLista(pp);
  206.         printf("\n\nInserire oggetto da eliminare\t");
  207.         scanf("%20s", update);
  208.         paus=confronta(p, &j, update);
  209.         if(j==1){
  210.             printf("\nEliminare veramente? s o n\t");
  211.             scanf("%s", &risp);
  212.             if(risp=='s'){
  213.                 while(p!=paus){
  214.                     paux=p;
  215.                     p=p->pun;}
  216.                 if(pp==paus) pp=p->pun;
  217.                 else if(paus->pun==NULL) paux->pun=NULL;
  218.                 else paux->pun=p->pun;
  219.                 free(paus);}}
  220.         else printf("\n\nProdotto non presente!\n");
  221.         printf("\nEliminare un altro prodotto? s o n\t");
  222.         scanf("%1s", &nuovo);
  223.         }
  224.         return pp;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement