#include <stdio.h>
#include <string.h>
#define MAXNAME 20
#define MAXSEAT 12
#define HB 4
//结构框架
struct seat{
int number;
int reserve;
char name[MAXNAME];
char sname[MAXNAME];
char yn;
};
//预定座位
void Reserved_seats(struct seat *p);
//输入
char *s_gets(char *st, int n);
//删除预定
void delete_reserve(struct seat *p);
//按字母顺序排列
void alphabetical_order(struct seat *p);
int main(void)
{
char ch=0;
int i,seat_empty=MAXSEAT;
int j;
//初始化结构,reserve成员1代表没有订购,0代表已经订购。
struct seat plane[HB][MAXSEAT]={{
{1,1,’\0’,’\0’,’\0’},
{2,1,’\0’,’\0’,’\0’},
{3,1,’\0’,’\0’,’\0’},
{4,1,’\0’,’\0’,’\0’},
{5,1,’\0’,’\0’,’\0’},
{6,1,’\0’,’\0’,’\0’},
{7,1,’\0’,’\0’,’\0’},
{8,1,’\0’,’\0’,’\0’},
{9,1,’\0’,’\0’,’\0’},
{10,1,’\0’,’\0’,’\0’},
{11,1,’\0’,’\0’,’\0’},
{12,1,’\0’,’\0’,’\0’}
},
{
{1,1,’\0’,’\0’,’\0’},
{2,1,’\0’,’\0’,’\0’},
{3,1,’\0’,’\0’,’\0’},
{4,1,’\0’,’\0’,’\0’},
{5,1,’\0’,’\0’,’\0’},
{6,1,’\0’,’\0’,’\0’},
{7,1,’\0’,’\0’,’\0’},
{8,1,’\0’,’\0’,’\0’},
{9,1,’\0’,’\0’,’\0’},
{10,1,’\0’,’\0’,’\0’},
{11,1,’\0’,’\0’,’\0’},
{12,1,’\0’,’\0’,’\0’}
},
{
{1,1,’\0’,’\0’,’\0’},
{2,1,’\0’,’\0’,’\0’},
{3,1,’\0’,’\0’,’\0’},
{4,1,’\0’,’\0’,’\0’},
{5,1,’\0’,’\0’,’\0’},
{6,1,’\0’,’\0’,’\0’},
{7,1,’\0’,’\0’,’\0’},
{8,1,’\0’,’\0’,’\0’},
{9,1,’\0’,’\0’,’\0’},
{10,1,’\0’,’\0’,’\0’},
{11,1,’\0’,’\0’,’\0’},
{12,1,’\0’,’\0’,’\0’}
},
{
{1,1,’\0’,’\0’,’\0’},
{2,1,’\0’,’\0’,’\0’},
{3,1,’\0’,’\0’,’\0’},
{4,1,’\0’,’\0’,’\0’},
{5,1,’\0’,’\0’,’\0’},
{6,1,’\0’,’\0’,’\0’},
{7,1,’\0’,’\0’,’\0’},
{8,1,’\0’,’\0’,’\0’},
{9,1,’\0’,’\0’,’\0’},
{10,1,’\0’,’\0’,’\0’},
{11,1,’\0’,’\0’,’\0’},
{12,1,’\0’,’\0’,’\0’}
}
};
struct seat *p;
int flight[HB]={102,311,444,519};
Help: do
{
puts(“Please enter the flight you want to select:”);
printf(“0) 102 Flight. 1) 311 Flight.\n”);
printf(“2) 444 Flight. 3) 519 Flight.\n”);
printf(“4) Quit\n”);
scanf("%d",&j);
while(getchar()!=’\n’)
continue;
if(4==j)
return 0;
}while(j<0 && j>=4);
p=plane[j];
printf("You are booking flight %d!\n",flight[j]);
puts("To choose a function, enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of emptyseats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) Confirm seat allocation!");
puts("g) Quit");
//退出条件预定满 或F;
while(seat_empty>0 && ch!='g')
{
scanf("%c",&ch);
while(getchar()!='\n')
continue;
//选择
switch(ch)
{
case 'a': puts("Show number of empty seats");
for(i=0;i<MAXSEAT;i++)
if(0==plane[j][i].reserve)
seat_empty=seat_empty-1;
printf("There are %2d more seats to reserve.\n\n\n",seat_empty);
break;
case 'b': puts("Show list of emptyseats");
for(i=0;i<MAXSEAT;i++)
if(plane[j][i].reserve)
printf("Seat %2d can be reserved.\n",plane[j][i].number);
break;
case 'c': alphabetical_order(p);
break;
case 'd': Reserved_seats(p);
break;
case 'e': delete_reserve(p);
break;
case 'f': for(i=0;i<MAXSEAT;i++)
{
if('\0'==plane[j][i].yn &&0==plane[j][i].reserve)
{
plane[j][i].yn='y';
printf("Seat %d on your flight %d was booked successfully\n",
plane[j][i].number,flight[j]);
}
}
break;
case 'g': ch=0;
goto Help;
}
printf("You are booking flight %d!\n",flight[j]);
puts("To choose a function, enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of emptyseats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) Confirm seat allocation!");
puts("g) Quit");
}
//如果预定满了
if(0==seat_empty)
puts("Sorry, the tickets are sold out.");
puts("Done!");
return 0;
}
void Reserved_seats(struct seat *p)
{
int n;
puts("Please enter the seat number you want to reserve. (1-12)");
puts("\"g\" Quit");
while(scanf("%d",&n)==1 && n!='g')
{
while(getchar()!='\n')
continue;
//如果座位已预定
if(!(p+(n-1))->reserve)
{
printf("Seat %2d is reserved.\n",(p+(n-1))->number);
puts("Please enter the seat number you want to reserve. (1-12)");
puts("\"g\" Quit");
continue;
}
(p+(n-1))->reserve=0;
printf("Please enter your name\n");
s_gets((p+(n-1))->name,MAXNAME);
printf("please enter your sname\n");
s_gets((p+(n-1))->sname,MAXNAME);
printf("You have reserved seat %2d. Your name is: %s %s.\n\n",
(p+(n-1))->number,
(p+(n-1))->name,
(p+(n-1))->sname);
puts("Please enter the seat number you want to reserve. (1-12)");
//这里有个问题如果和主函数里循环同样输入‘F'退出,会直接退出主程序。是栈的问题吗?有知道原因的请告诉我谢谢。
puts("\"q\" Quit");
}
while(getchar()!='\n')
continue;
}
void delete_reserve(struct seat *p)
{
int n,i;
char a;
puts(“Please enter the reserved seat number to be deleted.(f)Quit”);
while(scanf("%d",&n)==1 && n!=‘g’)
{
while(getchar()!=’\n’)
continue;
printf(“Please check your information:\n %d %s %s.\n”,
(p+(n-1))->number,(p+(n-1))->name,(p+(n-1))->sname);
puts(“Please confirm the information: (y or n)”);
//确认是否删除
scanf("%c",&a);
while(getchar()!=’\n’)
continue;
if(‘y’==a || ‘Y’==a)
{
(p+(n-1))->reserve=1;
for(i=0;i<MAXNAME;i++)
{
(p+(n-1))->name[i]=’\0’;
(p+(n-1))->sname[i]=’\0’;
}
puts(“Information has been deleted!”);
puts("\n");
break;
}
else if(‘N’==a|| ‘n’==a)
break;
}
while(getchar()!=’\n’)
continue;
}
void alphabetical_order(struct seat p)
{
struct seat k[MAXSEAT];
struct seat j;
int i,n;
for(i=0;i<MAXSEAT;i++)
k[i]=(p+i);
for(i=0;i<MAXSEAT-1;i++)
for(n=i+1;n<MAXSEAT;n++)
if(strcmp(k[i].name,k[n].name)>0)
{
j=k[i];
k[i]=k[n];
k[n]=j;
}
for(i=0;i<MAXSEAT;i++)
if(0==k[i].reserve)
printf("%s %s %d\n",k[i].name,k[i].sname,k[i].number);
}
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;
ret_val=fgets(st, n, stdin);
if(ret_val)
{
find = strchr(st, '\n');
if(find)
*find='\0';
else
while(getchar()!='\n')
continue;
}
return ret_val;
}