Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Compito 20/07/2012
- In un condominio si gestisce il conto spese con un archivio informatico rappresentato da un vettore.
- Ogni elemento del vettore
- contiene due liste che si riferiscono a scala a e b.
- Ogni lista contiene:
- -cognome
- -nome
- -spese da sostenere
- La dimensione del vettore corrisponde al numero di piani.
- Codificare:
- un main
- una funzione che allochi il vettore
- una funzione che riceva in ingresso: piano, scala, cognome e nome e inserisca un nuovo condomine
- (le spese vengono poste a 0)
- una funzione che riceva in ingresso: piano, scala, cognome, nome e il nuovo valore delle spese condominiali,
- la funzione deve rimpiazzare il precedente valore con il nuovo
- */
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define N 12
- #define FLUSH while(getchar()!='\n')
- typedef struct{
- char nome[N],cognome[N];
- unsigned int spese;
- }tipobaseList;
- void LeggiStringa(char s[],unsigned int dim){
- unsigned int i;
- for(i=0; i<dim-1 && (s[i]=getchar())!='\n'; i++);
- s[i]='\0';
- if(i==dim-1) FLUSH;
- }
- typedef short boolean;
- void LeggiElemento(tipobaseList *x){
- printf("\nNome: ");
- LeggiStringa(x->nome,N);
- printf("\nCognome: ");
- LeggiStringa(x->cognome,N);
- }
- void CercaElemento(tipobaseList *x){
- printf("\nNome: ");
- LeggiStringa(x->nome,N);
- printf("\nCognome: ");
- LeggiStringa(x->cognome,N);
- printf("\nspese: ");
- scanf("%u",&x->spese);
- FLUSH;
- }
- void AzzeraSpese(tipobaseList *x){
- x->spese=0;
- }
- int Confronta(tipobaseList a,tipobaseList b){
- if(!strcmp(a.cognome,b.cognome)) return(strcmp(a.nome,b.nome));
- else return(strcmp(a.cognome,b.cognome));
- }
- //lista.h
- #define LISTAVUOTA NULL
- typedef struct nodoList{
- tipobaseList info;
- struct nodoList *next;
- }*list;
- typedef list position;
- typedef short boolean;
- void MakeNullList(list *l){
- *l=LISTAVUOTA;
- }
- position First(list l){
- return(LISTAVUOTA);
- }
- position End(list l){
- if(l==LISTAVUOTA) return(LISTAVUOTA);
- else while(l->next!=LISTAVUOTA){
- l=l->next;
- return(l);//attenzione
- }
- }
- 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;
- }
- 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;
- }
- }
- }
- position Locate(list l,tipobaseList x){
- if(!EmptyList(l)){
- if(!Confronta(l->info,x)) return(LISTAVUOTA);
- else 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(!EmptyList(l)){
- if(p==LISTAVUOTA) return(l);
- else return(p->next);
- }
- }
- 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);//attenzione
- }
- }
- }
- //compito
- struct nodoVect{
- list ListaA;
- list ListaB;
- }*archivio;
- void Inizializza(struct nodoVect *, unsigned int);
- void Alloca(struct nodoVect **, unsigned int);
- unsigned int Index(unsigned int);
- void Insord(list *,tipobaseList);
- void InserisciCondomine(struct nodoVect *,tipobaseList ,unsigned int);
- void AggiornaSpese(struct nodoVect *,tipobaseList ,unsigned int);
- main(){
- unsigned int n;
- unsigned short scelta;
- tipobaseList condomine;
- do{
- printf("dimensione vettore:\n");
- scanf("%u",&n);
- FLUSH;
- }while(n<=0);
- Alloca(&archivio,n);
- do{
- printf("------MENU'-----\n\n");
- printf("1) Inserisci condomine\n");
- printf("2) aggiorna spese\n");
- printf("3) FINE\n");
- printf("Inserisci scelta----> \n");
- scanf("%u",&scelta);
- FLUSH;
- switch(scelta){
- case 1:
- LeggiElemento(&condomine);
- InserisciCondomine(archivio,condomine,n);
- break;
- case 2:
- CercaElemento(&condomine);
- AggiornaSpese(archivio,condomine,n);
- break;
- }
- }while(scelta<3);
- }
- void Inizializza(struct nodoVect *v, unsigned int dim){
- unsigned int j;
- for(j=0; j<dim; j++){
- MakeNullList(&(v[j].ListaA));
- MakeNullList(&(v[j].ListaB));
- }
- }
- void Alloca(struct nodoVect **v, unsigned int dim){
- *v=(struct nodoVect *)malloc(dim*sizeof(struct nodoVect));
- Inizializza(*v,dim);
- }
- unsigned int Index(unsigned int dim){
- unsigned int i;
- printf("\ndigita piano");
- do{
- scanf("%u",&i);
- FLUSH;
- }while(i>dim && i<0);
- return(i);
- }
- 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 InserisciCondomine(struct nodoVect *v,tipobaseList y,unsigned int dim){
- unsigned int i;
- position p;
- char scala;
- i=Index(dim);
- do{
- printf("\n inserisci scala a\b \n");
- scanf("%c",&scala);
- FLUSH;
- switch(scala){
- case 'a':
- if(!EmptyList(v[i].ListaA)) p=Locate(v[i].ListaA,y);
- if(!EmptyList(v[i].ListaA) && p!=End(v[i].ListaA))
- printf("\nelemento già presente");
- else{
- y=Retrieve(v[i].ListaA,p);
- AzzeraSpese(&y);
- Insord(&(v[i].ListaA),y);
- }
- break;
- case 'b':
- if(!EmptyList(v[i].ListaB)) p=Locate(v[i].ListaB,y);
- if(!EmptyList(v[i].ListaB) && p!=End(v[i].ListaB))
- printf("\nelemento già presente");
- else{
- y=Retrieve(v[i].ListaB,p);
- AzzeraSpese(&y);
- Insord(&(v[i].ListaB),y);
- }
- break;
- }
- }while(scala!='a' && scala!='b');
- printf("\n\napposto\n\n");
- }
- void AggiornaSpese(struct nodoVect *v,tipobaseList y,unsigned int dim){
- unsigned int i;
- position p;
- char scala;
- i=Index(dim);
- do{
- printf("\n inserisci scala a\b \n");
- scanf("%c",&scala);
- switch(scala){
- case 'a':
- if(!EmptyList(v[i].ListaA)) p=Locate(v[i].ListaA,y);
- if(EmptyList(v[i].ListaA) || p==End(v[i].ListaA))
- printf("condomine non presente");
- else{
- x=Retrieve(v[i].ListaA,p);
- DeleteList(&(v[i].ListaA),p);
- InsertList(&(v[i].ListaA),p,y); printf("\nappostooooo\n");
- }
- break;
- case 'b':
- if(!EmptyList(v[i].ListaB)) p=Locate(v[i].ListaB,y);
- if(EmptyList(v[i].ListaB) || p==End(v[i].ListaB))
- printf("condomine non presente");
- else{
- x=Retrieve(v[i].ListaB,p);
- DeleteList(&(v[i].ListaB),p);
- InsertList(&(v[i].ListaB),p,y); printf("\nappostooooo\n");
- }
- break;
- }
- }while(scala!='a' && scala!='b');
- printf("\n\napposto\n\n");
- }
Add Comment
Please, Sign In to add comment