void delet_seat(struct planestats p[], int n) { int number, seat; show_seats(p,n); puts("Enter the seat ID (1 ~ 12) you choice:"); while(scanf("%d",&number) != 1 || number < 1 || number > 12) { scanf("%*s"); puts("Enter number (1 ~ 12)"); } while(getchar() != '\n'); for(seat = 0; seat < n; seat++) if(p[seat].seat_id == number) { if(p[seat].status == EMPTY) printf("NO.%d seat is already empty!\n",p[seat].seat_id); else { p[seat].status = EMPTY; printf("delet NO.%d seat successfully!\n",p[seat].seat_id); } } }
int main(void) { struct planestats plane[SEATS]; int choice; int i; size_t size = sizeof(struct planestats); FILE *fp; if((fp = fopen("air.dat","rb")) == NULL) { fprintf(stderr, "Can't open file %s","air.dat\n"); if((fp = fopen("air.dat","wb")) == NULL) { exit(1); } for(i = 0; i < SEATS; i++) { plane[i].seat_id = i + 1; plane[i].status = 0; } fwrite(plane, size , SEATS, fp); puts("We created the file air.dat now."); exit(1); } for(i = 0; i < SEATS; i++) { plane[i].seat_id = i + 1; plane[i].status = 0; } fread(plane, size , SEATS, fp); fclose(fp); while((choice = getmenu()) != 'f') { switch(choice) { case 'a': printf("%d seat is empty!\n",empty_seat(plane,SEATS)); break; case 'b': show_empty(plane,SEATS); break; case 'c': show_seats(plane,SEATS); break; case 'd': assign_seat(plane,SEATS); break; case 'e': delet_seat(plane,SEATS); break; } } if((fp = fopen("air.dat","wb")) == NULL) { fprintf(stderr, "Can't open file %s","air.dat"); exit(1); } fwrite(plane, size , SEATS, fp); fclose(fp); puts("Bye!"); return 0; }
void execute_input(char * input){ char cmd[15]; char id[3]; char times[5]; int i; int cant; for(i=0; i<15 && input[i]!= '\0' && input[i]!=' ' && input[i]!= '\n';i++){ cmd[i]=input[i]; } cmd[i] = '\0'; if(strcmp(cmd, "ShowMovies")==0){ show_movies(); return; } if(strcmp(cmd,"ReserveSeat")==0){ for(i=0;i<2 && input+(12+i)!='\0' && input[12+i]!=' ';i++){ id[i]=*(input+(12+i)); } id[2] = '\0'; if(i!=2|| input[14] !=' '){ printf("%s\n", "Not valid arguments."); return; } for(i=0;i<4 && input+(15+i) !='\0' && input[15+i]!=' ';i++){ times[i]=*(input+(15+i)); } if(i!=4 || input[19] != ' '){ printf("%s\n", "Not valid arguments."); return; } times[4]='\0'; cant=atoi(input+20); if(cant==0){ printf("%s\n","Not valid arguments" ); return; } if(getSeats(id, times) < cant){ printf("The are not enough seats available.\n", cant); return; } reserveSeat(id,times,cant); return; } if(strcmp(cmd, "CheckSeats")==0){ for(i=0;i<2 && input+(11+i)!='\0' && input[11+i]!=' ';i++){ id[i]=*(input+(11+i)); } id[2] = '\0'; if(i!=2|| input[13] !=' '){ printf("%s\n", "Not valid arguments."); return; } for(i=0;i<4 && input+(14+i) !='\0' && input[14+i]!=' ';i++){ times[i]=*(input+(14+i)); } if(i!=4){ printf("%s\n", "Not valid arguments."); return; } times[4]='\0'; show_seats(id,times); return; } if(strcmp(cmd, "Help")==0){ printf("ShowMovies: displays a list of all the movies available.\n\n"); printf("CheckSeats: indicates how many seats left are available for that movie. You will have to indicate the ID and time of the movie.\n"); printf(" Example: 'CheckSeats 09 2100' will indicate the number of seats left for the movie which has 09 as id and airs at 2100\n\n"); printf("ReserveSeats: reserves the seats for a specific movie at a specific time. You will have to indicate the id and time of the movie as well as the amount of seats to reserve.\n"); printf(" Example: 'ReserveSeats 02 2100 5' will reserve 5 seats for the movie which has id 02 at 2100"); } printf("%s\n", "Invalid option.\n"); }