[C언어] - 좌석예약 시스템

로안·2024년 4월 30일
0

C언어

목록 보기
2/5

문제

배열을 이용하여 간단한 극장 예약 시스템을 작성하여 보자. 아주 작은 극장이라서 좌석이 10개밖에 안 된다

좌석을 예약하시겠습니까?(y 또는n) y
-------------------------------
  1  2  3  4  5  6  7  8  9  10
-------------------------------
  0  0  0  0  0  0  0  0  0  0
몇 번째 좌석을 예약하시겠습니까 1
예약되었습니다.

solution

#include <stdio.h>

int main(void) {

	int seats[10] = { 0 };
    int seatNumber;
    char choice;

    do {
        printf("좌석을 예약하시겠습니까?(y 또는 n) ");
        scanf_s(" %c", &choice);

        printf("---------------------------------\n");
        for (int i = 1; i <= 10; i++) 
        {
            printf("  %d", i);
        }
        printf("\n---------------------------------\n");
        for (int i = 0; i < 10; i++) 
        {
            printf("  %d", seats[i]);
        }
        printf("\n\n");

        if (choice == 'y') {
            printf("몇 번째 좌석을 예약하시겠습니까 : ");
            scanf_s("%d", &seatNumber);

            if (seats[seatNumber - 1] == 0)   // 0인경우 빈자리이기 때문에 가능 
            {
                seats[seatNumber - 1] = 1;   // 1인경우 누군가 예약한 좌석이기 때문에 불가능
                printf("예약되었습니다.\n\n");
            }
            else {
                printf("이미 예약된 자리입니다.\n\n");
            }
        }
    } while (choice != 'n');

}


profile
MOOOMENT

0개의 댓글