Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Si scriva un programma in grado di gestire un listino prezzi, ovvero deve
- gestire un elenco di prodotti e i loro relativi prezzi in €. Il programma
- utilizza le liste, ogni informazione ha lunghezza M.
- Nel campo 'price' verrà memorizzato il costo.
- Il programma deve permettere all'utente di invocare tramite menu le
- seguenti operazioni per manipolare il listino:
- a. 'creaLista': è una funzione che permette di includere un nuovo
- prodotto nel listino; restituisce il valore 1 se il prodotto non era ancora
- presente, 0 se già inserito in precedenza.
- b. 'printLista': è una funzione che permette di visualizzare a video il
- contenuto del listino. Inoltre, la funzione stampa 2 valori: il prezzo
- medio ed il prezzo massimo.
- c. 'updateLista': è una funzione che permette di aggiornare il prezzo
- di uno specifico prodotto; informa se l’aggiornamento è avvenuto
- con successo o se il prodotto non esiste.
- d. 'removeProduct': è una funzione che permette di rimuovere un
- prodotto dal listino; informa se la rimozione è avvenuto con
- successo o se il prodotto non esiste;
- */
- /*Librerie*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define N 20
- /*strutture*/
- struct lista {
- char inf[N];
- float price;
- struct lista *pun;
- };
- /*funzioni*/
- int menuLista();
- struct lista *creaLista();
- void pushLista(struct lista *);
- void printLista(struct lista *);
- void updateLista(struct lista *);
- struct lista *confronta(struct lista *, int *, char *);
- struct lista *popProduct(struct lista *);
- /*main!*/
- main()
- {
- int menu=-1;
- struct lista *puntLista=NULL;
- while(menu!=0){
- if(menu==-1) menu=1;
- else menu=menuLista();
- switch(menu){
- case 1: {
- if(puntLista==NULL) puntLista=creaLista();
- pushLista(puntLista);
- break;}
- case 2: {
- system("cls");
- updateLista(puntLista); break;}
- case 3: {
- system("cls");
- puntLista=popProduct(puntLista); break;}
- case 4: {
- system("cls");
- printLista(puntLista);
- printf("\n\n\nIn attesa...\t");
- getchar(); break;}
- case 5: menu=0; break;
- default:{
- printf("opzione non ammessa");
- printf("\n\n\nIn attesa...\t");
- getchar();}
- }
- system("cls"); }
- return 0;
- }
- /*menu'*/
- int menuLista()
- {
- int menu;
- printf("\n------------- MENU'------------\n");
- printf("\n1. Inserisci prodotto\n\n2. Aggiorna prezzo\n\n3. Rimuovi prodotto\n\n4. Stampa listino\n\n5. Exit\n\n ");
- scanf("%d", &menu);
- getchar();
- return menu;
- }
- /*crea lista*/
- struct lista *creaLista()
- {
- struct lista *p;
- p=(struct lista *)malloc(sizeof(struct lista));
- printf("\nInserisci nome prodotto \t");
- scanf("%20s", p->inf);
- printf("\nInserisci prezzo prodotto\t");
- scanf("%f", &p->price);
- p->pun=NULL;
- return p;
- }
- void pushLista(struct lista *p)
- {
- struct lista *paus, *paux=p;
- char elem[N];
- int j;
- while(paux!=NULL) {
- paus=paux;
- paux=paux->pun;}
- while(elem[0]!='0') {
- system("cls");
- printLista(p);
- printf("\n\nInserisci nome prodotto, 0 per uscire\t");
- scanf("%20s", &elem);
- if(elem[0]!='0') {
- confronta(p, &j, elem);
- if(j!=1){
- paus->pun=(struct lista *)malloc(sizeof(struct lista));
- paus=paus->pun;
- printf("\nInserisci prezzo prodotto\t\t");
- scanf("%f", &paus->price);
- strcpy(paus->inf, elem);
- paus->pun=NULL;}
- else {printf("\n\nProdotto gia' presente!\t");
- getchar(); getchar();}}}
- }
- /*stampa lista*/
- void printLista(struct lista *p)
- {
- float n=0, j=0, avg, max=0;
- printf("\n\tNome Prodotto\t\tPrezzo");
- while(p!=NULL) {
- n++;
- printf("\n\t%-20s", p->inf);
- printf("\t%.2f", p->price);
- j=j+p->price;
- avg=j/n;
- if(max<p->price) max=p->price;
- p=p->pun;
- }
- printf("\n\n\nPrezzo medio: %.2f, prezzo massimo: %.2f", avg, max);
- }
- /*aggiorna listino prezi*/
- void updateLista(struct lista *p)
- {
- int j=0, b;
- char update[N];
- char nuovo='s';
- struct lista x;
- struct lista *paus;
- paus=p;
- while(nuovo=='s') {
- paus=p;
- system("cls");
- printLista(p);
- printf("\n\nInserire oggetto da aggiornare\t");
- scanf("%20s", update);
- paus=confronta(p, &j, update);
- if(j==1){
- printf("\nNuovo prezzo?\t");
- scanf("%f", &x.price);
- paus->price=x.price;}
- else printf("\n\nProdotto non presente!\n");
- printf("\nAggiornare un altro prodotto? s o n\t");
- scanf("%1s", &nuovo);
- }
- }
- /*funzione di appoggio, confronta elementi*/
- struct lista *confronta(struct lista *p, int *j, char *a)
- {
- int x=0;
- struct lista *paus;
- *j=0;
- while(p!=NULL) {
- if(strcmp(p->inf, a)==0) *j=1; //ritorna j=0 se l esito è negativo
- if(*j==1 && x==0) {paus=p; x++;} //j=1 se trova un elemento uguale
- p=p->pun;}
- return paus;
- }
- /*rimuovi prodotto*/
- struct lista *popProduct(struct lista *pp)
- {
- int j=0, b;
- char update[N];
- char nuovo='s', risp;
- struct lista x;
- struct lista *paus, *paux, *p;
- p=pp;
- while(nuovo=='s') {
- p=pp;
- system("cls");
- printLista(pp);
- printf("\n\nInserire oggetto da eliminare\t");
- scanf("%20s", update);
- paus=confronta(p, &j, update);
- if(j==1){
- printf("\nEliminare veramente? s o n\t");
- scanf("%s", &risp);
- if(risp=='s'){
- while(p!=paus){
- paux=p;
- p=p->pun;}
- if(pp==paus) pp=p->pun;
- else if(paus->pun==NULL) paux->pun=NULL;
- else paux->pun=p->pun;
- free(paus);}}
- else printf("\n\nProdotto non presente!\n");
- printf("\nEliminare un altro prodotto? s o n\t");
- scanf("%1s", &nuovo);
- }
- return pp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement