问题描述

类似机房的密集设备场所,人工进行查询和预约登记的效率较低,故使用C语言进行客户的查询和预约登记,降低人工压力

功能要求

  • 从原数据文件中读取日期(年月日),时段,客户信息(姓名,性别,电话)这七个信息,放入程序进行处理
  • 存储100天的机位和客户信息,输入日期(时段)进行查询机位状态
  • 根据客户要求对某一天某一时段进行预约,若要求机位已满,自动寻找空余机位,二次询问客户需求,并更改文件中的状态值
  • 搭配查询系统,根据客户需求,对某天某时取消预约,重置机位状态

问题的解决方案

  • 设置两个结构体cusinfo和mechine分别作为客户信息和机位信息,在mechine机位信息中嵌套cusinfo客户信息的结构体数组waitlist[6]表示一天中的6个时段,设置mechine info[100]表示一百天的机位信息
  • 自定义函数search,search负责对于数据进行检索和,用户输入日期信息,程序循环遍历数据文件,检测机位状态,如果不为空即打印机位信息,遍历后找不到指定日期,即输出“未找到该日期的记录”
  • 机位预定函数buy,让用户输入日期和时间,程序从同开始遍历,找到空信息槽位即提示用户填入信息,若找到指定信息槽位已被占满,则寻找特定时间的其他日期,则提示用户查询其他日期,进行机位状态检查
  • 取消预订函数exit_buy,让用户输入原预定的日期和姓名,若信息匹配,即机位信息改为0,姓名改为None,其他信息无需改动,因为在buy和exit_buy函数中只检查机位状态
  • 文件函数,先读再写,避免源文件直接被随机值覆盖,防止修改后的值丢失。嵌套循环,让系统读取七个变量
  • 主函数,SetConsoleOutputCP(65001);使用UTF-8编码,避免运行乱码。先使用文件读取函数,再叠加以4为结束标志的while循环,循环内使用switch-case检测用户输入功能标号,对应使用功能函数

需求分析

1.1 输入形式

程序需要接受客户的功能要求(整形变量+switch-case),读取源文件的数据变量(整形,字符串)。

1.2 输出形式:

根据客户要求功能和输入信息,打印机位状态信息,并且把更改存入文件,完成数据更新。

1.3 数据约束:

日期为100天内(可以自定义txt文件),机位状态0表示有空机位,1表示没有空机位,姓名要求四字以内,电话要求不大于11位,性别使用1和0

系统概要设计

2.1 系统功能模块图:

图2.1-1 系统功能模块图

2.2 主要数据结构:

图2.2-1 数据结构图

设置两个结构体cusinfo和mechine分别作为客户信息和机位信息,在mechine机位信息中嵌套cusinfo客户信息的结构体数组waitlist[6]表示一天中的6个时段,设置mechine info[100]表示一百天的机位信息

2.3 :总体流程图

详细设计及实现

3.1 核心函数解析:

函数原型:

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
// 1. 查询功能
void search(device info[])
{
int choice;
printf("\n=== 查询功能 ===\n");
printf("1. 按日期查询所有机位状态\n");
printf("2. 按机位查询所有日期状态\n");
printf("3. 按客户信息查询预约记录\n");
printf("请选择查询方式: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
// 按日期查询
int year, month, day;
int valid_date = 0;
// 循环直到输入有效日期
while (!valid_date)
{
printf("请输入查询日期 (格式: year/month/day): ");
scanf("%d/%d/%d", &year, &month, &day);
if (validate_date(year, month, day))
{
valid_date = 1;
}
else
{
printf("请重新输入日期。\n");
}
}
int date_idx = -1;
for (int i = 0; i < day_count; i++)
{
if (info[i].year == year && info[i].month == month &&
info[i].day == day)
{
date_idx = i;
break;
}
}
if (date_idx == -1)
{
printf("该日期暂无数据。\n");
return;
}
printf("\n日期: %d年%02d月%02d日\n", year, month, day);
printf("机位状态总览:\n");
printf("机位\\时段");
for (int slot = 0; slot < Time; slot++)
{
printf("\t%s", get_time_slot_str(slot));
}
printf("\n");
// 显示每个机位的状态
for (int seat = 0; seat < States; seat++)
{
printf("机位%02d", seat + 1);
for (int slot = 0; slot < Time; slot++)
{
printf("\t%s",
info[date_idx].state[seat][slot] ? "占用" : "空闲");
}
printf("\n");
}
// 显示详细预约信息
printf("\n详细预约信息:\n");
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[date_idx].state[seat][slot] == 1)
{
printf("机位%02d %s: 顾客: %s, 性别: %s, 电话: %s\n",
seat + 1, get_time_slot_str(slot),
info[date_idx].waitlist[seat][slot].name,
info[date_idx].waitlist[seat][slot].sex == 1 ? "男"
: "女",
info[date_idx].waitlist[seat][slot].tel);
}
}
}
break;
}
case 2:
{
// 按机位查询
int seat_num;
printf("请输入要查询的机位号 (1-50): ");
scanf("%d", &seat_num);
if (seat_num < 1 || seat_num > States)
{
printf("机位号无效!\n");
return;
}
int seat_idx = seat_num - 1;
printf("\n机位 %d 的预约情况:\n", seat_num);
int found = 0;
for (int i = 0; i < day_count; i++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[i].state[seat_idx][slot] == 1)
{
found = 1;
printf("日期: %d/%02d/%02d, 时间段: %s, 顾客: %s, 性别: "
"%s, 电话: %s\n",
info[i].year, info[i].month, info[i].day,
get_time_slot_str(slot),
info[i].waitlist[seat_idx][slot].name,
info[i].waitlist[seat_idx][slot].sex == 1 ? "男"
: "女",
info[i].waitlist[seat_idx][slot].tel);
}
}
}
if (!found)
{
printf("该机位暂无预约记录。\n");
}
break;
}
case 3:
{
// 按客户信息查询
char name[20];
char tel[12];
printf("请输入客户姓名: ");
scanf("%s", name);
printf("请输入客户电话: ");
scanf("%s", tel);
printf("\n%s (%s) 的预约记录:\n", name, tel);
int found = 0;
for (int i = 0; i < day_count; i++)
{
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[i].state[seat][slot] == 1 &&
strcmp(info[i].waitlist[seat][slot].name, name) == 0 &&
strcmp(info[i].waitlist[seat][slot].tel, tel) == 0)
{
found = 1;
printf("日期: %d/%02d/%02d, 机位: %02d, 时间段: %s\n",
info[i].year, info[i].month, info[i].day,
seat + 1, get_time_slot_str(slot));
}
}
}
}
if (!found)
{
printf("未找到该客户的预约记录。\n");
}
break;
}
default:
printf("无效的选择!\n");
}
}

算法思路:
For循环遍历百天info数组,找到空信息槽位填入,多层if语句判断,不同情况下对于用户需求的处理(正好填入,定时间不定日期,完全没有合适槽位),嵌套结构体数组填入用户信息

流程图:

调试与测试

4.1 测试用例:

部分测试数据

图4.1-1 部分测试数据

正常测试:

异常测试:

4.2 遇到的问题及解决方案:

问题:

  • 11位电话字符串数组越界
  • 忘记引入机位变量
  • Switch-case不能检测字符
  • Waitlist 结构体数组不能单独使用
  • 取消预定时过多的身份判断条件和过多的源文件信息修改
  • 对于数据文件,直接写入模式会覆盖元数据为随机值
  • 没有使用UTF-8编码,导致exe文件执行时中文乱码

解决:

  • 电话字符串数组改为12
  • 性别改为数字识别
  • 搭配嵌套info[ ].wailist.[ ]
  • 只修改机位状态
  • 对数据文件先读再写
  • 引入 <windows.h>,主函数中添加SetConsoleOutputCP(65001);

设计心得与总结

课程回顾:

  • 我对于结构体,特别是嵌套结构体的运用熟悉了
  • 对于多层for循环和if判断的使用,提高了我对代码运行逻辑思考的能力
  • 文件读取和写入函数的编写,理解文件指针对于读取和写入数据作用

不足之处:

  • 没有使用链表结构
  • 对于用户查询信息准确率要求过高,特别是时间输入过于死板
  • 思考能力差,实现时段切换的方法本来不够巧妙
  • 抹除用户数据时只抹除了机位状态,这样查看数据文件时,就会看到机位为空,但是有旧客户数据的奇怪现象
  • 没有现代化的交互界面

个人感悟:
第一次写这么多代码,写完的时候,本以为语法严谨,至少没有语法报错,但是控制台足足爆出了26处语法错误,还有很多隐藏的数组越界,for和if的逻辑混乱错误,但好在主要的编程思路是对的,后面借助AI修改了错误,补全了注释

第一次代码debug

6、附录
完整源代码:

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define MAX_DAYS 100 // 最大存储天数
#define Time 6 // 6个时段:08:00-20:00,每2小时
#define States 50 // 50个机位
// 客户信息结构体
typedef struct cusinfo
{
char name[20];
int sex; // 0:女, 1:男
char tel[12]; // 11位电话号码
} cinfo;
// 机位信息结构体(按日期组织)
typedef struct mechine
{
int year;
int month;
int day;
// 50个机位,每个机位6个时段的状态和预约信息
int state[States][Time]; // 状态:0空闲,1占用,二维数组,将机位和时段绑定表示机位状态值
cinfo waitlist[States][Time]; // 预约信息
} device;
device info[MAX_DAYS]; // 存储多天的数据
int day_count = 0; // 实际存储的天数,最后打印输出此次改变的天数
// 获取时间段索引 (08:00=0, 10:00=1, 12:00=2, 14:00=3, 16:00=4, 18:00=5)
int get_time_slot_index(int hour) { return (hour - 8) / 2; }
// 获取时间段字符串
char *get_time_slot_str(int slot)
{
static char time_str
[20]; // static保持数组的静态性,防止重复执行,有static,数组在内存中只有一份
int start_hour = 8 + slot * 2;
sprintf(time_str, "%02d:00-%02d:00", start_hour,
start_hour + 2); // 格式化字符串
return time_str; // 由于是static数组,返回后仍然有效
}
// 验证日期是否有效(月份1-12,日期1-30)
int validate_date(int year, int month, int day)
{
// 检查月份
if (month < 1 || month > 12)
{
printf("月份无效!请输入1-12之间的数字。\n");
return 0;
}
// 检查日期
if (day < 1 || day > 30)
{
printf("日期无效!请输入1-30之间的数字。\n");
return 0;
}
// 检查年份(可选,可以根据需要设置范围)
if (year < 2000 || year > 2100)
{
printf("年份无效!请输入2000-2100之间的数字。\n");
return 0;
}
return 1; // 日期有效
}
// 查找日期索引,如果不存在则创建
int find_or_create_date(int year, int month, int day)
{
// 首先验证日期
if (!validate_date(year, month, day))
{
printf("日期无效,无法创建新日期记录。\n");
return -1;
}
for (int i = 0; i < day_count; i++)
{
if (info[i].year == year && info[i].month == month &&
info[i].day == day)
{
return i;
}
}
// 如果没找到,创建新的日期记录
if (day_count < MAX_DAYS)
{
int idx = day_count;
info[idx].year = year;
info[idx].month = month;
info[idx].day = day;
// 初始化所有机位状态为空闲
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
info[idx].state[seat][slot] = 0; // 空闲
strcpy(info[idx].waitlist[seat][slot].name, "");
strcpy(info[idx].waitlist[seat][slot].tel, "");
info[idx].waitlist[seat][slot].sex = 0;
}
}
day_count++;
return idx;
}
return -1; // 达到最大天数限制
}
// 1. 查询功能
void search(device info[])
{
int choice;
printf("\n=== 查询功能 ===\n");
printf("1. 按日期查询所有机位状态\n");
printf("2. 按机位查询所有日期状态\n");
printf("3. 按客户信息查询预约记录\n");
printf("请选择查询方式: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
// 按日期查询
int year, month, day;
int valid_date = 0;
// 循环直到输入有效日期
while (!valid_date)
{
printf("请输入查询日期 (格式: year/month/day): ");
scanf("%d/%d/%d", &year, &month, &day);
if (validate_date(year, month, day))
{
valid_date = 1;
}
else
{
printf("请重新输入日期。\n");
}
}
int date_idx = -1;
for (int i = 0; i < day_count; i++)
{
if (info[i].year == year && info[i].month == month &&
info[i].day == day)
{
date_idx = i;
break;
}
}
if (date_idx == -1)
{
printf("该日期暂无数据。\n");
return;
}
printf("\n日期: %d年%02d月%02d日\n", year, month, day);
printf("机位状态总览:\n");
printf("机位\\时段");
for (int slot = 0; slot < Time; slot++)
{
printf("\t%s", get_time_slot_str(slot));
}
printf("\n");
// 显示每个机位的状态
for (int seat = 0; seat < States; seat++)
{
printf("机位%02d", seat + 1);
for (int slot = 0; slot < Time; slot++)
{
printf("\t%s",
info[date_idx].state[seat][slot] ? "占用" : "空闲");
}
printf("\n");
}
// 显示详细预约信息
printf("\n详细预约信息:\n");
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[date_idx].state[seat][slot] == 1)
{
printf("机位%02d %s: 顾客: %s, 性别: %s, 电话: %s\n",
seat + 1, get_time_slot_str(slot),
info[date_idx].waitlist[seat][slot].name,
info[date_idx].waitlist[seat][slot].sex == 1 ? "男"
: "女",
info[date_idx].waitlist[seat][slot].tel);
}
}
}
break;
}
case 2:
{
// 按机位查询
int seat_num;
printf("请输入要查询的机位号 (1-50): ");
scanf("%d", &seat_num);
if (seat_num < 1 || seat_num > States)
{
printf("机位号无效!\n");
return;
}
int seat_idx = seat_num - 1;
printf("\n机位 %d 的预约情况:\n", seat_num);
int found = 0;
for (int i = 0; i < day_count; i++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[i].state[seat_idx][slot] == 1)
{
found = 1;
printf("日期: %d/%02d/%02d, 时间段: %s, 顾客: %s, 性别: "
"%s, 电话: %s\n",
info[i].year, info[i].month, info[i].day,
get_time_slot_str(slot),
info[i].waitlist[seat_idx][slot].name,
info[i].waitlist[seat_idx][slot].sex == 1 ? "男"
: "女",
info[i].waitlist[seat_idx][slot].tel);
}
}
}
if (!found)
{
printf("该机位暂无预约记录。\n");
}
break;
}
case 3:
{
// 按客户信息查询
char name[20];
char tel[12];
printf("请输入客户姓名: ");
scanf("%s", name);
printf("请输入客户电话: ");
scanf("%s", tel);
printf("\n%s (%s) 的预约记录:\n", name, tel);
int found = 0;
for (int i = 0; i < day_count; i++)
{
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
if (info[i].state[seat][slot] == 1 &&
strcmp(info[i].waitlist[seat][slot].name, name) == 0 &&
strcmp(info[i].waitlist[seat][slot].tel, tel) == 0)
{
found = 1;
printf("日期: %d/%02d/%02d, 机位: %02d, 时间段: %s\n",
info[i].year, info[i].month, info[i].day,
seat + 1, get_time_slot_str(slot));
}
}
}
}
if (!found)
{
printf("未找到该客户的预约记录。\n");
}
break;
}
default:
printf("无效的选择!\n");
}
}
// 2. 机位预订
void buy(device info[])
{
int year, month, day, seat_num, start_hour;
int valid_date = 0;
printf("\n=== 机位预订 ===\n");
// 循环直到输入有效日期
while (!valid_date)
{
printf("请输入预订日期 (格式: year/month/day): ");
scanf("%d/%d/%d", &year, &month, &day);
if (validate_date(year, month, day))
{
valid_date = 1;
}
else
{
printf("请重新输入日期。\n");
}
}
printf("请输入机位号 (1-50): ");
scanf("%d", &seat_num);
if (seat_num < 1 || seat_num > States)
{
printf("机位号无效!\n");
return;
}
printf("请选择时间段 (输入起始小时: 8,10,12,14,16,18): ");
scanf("%d", &start_hour);
if (start_hour < 8 || start_hour > 18 || (start_hour % 2 != 0))
{
printf("时间段无效!请选择8,10,12,14,16,18中的一个。\n");
return;
}
int slot = get_time_slot_index(start_hour);
int seat_idx = seat_num - 1;
// 查找或创建日期
int date_idx = find_or_create_date(year, month, day);
if (date_idx == -1)
{
printf("已达到最大存储天数限制!\n");
return;
}
// 检查机位是否已被占用
if (info[date_idx].state[seat_idx][slot] == 1)
{
printf("该机位在%s时段已被占用!\n", get_time_slot_str(slot));
// 显示同时间段其他空闲机位
printf("同时间段空闲机位: ");
int found = 0;
for (int s = 0; s < States; s++)
{
if (info[date_idx].state[s][slot] == 0)
{
printf("%d ", s + 1);
found = 1;
}
}
if (found)
printf("\n");
else
printf("无\n");
return;
}
// 获取客户信息
printf("请输入客户信息:\n");
printf("姓名: ");
scanf("%s", info[date_idx].waitlist[seat_idx][slot].name);
printf("性别 (0:女, 1:男): ");
scanf("%d", &info[date_idx].waitlist[seat_idx][slot].sex);
printf("电话 (11位): ");
scanf("%s", info[date_idx].waitlist[seat_idx][slot].tel);
// 更新状态
info[date_idx].state[seat_idx][slot] = 1;
printf("\n预订成功!\n");
printf("预约信息: 日期 %d/%02d/%02d, 机位 %02d, 时间段 %s, 客户 %s\n", year,
month, day, seat_num, get_time_slot_str(slot),
info[date_idx].waitlist[seat_idx][slot].name);
}
// 3. 取消预订
void exit_buy(device info[])
{
int year, month, day, seat_num, start_hour;
char name[20], tel[12];
int valid_date = 0;
printf("\n=== 取消预订 ===\n");
printf("请输入预订信息:\n");
// 循环直到输入有效日期
while (!valid_date)
{
printf("日期 (格式: year/month/day): ");
scanf("%d/%d/%d", &year, &month, &day);
if (validate_date(year, month, day))
{
valid_date = 1;
}
else
{
printf("请重新输入日期。\n");
}
}
printf("机位号 (1-50): ");
scanf("%d", &seat_num);
if (seat_num < 1 || seat_num > States)
{
printf("机位号无效!\n");
return;
}
printf("时间段起始小时 (8,10,12,14,16,18): ");
scanf("%d", &start_hour);
printf("客户姓名: ");
scanf("%s", name);
printf("客户电话: ");
scanf("%s", tel);
int slot = get_time_slot_index(start_hour);
int seat_idx = seat_num - 1;
// 查找日期
int date_idx = -1;
for (int i = 0; i < day_count; i++)
{
if (info[i].year == year && info[i].month == month &&
info[i].day == day)
{
date_idx = i;
break;
}
}
if (date_idx == -1)
{
printf("未找到该日期的记录!\n");
return;
}
// 验证预订信息
if (info[date_idx].state[seat_idx][slot] == 1 &&
strcmp(info[date_idx].waitlist[seat_idx][slot].name, name) == 0 &&
strcmp(info[date_idx].waitlist[seat_idx][slot].tel, tel) == 0)
{
// 取消预订
info[date_idx].state[seat_idx][slot] = 0;
strcpy(info[date_idx].waitlist[seat_idx][slot].name, "");
strcpy(info[date_idx].waitlist[seat_idx][slot].tel, "");
info[date_idx].waitlist[seat_idx][slot].sex = 0;
printf("取消预订成功!\n");
}
else
{
printf("未找到匹配的预订信息!请检查输入是否正确。\n");
}
}
// 读取数据
void read_data(device info[])
{
FILE *fp = fopen("computer_room_data.txt", "r");
if (!fp)
{
printf("未找到数据文件,将创建新文件。\n");
return;
}
// 读取天数
fscanf(fp, "%d", &day_count);
for (int i = 0; i < day_count && i < MAX_DAYS; i++)
{
fscanf(fp, "%d %d %d", &info[i].year, &info[i].month, &info[i].day);
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
fscanf(fp, "%d", &info[i].state[seat][slot]);
if (info[i].state[seat][slot] == 1)
{
fscanf(fp, " %s %d %s", info[i].waitlist[seat][slot].name,
&info[i].waitlist[seat][slot].sex,
info[i].waitlist[seat][slot].tel);
}
else
{
// 跳过空信息
char dummy_name[20], dummy_tel[12];
int dummy_sex;
fscanf(fp, " %s %d %s", dummy_name, &dummy_sex, dummy_tel);
strcpy(info[i].waitlist[seat][slot].name, "");
strcpy(info[i].waitlist[seat][slot].tel, "");
info[i].waitlist[seat][slot].sex = 0;
}
}
}
}
fclose(fp);
printf("成功读取%d天的数据。\n", day_count);
}
// 保存数据
void save_data(device info[])
{
FILE *fp = fopen("computer_room_data.txt", "w");
if (!fp)
{
printf("无法创建数据文件!\n");
return;
}
// 写入天数
fprintf(fp, "%d\n", day_count);
for (int i = 0; i < day_count; i++)
{
fprintf(fp, "%d %d %d\n", info[i].year, info[i].month, info[i].day);
for (int seat = 0; seat < States; seat++)
{
for (int slot = 0; slot < Time; slot++)
{
fprintf(fp, "%d", info[i].state[seat][slot]);
if (info[i].state[seat][slot] == 1)
{
fprintf(fp, " %s %d %s", info[i].waitlist[seat][slot].name,
info[i].waitlist[seat][slot].sex,
info[i].waitlist[seat][slot].tel);
}
else
{
fprintf(fp, " None 0 None");
}
if (slot < Time - 1)
fprintf(fp, " ");
}
fprintf(fp, "\n");
}
}
fclose(fp);
printf("数据已保存到 computer_room_data.txt,共%d天。\n", day_count);
}
int main()
{
SetConsoleOutputCP(65001); // 支持中文输出
// 初始化数组
memset(info, 0, sizeof(info));
// 读取已有数据
read_data(info);
int choice = 0;
while (choice != 4)
{
printf("\n=== 机房机位预约系统 ===\n");
printf("1. 查询机位状态\n");
printf("2. 预订机位\n");
printf("3. 取消预订\n");
printf("4. 保存并退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
search(info);
break;
case 2:
buy(info);
break;
case 3:
exit_buy(info);
break;
case 4:
save_data(info);
printf("感谢使用,再见!\n");
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
return 0;
}