Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FLUSH while(getchar()!='\n')
- #define LISTAVUOTA NULL
- #define N 15
- typedef struct{
- char codice[N],marca[N];
- unsigned int prezzo,presenti;
- }tipobaselist;
- void leggistringa(char s[],short dim){
- short i;
- for(i=0;(s[i]=getchar())!='\n' && i<dim-1;i++);
- s[i]='\0';
- if(i==dim-1) FLUSH;}
- void acquisisci(tipobaselist *x){
- printf("\ninserire il codice: ");
- leggistringa(x->codice,N);
- printf("inserire la marca: ");
- leggistringa(x->marca,N);
- printf("prezzo: ");
- scanf("%u", &x->prezzo);
- printf("pezzi presenti in magazzino: ");
- scanf("%u", &x->presenti);
- FLUSH;}
- void mostra(tipobaselist x){
- printf("\nmarca: %s\n", x.marca);
- printf("prezzo: %u\n", x.prezzo);
- printf("pezzi presenti: %u\n", x.presenti);}
- void cerca(tipobaselist *x){
- printf("\ninserire il codice da ricercare: ");
- leggistringa(x->codice,N);}
- int confronta(tipobaselist a,tipobaselist b){
- return(strcmp(a.codice,b.codice));}
- typedef struct nodolist{
- tipobaselist info;
- struct nodolist *next;
- }*list;
- typedef list position;
- typedef short boolean;
- void makenulllist(list *l){
- *l=LISTAVUOTA;}
- boolean emptylist(list l){
- return(l==LISTAVUOTA);}
- boolean fulllist(list l){
- struct nodolist *tmp;
- tmp=(struct nodolist *)malloc(sizeof(struct nodolist));
- if(tmp==LISTAVUOTA)
- return 1;
- free(tmp);
- return 0;}
- position first(list l){
- return(LISTAVUOTA);}
- position end(list l){
- if(l==LISTAVUOTA) return(LISTAVUOTA);
- while(l->next!=LISTAVUOTA)
- l=l->next;
- return(l);}
- void insertlist(list *l,position p,tipobaselist x){
- struct nodolist *tmp;
- if(!fulllist(*l)){
- tmp=(struct nodolist *)malloc(sizeof(struct nodolist));
- tmp->info=x;
- if(p==LISTAVUOTA){
- tmp->next=*l;
- *l=tmp;}
- else{
- tmp->next=p->next;
- p->next=tmp;}}}
- void deletelist(list *l,position p){
- struct nodolist *tmp;
- if(!emptylist(*l))
- if(p==LISTAVUOTA){
- tmp=(*l)->next;
- free(*l);
- *l=tmp;}
- else{
- tmp=p->next;
- p->next=tmp->next;
- free(tmp);}}
- position locate(list l,tipobaselist x){
- if(!emptylist(l)){
- if(!confronta(l->info,x)) return(LISTAVUOTA);
- while(l->next!=LISTAVUOTA){
- if(!confronta(l->next->info,x)) return l;
- l=l->next;}
- return l;}}
- tipobaselist retrieve(list l,position p){
- if(!emptylist(l))
- if(p==LISTAVUOTA) return(l->info);
- else return(p->next->info);}
- position next(list l,position p){
- if(p==LISTAVUOTA) return(l);
- else return(p->next);}
- struct nodovettore{
- unsigned int contatore;
- list lista;
- };
- void allocalist(list **);
- void allocavet(struct nodovettore **,short );
- void insord(list *,tipobaselist);
- void inizializza(struct nodovettore *,short);
- void inserisci_prodotto(struct nodovettore *,tipobaselist,unsigned int,unsigned int *);
- void mostra_prodotto(struct nodovettore *,tipobaselist, unsigned int);
- main()
- {
- unsigned int scelta,n;
- struct nodovettore *archivio;
- tipobaselist elem;
- unsigned int index=-1;
- printf("inserire la cardinalita' del vettore: ");
- scanf("%u", &n);
- allocavet(&archivio,n);
- inizializza(archivio,n);
- do{
- printf("1 inserire un nuovo prodotto\n");
- printf("2 mostra prodotto\n");
- printf("3 esci\n");
- printf("cosa si desidera fare: ");
- scanf("%u", &scelta);
- FLUSH;
- switch(scelta){
- case 1:
- acquisisci(&elem);
- inserisci_prodotto(archivio,elem,n,&index);
- break;
- case 2:
- cerca(&elem);
- mostra_prodotto(archivio,elem,n);
- break;
- case 3:
- exit(1);
- default:
- printf("valore non presente\n");
- }
- }while(scelta!=3);
- }
- void allocalist(list **l){
- *l=(list *)malloc(sizeof(list));}
- void allocavet(struct nodovettore **v,short dim){
- *v=(struct nodovettore *)malloc(sizeof(struct nodovettore));}
- void inizializza(struct nodovettore *v,short dim){
- short i;
- for(i=0;i<dim;i++){
- v[i].contatore=0;
- allocalist(&(v[i].lista));
- makenulllist(&(v[i].lista));}}
- void insord(list *l,tipobaselist x){
- position p,u;
- tipobaselist tmp;
- if(emptylist(*l)) insertlist(l,first(*l),x);
- else{
- p=first(*l);
- u=end(*l);
- while(p!=u){
- tmp=retrieve(*l,p);
- if(confronta(tmp,x)<0) p=next(*l,p);
- else break;}
- insertlist(l,p,x);}}
- void inserisci_prodotto(struct nodovettore *v,tipobaselist x,unsigned int n,unsigned int *indice){
- position pos;
- unsigned int i;
- unsigned int min;
- short xx=0;
- if((*indice) ==-1){
- insord(&(v[0].lista),x);
- (v[0].contatore)++ ;
- (*indice)=0;
- printf("prodotto inserito\n");}
- else{
- min=(v[0].contatore);
- for(i=0;i<n;i++) {
- if(!emptylist(v[i].lista)) pos=locate((v[i].lista),x);
- if(emptylist(v[i].lista) || pos==end(v[i].lista));
- else{
- printf("prodotto gia' presente\n");
- xx++;
- break;}}
- if(xx==0){
- for(i=0;i<n;i++)
- if((v[i].contatore)<min){
- min=v[i].contatore;
- (*indice)=i; }
- if(!fulllist(v[*indice].lista)){
- insord(&(v[*indice].lista),x);
- (v[*indice].contatore)++ ;
- printf("prodotto inserito\n");}
- else printf("archivio pieno\n");}}}
- void mostra_prodotto(struct nodovettore *v,tipobaselist x, unsigned int n){
- position pos;
- tipobaselist tmp;
- unsigned int i;
- short counter=0;
- for(i=0;i<n;i++){
- if(emptylist(v[i].lista));
- else{
- pos=locate(v[i].lista,x);
- if(pos!=end(v[i].lista)){
- tmp=retrieve((v[i].lista),pos);
- mostra(tmp);
- counter++;
- break;}}}
- if(counter==0) printf("codice non presente\n");}
Advertisement
Add Comment
Please, Sign In to add comment