#include #include #include #define MAXPAROLE 11 #define MAXGIORNO 2 struct _aule{ char lun[MAXGIORNO],mar[MAXGIORNO],mer[MAXGIORNO],gio[MAXGIORNO]; char nome[MAXPAROLE],ven[MAXGIORNO],sab[MAXGIORNO]; int posti; struct _aule *next; }; typedef struct _aule aule; aule *head=NULL; FILE *fp; int ok_giorno=0,ok_posti=1; void acquisisci(void); void salva(void); int prenota(void); void free_all(void); int main(void) { int scelta; char temp[3]; acquisisci(); do{ printf("[1]Prenota\n[2]Salva\n[3]Esci\n--> "); gets(temp); scelta=atoi(temp); switch(scelta){ case 1: prenota(); break; case 2: salva(); free_all(); acquisisci(); break; case 3: return 1; default: printf("Opzione errata\n"); } }while(scelta!=3); return 0; } void acquisisci(void) { char temp[80]; aule *p1; if((fp=fopen("aule.txt","r"))==NULL) { printf("Impossibile aprire il file\n"); exit(1); } while(fgets(temp,81,fp)!=NULL) { if(!head) { head=(aule*)malloc(sizeof(aule)); sscanf(temp,"%s %d %s %s %s %s %s %s",&head->nome,&head->posti,&head->lun,&head->mar,&head->mer,&head->gio,&head->ven,&head->sab); head->next=NULL; } else { p1=head; head=(aule*)malloc(sizeof(aule)); sscanf(temp,"%s %d %s %s %s %s %s %s",&head->nome,&head->posti,&head->lun,&head->mar,&head->mer,&head->gio,&head->ven,&head->sab); head->next=p1; } } fclose(fp); } int prenota(void) { aule *p1; int ok; char giorno[MAXPAROLE],temp[20]; int numero; printf("Inserisci il giorno: "); gets(giorno); if(strlen(giorno)==0) ok_giorno=1; printf("Inserisci il numero di posti richiesti: "); gets(temp); if(strlen(temp)==0) ok_posti=0; else { do{ numero=atoi(temp); if(numero<=0 || numero>200) { printf("Numero errato. Digitare un numero compreso tra 1 e 200: "); gets(temp); numero=atoi(temp); } }while(numero<=0 || numero>200); } for(p1=head;p1!=NULL;p1=p1->next) { ok=0; if(ok_giorno || !strcmpi(giorno,"Lunedi'")) { if(p1->lun[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->lun,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } if(ok_giorno || !strcmpi(giorno,"Martedi'")) { if(p1->mar[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->mar,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } if(ok_giorno || !strcmpi(giorno,"Mercoledi'")) { if(p1->mer[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->mer,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } if(ok_giorno || !strcmpi(giorno,"Giovedi'")) { if(p1->gio[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->gio,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } if(ok_giorno || !strcmpi(giorno,"Venerdi'")) { if(p1->ven[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->ven,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } if(ok_giorno || !strcmp(giorno,"Sabato")) { if(p1->sab[0]=='1') ok=0; else if(numero<=p1->posti || !ok_posti) { strcpy(p1->sab,"1\0"); printf("aula prenotata\n"); return 1; } else ok=0; } } if(!ok) printf("Impossibile prenotare\n"); return 0; } void free_all(void) { aule *p0,*p1; int ok=0; p1=head; if(head==NULL) ok=1; while(!ok) { p0=p1; p1=p1->next; free(p0); if(p1==NULL) ok=1; } head=NULL; } void salva(void) { aule *p1; fp=fopen("aule.txt","w"); for(p1=head;p1!=NULL;p1=p1->next) fprintf(fp,"%s %d %s %s %s %s %s %s \n",p1->nome,p1->posti,p1->lun,p1->mar,p1->mer,p1->gio,p1->ven,p1->sab); fclose(fp); }