http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=297380724
# include <stdio.h>
# include <stdlib.h>
# define SIZE 15
# define ERROR 1
# define EMPTY 0
//-------------------------------------
typedef struct MovieSeat {
int seat[SIZE][SIZE]; // 15 X 15
}MovieSeat, *ptrMovieSeat;
//-------------------------------------
// $function prototype$
ptrMovieSeat retNode(); // #1.
void SeatMenu(); // #2.
void AllSeat(MovieSeat** s); // #3.
void ChoiceSeat(MovieSeat** s); // #4.
void ReserveSeat(MovieSeat** s); // #5.
//-------------------------------------
int main(void) {
ptrMovieSeat mySeat = retNode();
int choiceNumber = 0;
while (1) { // 무한 루프
SeatMenu();
scanf("%d", &choiceNumber);
if (choiceNumber < 1 || choiceNumber > 4) {
printf("입력 에러\n");
}
else {
// choiceNumber >= 1 && choiceNumber <= 4
switch (choiceNumber) {
case 1: ChoiceSeat(&mySeat); break;
case 2: AllSeat(&mySeat); break;
case 3: ReserveSeat(&mySeat); break;
case 4: printf("프로그램 종료 \n");
goto end;
}
}
printf("<Enter>");
getch();
system("cls");
}
end:
free(mySeat); // 메모리 해제
return 0;
} // end of main function
ptrMovieSeat retNode() { // #1.
ptrMovieSeat node = (ptrMovieSeat)malloc(sizeof(MovieSeat));
if (node == NULL) {
fprintf(stderr, "malloc error ... !!!\n");
exit(ERROR);
}
else {
// 데이터 초기화
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
node->seat[i][j] = EMPTY;
}
}
return node;
}
} // end of retNode function
void SeatMenu() { // #2.
printf("---------------------------------------\n");
printf(" >>>>> 좌석 예약 시스템 입니다. <<<<<< \n");
printf("1번 : 좌석 예약 <<<<<<<<<<<<<<<<<<<<< \n");
printf("2번 : 전체 좌석 보기 <<<<<<<<<<<<<<<<< \n");
printf("3번 : 예약된 좌석 정보 보기 <<<<<<<<<< \n");
printf("4번 : 예약 끝내기 <<<<<<<<<<<<<<<<<<< \n");
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n");
printf("입력 : ");
} // end of SeatMenu function
void AllSeat(MovieSeat** s) { // #3.
printf("전체 좌석 정보 (0:비어 있는 좌석)/ (1:예약된 좌석) \n");
printf("====================================\n");
for (int i = 0; i <= SIZE; i++) {
for (int j = 0; j <= SIZE; j++) {
if (i == 0 && j == 0) {
printf(" ");
}
else {
// i != 0 or j != 0
if (i == 0 && j != 0) {
printf("%02d ", j);
}
else if (i!=0 && j == 0) {
printf("%02d ", i);
}
else { // i != 0 && j != 0
printf("%02d ", (*s)->seat[i-1][j-1]);
}
}
}
printf("\n");
}
printf("====================================\n");
} // end of AllSeat function
void ChoiceSeat(MovieSeat** s) { // #4.
int rSeat; // 행
int cSeat; // 열
printf("예약 좌석 번호 (행)입력 : ");
scanf("%d", &rSeat);
printf("예약 좌석 번호 (열)입력 : ");
scanf("%d", &cSeat);
if ((**s).seat[rSeat-1][cSeat-1] != 0) {
printf("이미 예약된 좌석입니다. \n");
}
else {
if (rSeat <= 0 || rSeat > 15) {
printf("좌석의 범위가 맞지 않습니다. (1~15)\n");
}
else {
// 비어 있는 좌석이라면
// (**s).seat[rSeat][cSeat] == 0
(**s).seat[rSeat-1][cSeat-1] = 1;
}
}
} // end of ChoiceSeat function
void ReserveSeat(MovieSeat** s) {
printf("이미 예약된 좌석들의 정보 입니다. \n");
printf("======================================================\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if ((** s).seat[i][j] == 1) {
printf("seat[%d][%d]\n",i+1, j+1);
}
}
}
printf("======================================================\n");
} // end of ReserveSeat function
'언어 > c언어' 카테고리의 다른 글
네이버 지식인 문제 _01 (0) | 2018.06.12 |
---|---|
네이버 지식인 문제 (0) | 2018.03.19 |
네이버 풀이 (0) | 2018.03.11 |
stack (0) | 2018.03.04 |
오랜만에 (0) | 2018.01.24 |