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 #define States 50
typedef struct cusinfo { char name[20]; int sex; char tel[12]; } cinfo;
typedef struct mechine { int year; int month; int day; int state[States][Time]; cinfo waitlist[States][Time]; } device; device info[MAX_DAYS]; int day_count = 0;
int get_time_slot_index(int hour) { return (hour - 8) / 2; }
char *get_time_slot_str(int slot) { static char time_str [20]; int start_hour = 8 + slot * 2; sprintf(time_str, "%02d:00-%02d:00", start_hour, start_hour + 2); return time_str; }
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; }
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"); } }
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); }
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; }
|