#include<stdio.h>
#include<string.h>
#define NAMEL 21
#define SEATS 12
struct giant {
int seat_number;
_Bool is_ordered;
char fname[NAMEL];
char lname[NAMEL];
};
void showmenu(void);
void eatline(void);
char* s_gets(char* st, int n);
int main(void)
{
char choice;
int i, empty_seats_count = 0, assign_number,airline;
struct giant plane[SEATS];
struct giant airline102[SEATS];
struct giant airline311[SEATS];
struct giant airline444[SEATS];
struct giant airline519[SEATS];
struct giant* pts[SEATS];
struct giant* temp;
for (i = 0; i < SEATS; i++)
{
plane[i].is_ordered = 0;
plane[i].fname[0] = '\0';
plane[i].lname[0] = '\0';
plane[i].seat_number = i + 1;
airline102[i] = plane[i];
airline311[i] = plane[i];
airline444[i] = plane[i];
airline519[i] = plane[i];
}
printf("Input the number of the airline to select it(input 0 to quit):");
while ((scanf_s("%d", &airline) == 1) && airline != 0)
{
eatline();
switch (airline)
{
case 0:
goto quit;
case 102:
for (i = 0; i < SEATS; i++)
pts[i] = &airline102[i];
break;
case 311:
for (i = 0; i < SEATS; i++)
pts[i] = &airline311[i];
break;
case 444:
for (i = 0; i < SEATS; i++)
pts[i] = &airline444[i];
break;
case 519:
for (i = 0; i < SEATS; i++)
pts[i] = &airline519[i];
break;
}
printf("Now you are operating airline%d.\n", airline);
showmenu();
while ((scanf("%c", &choice) == 1) && choice != 'f')
{
eatline();
switch (choice)
{
case 'a':
printf("Number of empty seats:\n");
for (i = 0; i < SEATS; i++)
if (pts[i]->is_ordered == 0)
empty_seats_count++;
printf("There are %d empty seats.\n", empty_seats_count);
empty_seats_count = 0;
break;
case 'b':
printf("List of empty seats:\n");
for (i = 0; i < SEATS; i++)
if (pts[i]->is_ordered == 0)
printf("Seat number %2d.\n", pts[i]->seat_number);
break;
case 'c':
printf("Alphabetical list of seats depend on customer's first name:\n");
for (i = 0; i < SEATS - 1; i++)
for (int j = i + 1; j < SEATS; j++)
if ((strcmp(pts[i]->fname, pts[j]->fname)) > 0)
{
temp = pts[i];
pts[i] = pts[j];
pts[j] = temp;
}
for (i = 0; i < SEATS; i++)
if (pts[i]->is_ordered == 1)
printf("%s %s:seat number %2d.\n", pts[i]->fname,
pts[i]->lname, pts[i]->seat_number);
break;
case 'd':
printf("Input the seat number you wanna assign:");
scanf_s("%d", &assign_number);
eatline();
for (i = 0; i < SEATS; i++)
{
if (pts[i]->seat_number == assign_number)
{
printf("Input your first name:");
s_gets(pts[i]->fname, NAMEL);
printf("Input your last name:");
s_gets(pts[i]->lname, NAMEL);
pts[i]->is_ordered = 1;
}
}
break;
case 'e':
printf("Input the seat number you wanna delete assignment:");
while (scanf_s("%d", &assign_number) != 1 || assign_number < 1 || assign_number>12)
printf("Input seat number from 1 to 12");
eatline();
for (i = 0; i < SEATS; i++)
{
if (pts[i]->seat_number == assign_number)
{
pts[i]->is_ordered = 0;
pts[i]->fname[0] = '\0';
pts[i]->lname[0] = '\0';
}
}
break;
case 'f':;
case 'g':
printf("Confirm seat assignment:\n");
for (i = 0; i < SEATS; i++)
{
if (pts[i]->is_ordered == 1)
printf("%s %s:seat number %2d.\n", pts[i]->fname,
pts[i]->lname, pts[i]->seat_number);
}
break;
default:
printf("Input a,b,c,d,e,f or g to seltect.\n");
continue;
}
showmenu();
printf("Now you are operating airline%d.\n", airline);
}
printf("Input the number of the airline to select it(input 0 to quit):");
}
quit:return 0;
}
void showmenu(void)
{
printf("To choose a function, enter its letter label :\n"
"a) Show number of empty seats\nb) Show list of empty seats\n"
"c) Show alphabetical list of seats\nd) Assign a customer to a seat assignment\n"
"e) Delete a seat assignment\nf) Quit\ng) Confirm the seat assignment\n");
}
void eatline(void)
{
while (getchar() != '\n')
continue;
}
char* s_gets(char* st, int n)
{
char* ret_val, * find;
if (ret_val = fgets(st, n, stdin))
if (find = strchr(st, '\n'))
*find = '\0';
else
while (getchar() != '\n')
continue;
return ret_val;
}