-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaneHomework_function.cpp
More file actions
180 lines (151 loc) · 4.26 KB
/
PlaneHomework_function.cpp
File metadata and controls
180 lines (151 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ROWS 10
#define COLS 6
int seat[ROWS][COLS];
// 初始化座位
void initializeSeats(int fCsettings, int eCsettings) {
int count = 1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (count <= fCsettings + eCsettings) {
seat[i][j] = count;
count++;
} else {
seat[i][j] = 0;
}
}
}
}
// 顯示座位
void displaySeats() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if(seat[i][j] == 0) {
printf("0 ");
} else if(seat[i][j] < 10) {
printf("%d ", seat[i][j]);
} else {
printf("%d ", seat[i][j]);
}
}
printf("\n");
}
}
// 查找座位
void findSeat(int num, int firstCount) {
int found = 0;
for (int i = 0; i < ROWS && !found; i++) {
for (int j = 0; j < COLS && !found; j++) {
if (seat[i][j] == num) {
char row = 'A' + i;
int col = j + 1;
if (num <= firstCount) {
printf("座位 %d 頭等艙, 位置 %c%d\n", num, row, col);
} else {
printf("座位 %d 經濟艙, 位置 %c%d\n", num, row, col);
}
found = 1;
}
}
}
if (!found) {
printf("查無座位 %d\n", num);
}
}
// 更換座位
void changeSeat(int change1, int change2) {
int pos1_i = -1, pos1_j = -1;
int pos2_i = -1, pos2_j = -1;
// 找到兩個座位位置
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (seat[i][j] == change1) {
pos1_i = i;
pos1_j = j;
}
if (seat[i][j] == change2) {
pos2_i = i;
pos2_j = j;
}
}
}
// 如果座位不存在
if (pos1_i == -1 || pos2_i == -1) {
printf("錯誤: 有座位不存在, 無法進行更換\n");
return;
}
// 進行座位交換
int temp = seat[pos1_i][pos1_j];
seat[pos1_i][pos1_j] = seat[pos2_i][pos2_j];
seat[pos2_i][pos2_j] = temp;
printf("已將座位 %d 與座位 %d 交換\n", change1, change2);
displaySeats();
}
// 用戶設置座位數量
void setupSeats(int* fCsettings, int* eCsettings) {
int total;
do {
printf("請輸入頭等艙乘客數量: ");
fflush(stdin);
scanf("%d", fCsettings);
printf("請輸入經濟艙乘客數量: ");
fflush(stdin);
scanf("%d", eCsettings);
total = *fCsettings + *eCsettings;
// 檢查是否超過最大數量
if (*fCsettings > 12) {
printf("預約座位數量超過最大限制 (12)\n");
} else if (*eCsettings > 48) {
printf("空座位數量超過最大限制 (48)\n");
} else if (total > 60) {
printf("總座位數量超過最大限制 (60)\n");
}
} while (total > 60 || *fCsettings > 12 || *eCsettings > 48);
}
int main(void) {
int fCsettings, eCsettings, firstSearch, change1, change2;
printf("座位配置: 12 預約座位和 48 空座位,共 60 座位\n");
// 設定座位數量
setupSeats(&fCsettings, &eCsettings);
// 初始化座位
initializeSeats(fCsettings, eCsettings);
printf("\n初始座位配置:\n");
displaySeats();
// 查找座位
while (1) {
printf("\n請輸入查詢座位編號 (0退出): ");
fflush(stdin);
scanf("%d", &firstSearch);
if (firstSearch == 0) {
break;
}
findSeat(firstSearch, fCsettings);
}
// 更換座位
while (1) {
printf("請輸入要更換的兩個座位編號 (0退出): ");
fflush(stdin);
scanf("%d %d", &change1, &change2);
if (change1 == 0) {
break;
}
if (change1 != change2) {
changeSeat(change1, change2);
} else {
printf("錯誤: 不可以將座位與自己交換\n");
}
}
// 等待用戶按下空格鍵重新開始
while (1) {
printf("按空格鍵重新開始或其他任意鍵退出...\n");
if (_getch() != 32) {
return 0;
} else {
system("cls");
main();
}
}
return 0;
}