Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- typedef struct {
- char nome[20];
- int quantità;
- char u_m[12];
- }element;
- typedef struct list_element {
- element spesa;
- struct list_element *next;
- }item;
- typedef item* list;
- bool IsNameEqual(element e1, element e2)
- {
- if (strcmp(e1.nome, e2.nome) ==0)
- return 1;
- else
- return 0;
- }
- element copy(element e)
- {
- element el;
- el = e;
- return el;
- }
- list emptylist()
- {
- return NULL;
- }
- bool empty(list l)
- {
- if (l == NULL)
- return 1;
- else
- return 0;
- }
- element head(list l)
- {
- if (empty(l))
- abort();
- else
- return l->spesa;
- }
- list tail(list l)
- {
- if (empty(l))
- abort();
- else
- l = l->next;
- return l;
- }
- list cons(element e, list l)
- {
- list t;
- t = malloc(sizeof(item));
- t->spesa = e;
- t->next = l;
- return t;
- }
- bool yet (list l,element e)
- {
- while (!empty(l)) {
- if (IsNameEqual(head(l), e))
- return true;
- l = tail(l);
- }
- return false;
- }
- void print_Higher_quantity(list l, int quantità)
- {
- int n = 0;
- element el;
- while (!empty(l)) {
- el = head(l);
- if (el.quantità > quantità) {
- ++n;
- printf("\n Elemento con quantità strettamente maggiore dell' input : %s ;", el.nome);
- }
- l = tail(l);
- }
- if (n == 0)
- printf("\n NESSUN ELEMENTO HA UNA QUANTITA' STRETTAMENTE MAGGIORE DELL' INMPUT");
- }
- char *get_u_m(list l, char *nome)
- {
- element el;
- while (!empty(l)) {
- el = head(l);
- if (strcmp(nome, el.nome) == 0)
- break;
- l = tail(l);
- }
- return el.u_m;
- }
- int get_quantità(list l, char *nome)
- {
- element el;
- while (!empty(l)) {
- el = head(l);
- if (strcmp(nome, el.nome) == 0)
- break;
- l = tail(l);
- }
- return el.quantità;
- }
- list tail_insert(list l, element e)
- {
- list root = l;
- while (l->next != NULL) {
- l = tail(l);
- }
- list t = emptylist();
- t = cons(e, t);
- l->next = t;
- return root;
- }
- void showlist(list l)
- {
- element el;
- while (!empty(l)) {
- el = head(l);
- printf("\n Nome : %s ;\t", el.nome);
- printf("Quantità : %d ;\t", el.quantità);
- printf("U.M : %s ;\t", el.u_m);
- l = tail(l);
- }
- printf("\n");
- }
- void main()
- {
- char u_m[12];
- char nome[20];
- element e;
- list l = emptylist();
- int menù;
- printf("\t\t\t\t\t\t\t\t LISTA DELLA SPESA\n");
- do {
- printf("\t\t\t\t\t\t\tINSERIRE COMANDO PER ESEGUIRE AZIONE\n\n ");
- printf("0.Esci dal programma.Fine.\n");
- printf("1.Inserisci un elemento da acquistare\n");
- printf("2.Inserisci elemento da acquistare in coda\n");
- printf("3.Fornisci la quantità e l'unità di misura, dato il nome dell' elemento da acquistare\n");
- printf("4.Stampa tutti gli elementi da acquistare\n");
- printf("5.Data una quantità, stampa gli elementi da acquistare che hanno una quantità strettamente maggiore\n");
- scanf("%d", &menù);
- switch (menù){
- case 0:
- printf("\n\n USCITA DAL PROGRAMMA IN CORSO...");
- break;
- case 1:
- printf("\n inserisci nome elemento : ");
- scanf("%s", &e.nome);
- printf("\n inserisci quantità : ");
- scanf("%d", &e.quantità);
- printf("\n inserisci unità di misura : ");
- scanf("%s", &e.u_m);
- if (yet(l, e))
- printf("\nErrore elemento già presente...");
- else
- l = cons(e, l);
- break;
- case 2:
- printf("\n inserisci nome elemento : ");
- scanf("%s", &e.nome);
- printf("\n inserisci quantità : ");
- scanf("%d", &e.quantità);
- printf("\n inserisci unità di misura : ");
- scanf("%s", &e.u_m);
- if (yet(l, e))
- printf("\nErrore elemento già presente...");
- else
- l = tail_insert(l, e);
- break;
- case 3:
- printf("\n inserisci nome elemento : ");
- scanf("%s", nome);
- int quantità = get_quantità(l, nome);
- strcpy(u_m, get_u_m(l, nome));
- printf("\nquantità : %d", quantità);
- printf("\nunità di misura : %s ", u_m);
- break;
- case 4:
- showlist(l);
- break;
- case 5:
- printf("digitare la quantità : ");
- scanf("%d", &quantità);
- print_Higher_quantity(l, quantità);
- break;
- default:
- printf("inserimento errato...riprovare ");
- }
- } while (menù != 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement