#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct seat
{
int id;
int booked;
char name[20];
}list[13]={0};
void show_menu(void);
void get_empty(struct seat list[]);
void show_empty(struct seat list[]);
void show_booked(struct seat list[]);
void book_seat(struct seat list[]);
void cancel_book(struct seat list[]);
int main(int argc, char *argv[])
{
char selected;
show_menu();
while((selected=getchar())!='f')
{
switch(selected)
{
case 'a':
get_empty(list);
break;
case 'b':
show_empty(list);
break;
case 'c':
show_booked(list);
break;
case 'd':
book_seat(list);
break;
case 'e':
cancel_book(list);
break;
default:
break;
}
while(getchar()!='\n') continue;
show_menu();
}
return 0;
}
void show_menu(void)
{
puts("To chooose a function, enter is letter label: ");
puts("a)show number of empty seats ");
puts("b)show list of empty seats ");
puts("c)show alphabetical list of seats ");
puts("d)assign a customer to a seat assignment ");
puts("e)delete a seat assignment ");
puts("f)quit ");
}
void get_empty(struct seat list[])
{
int sum=0;
for(int i=1;i<13;i++)
if(list[i].booked==0)
sum++;
printf("There are %d seats empty\n",sum);
}
void show_empty(struct seat list[])
{
printf("Empty list: ");
for(int i=1;i<13;++i)
{
if(list[i].booked==0)
printf("%d ",i);
}
putchar('\n');
}
void show_booked(struct seat list[])
{
struct seat *ptstr[13];
for(int i=1;i<13;i++)
ptstr[i]=&list[i];
puts("Alphabetical list: ");
for(int i=1;i<13;i++)
{
if(ptstr[i]->booked==1)
printf("Seat NO:%d booked by %s\n",i,ptstr->name);
}
}
void book_seat(struct seat list[])
{
int id;
char name[20];
show_empty(list);
puts("Please select the seats: ");
scanf("%d ",&id);
if(list[id].booked==1)
{
puts("Error selected. ");
return;
}
list[id].id=id;
puts("Please input you name. ");
scanf("%s",name);
strcpy(list[id].name,name);
list[id].booked=1;
puts("Booked!");
write(list);
}
void cancel_book(struct seat list[])
{
show_booked(list);
char space[1]={'\0'};
int id;
puts("Please select the seat to cancel: ");
scanf("%d",&id);
if(list[id].booked==0)
{
puts("Error selected. ");
return;
}
list[id].booked=0;
strcpy(list[id].name,space);
write(list);
}
int write(strcpy seat list[])
{
FILE *fp;
fp=fopen("booked.txt","w");
fprintf(fp,"座位编号\t预定情况\t预定人姓名\n");
for(int i=1;i<13;i++)
{
fprintf(fp,"%d\t",list[i].id);
fprintf(fp,"%d\t",list[i].booked);
fprintf(fp,"%s\n",list[i].name);
}
fclose(fp);
return 0;
}