0

## 程式碼真正想跟你說的話

``````int main()
{
int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
int i, j, seat, number;

showSeats(seats, 10);

getUserInput(seat, number);

while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;

showSeats(seats, 10);

getUserInput(seat, number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}

bubble_sort(seats, 10);

showSeats(seats, 10);

printf("\n");
printf("***************\n");

return 0;
}
``````

``````int main()
{
const int size = 10;
int seats[size] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
int seat, number;

showSeats(seats, size);

getUserInput(seat, number);

while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;

showSeats(seats, size);

getUserInput(seat, number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}

bubble_sort(seats, size);

showSeats(seats, size);

printf("\n");
printf("***************\n");

return 0;
}
``````

1. 顯示所有位置資訊
2. 輸入位置索引和號碼
3. 判斷位置是否合法然後塞值
4. 判斷是否繼續輸入(使用者輸入數字不等於0)

``````    showSeats(seats, size);

getUserInput(seat, number);

while(number != 0){
if(seats [seat - 1] == 0){
seats[seat - 1] = number;

showSeats(seats, size);

getUserInput(seat, number);
}
else{
printf("Sorry, seat is taken.\n");
scanf("%d %d", &seat, &number);
}
}
``````

``````    do{
showSeats(seats, size);
getUserInput(seat, number);
if(isSeatValid(seats, size, seat)){
seats[seat - 1] = number;
}

}while(isContinueInput(number));
``````

``````

void showSeats(int seats[], int size)
{
printf("*seating*\n");
for(int i = 0; i < size; ++i){
printf("%d ", seats[i]);
}
}

void getUserInput(int& seat, int& number){
printf("\n");
printf("***************\n");
printf("Please input the seat (0~9) and number(-1 to end game)\n");
scanf("%d %d", &seat, &number);
}

bool isSeatValid(int seats[], int size, int seat){
if(seat - 1 < 0 || seat - 1 >= size){
printf("Sorry, seat is invalid.\n");
return false;
}

if(seats[seat - 1] != 0){
printf("Sorry, seat is taken.\n");
return false;
}

return true;
}

bool isContinueInput(int number){
return number != 0;
}

int main()
{
const int size = 10;
int seats[size] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
int seat, number;

do{
showSeats(seats, size);
getUserInput(seat, number);
if(isSeatValid(seats, size, seat)){
seats[seat - 1] = number;
}
}while(isContinueInput(number));

bubble_sort(seats, size);

showSeats(seats, size);

printf("\n");
printf("***************\n");

return 0;
}
``````