#include <stdio.h>
int spec_nosmk()
{
char c[4];
while (1) {
printf
("\n喫煙席を希望する方は 1 をタイプしてください。\n");
printf
("禁煙席を希望する方は 2 をタイプしてください。\n");
fgets(c, 3, stdin);
if ((*c == '1')
|| (*c == '2'))
break;
}
return (*c == '1') ? 1 : 2;
}
int spec_aside()
{
char c[4];
while (1) {
printf
("\n窓側を希望する方は 1 をタイプしてくださ。\n");
printf
("通路側を希望する方は 2 をタイプしてください。\n");
fgets(c, 3, stdin);
if ((*c == '1')
|| (*c == '2'))
break;
}
return (*c == '1') ? 1 : 2;
}
int req_change(int from)
{
char c[4];
while (1) {
printf
("\n%s区画が窓側又は通路側ともに満席です。\n",
from == 1 ? "喫煙" : "禁煙");
printf("%s区画でもよろしいですか(y/n)?",
from == 1 ? "禁煙" : "喫煙");
fgets(c, 3, stdin);
if ((*c == 'y')
|| (*c == 'n'))
break;
}
return *c == 'y';
}
int find_seat(int nosmk, int aside, int mode)
{
static int seat[6][4];
int i, j, zan = 0;
switch (mode) {
case 0: //initialize
for (i = 0; i < 2; i++) //喫煙席
for (j = 0; j < 4; j++)
seat[i][j] = 10;
for (i = 2; i < 6; i++) //禁煙席
for (j = 0; j < 4; j++)
seat[i][j] = 20;
for (i = 0; i < 6; i++) //
for (j = 0; j < 4; j++)
seat[i][j] += 1;
for (i = 0; i < 6; i++) //通路側
for (j = 1; j < 3; j++)
seat[i][j] += 1;
break;
case 1: //fid seat
for (i = 0; i < 6; i++) //通路側
for (j = 0; j < 4; j++)
if (seat[i][j] == nosmk * 10 + aside) {
printf("---------\n");
printf("%d%c %s\n", i + 1, 'A' + j,
(seat[i][j] / 10 ==
1) ? "喫煙席" : "禁煙席");
printf("---------\n");
seat[i][j] = 0;
return 1;
}
return 0;
case 2: //display
printf("残席状況:\n--------------\n");
for (i = 0; i < 6; i++) {
for (j = 0; j < 4; j++) {
if (seat[i][j]) {
printf("%d%c ", i + 1, j + 'A');
zan++;
} else
printf("== ");
}
putchar('\n');
}
printf("--------------\n");
return zan;
}
return 0;
}
int main(void)
{
int nosmk, aside;
char as[][10] = { "窓側", "通路側" };
find_seat(0, 0, 0); //initialize
while (1) {
if (find_seat(0, 0, 2) == 0) { //display
printf("満席です。\n");
break;
}
nosmk = spec_nosmk();
aside = spec_aside();
if (find_seat(nosmk, aside, 1))
continue;
printf("「%sはすでに満席のため%sで予約します」\n",
as[aside - 1], as[2 - aside]);
if (find_seat(nosmk, 3 - aside, 1))
continue;
if (req_change(nosmk)) {
if (find_seat(3 - nosmk, aside, 1))
continue;
find_seat(3 - nosmk, 3 - aside, 1);
}
printf
("「3時間後に次のフライトがあります。」\n");
}
return 0;
}