编程环境Visual Studio 2017
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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <math.h> // practice 1 typedef struct MONTH_INFO { char month[4]; int month_days; int month_number; } ST_MONTH_INFO; #define N_MONTH 12 const ST_MONTH_INFO info[N_MONTH] = { {"Jan", 31, 1}, {"Feb", 28, 2}, {"Mar", 31, 3}, {"Apr", 30, 4}, {"May", 31, 5}, {"Jun", 30, 6}, {"Jul", 31, 7}, {"Aug", 31, 8}, {"Sep", 30, 9}, {"Oct", 31, 10}, {"Nov", 30, 11}, {"Dec", 31, 12} }; void p14_1(void) { char month[4]; int total_days = 0; _Bool find = 0; printf("Enter the abbreviation of month:"); fgets(month, 4, stdin); for (int i = 0; i < N_MONTH; i++) { total_days += info[i].month_days; if (strcmp(month, info[i].month) == 0) { find = 1; break; } } if (find == 1) printf("There are %d days till %s\n", total_days, month); else printf("The month of addreviation is not found!\n"); return; } // practice 2 void p14_2(void) { char month[4]; int days = 0; int total_days = 0; _Bool find = 0; int i = 0; while (find == 0) { total_days = 0; printf("Enter the abbreviation of month:"); fgets(month, 4, stdin); for (i = 0; i < N_MONTH; i++) { total_days += info[i].month_days; if (strcmp(month, info[i].month) == 0) { find = 1; break; } } if (find == 1) printf("There are %d days till %s\n", total_days, month); else printf("The month of addreviation is not found! Enter again.\n"); } find = 0; while (find == 0) { printf("Enter the days of month:"); scanf("%d", &days); if (days < info[i].month_days) { total_days += days; find = 1; printf("There %d days till %s %d\n", total_days, month, days); } else printf("The day of month is not found! Enter again.\n"); } return; } // practice 3 char *s_gets(char *st, int n) { char *ret_val; char *find; ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, '\n'); if (find) { *find = '\0'; } else { while (getchar() != '\n') { continue; } } } return ret_val; } #define MAXTITL 40 #define MAXAUTL 40 #define MAXBKS 100 struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; // 下面一个变量用于practice 7,在practice 3时可以先删除 int delete; }; void p14_3(void) { struct book library[MAXBKS]; int count = 0; int index; printf("Please enter the book title.\n"); printf("Please [enter] at the start of a line to stop.\n"); while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0') { printf("Now enter the author.\n"); s_gets(library[count].author, MAXAUTL); printf("Now enter the value.\n"); scanf("%f", &library[count++].value); while (getchar() != '\n') { continue; } if (count < MAXBKS) { printf("Enter the next title.\n"); } } if (count > 0) { printf("Here is the list of your books:\n"); printf("Array by original:\n"); for (index = 0; index < count; index++) { printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value); } printf("\n"); printf("Array by title:\n"); for (int i = 0; i < count; i++) { for (int j = i; j < count; j++) { if (strcmp(library[i].title, library[j].title) > 0) { struct book tmp = library[i]; library[i] = library[j]; library[j] = tmp; } } } for (index = 0; index < count; index++) { printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value); } printf("\n"); printf("Array by value:\n"); for (int i = 0; i < count; i++) { for (int j = i; j < count; j++) { if (library[i].value > library[j].value) { struct book tmp = library[i]; library[i] = library[j]; library[j] = tmp; } } } for (index = 0; index < count; index++) { printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value); } printf("\n"); } else printf("No books? Too bad.\n"); return; } // practice 4 struct name_info { char first_name[32]; char last_name[32]; char mid_name[32]; }; struct member_info { char insuran_number[32]; struct name_info name; }; void p14_4a(struct member_info *member) { for (int i = 0; i < 5; i++) { printf("%s, %s", member[i].name.first_name, member[i].name.last_name); if (member[i].name.mid_name[0] != '\0') { printf(" %s.", member[i].name.mid_name); } printf(" -- %s\n", member[i].insuran_number); } } void p14_4b(struct member_info member) { printf("%s, %s", member.name.first_name, member.name.last_name); if (member.name.mid_name[0] != '\0') { printf(" %s.", member.name.mid_name); } printf(" -- %s\n", member.insuran_number); } void p14_4(void) { struct member_info member[5] = { {"302039823", {"Dribble", "FLossie", "M"}}, {"302039824", {"Hello", "Kitty"}}, {"302039285", {"Hello", "Jimmy", "J"}}, {"302039286", {"Good", "Job"}}, {"302039287", {"Jump", "High", "J"}} }; // a //p14_4a(member); // b for (int i = 0; i < 5; i++) { p14_4b(member[i]); } return; } // practice 5 struct name { char first_name[32]; char last_name[32]; }; struct student { struct name student_name; double grade[3]; double average; }; #define CSIZE 4 void p14_5(void) { struct student students[CSIZE]; // d for (int i = 0; i < CSIZE; i++) { printf("Enter the student's first name: "); s_gets(students[i].student_name.first_name, 32); printf("Enter the student's last name: "); s_gets(students[i].student_name.last_name, 32); printf("Enter three grades of this student: "); for (int j = 0; j < 3; j++) { scanf("%lf", &students[i].grade[j]); } getchar(); // 换行符还在缓冲区里 } // e for (int i = 0; i < CSIZE; i++) { double total_grade = 0.0; for (int j = 0; j < 3; j++) { total_grade += students[i].grade[j]; } students[i].average = total_grade / 3; } // f for (int i = 0; i < CSIZE; i++) { printf("Student: %s, %s, grade: %.2lf, %.2lf, %.2lf, average: %.2lf\n", students[i].student_name.first_name, students[i].student_name.last_name, students[i].grade[0], students[i].grade[1], students[i].grade[2], students[i].average); } // g double total_grades = 0.0; for (int i = 0; i < CSIZE; i++) { total_grades += students[i].average; } printf("All students' average grade is %.2lf\n", total_grades / CSIZE); return; } // practice 6 struct softball_player { char first_name[128]; char last_name[128]; int play_times; int hit_times; int run_bar_times; int rbi; double hit_rate; }; void p14_6(void) { struct softball_player players[19] = { {'\0', '\0', 0, 0, 0, 0, 0.0} }; FILE *fp; char file_name[128]; struct softball_player player; int player_number; printf("Enter the file name: "); s_gets(file_name, 128); if ((fp = fopen(file_name, "r")) == NULL) { printf("Can't open the %s.\n", file_name); } while (feof(fp) == 0) { fscanf(fp, "%d", &player_number); fscanf(fp, "%s", &player.first_name); fscanf(fp, "%s", &player.last_name); fscanf(fp, "%d", &player.play_times); fscanf(fp, "%d", &player.hit_times); fscanf(fp, "%d", &player.run_bar_times); fscanf(fp, "%d", &player.rbi); if (players[player_number].first_name[0] == '\0') { players[player_number] = player; } else { players[player_number].play_times += player.play_times; players[player_number].hit_times += player.hit_times; players[player_number].run_bar_times += player.run_bar_times; players[player_number].rbi += player.rbi; } } for (int i = 0; i < 19; i++) { if (players[i].first_name[0] != '\0') { players[i].hit_rate = (double)players[i].hit_times / players[i].play_times; } } for (int i = 0; i < 19; i++) { if (players[i].first_name[0] != '\0') { printf("Player:%s %s, play times: %d, hit times: %d, run barrier times: %d, rbi: %d, hit_rate: %.2lf\n", players[i].first_name, players[i].last_name, players[i].play_times, players[i].hit_times, players[i].run_bar_times, players[i].rbi, players[i].hit_rate); } } } // practice 7 // 结构的定义和宏定义都在practice 3中,这里就不再另外定义 void p14_7(void) { struct book library[MAXBKS] = { {'\0', '\0', 0.0, 0} }; int count = 0; int index, filecount; FILE *pbooks; int size = sizeof(struct book); if ((pbooks = fopen("book.dat", "r+b")) == NULL) { fputs("Can't open book.dat file\n", stderr); } rewind(pbooks); while (count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1) { if (count == 0) { puts("Current contents of boot.dat:"); } printf("%s by %s: $%.2f\n", library[count].title, library[count].author, library[count].value); count++; } filecount = count; if (count == MAXBKS) { fputs("The book.dat file is null.", stderr); } printf("Please enter the book.dat title.\n"); printf("Please [enter] at the start of a line to stop.\n"); while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0') { printf("Now enter the author.\n"); s_gets(library[count].author, MAXAUTL); printf("Now enter the value.\n"); scanf("%f", &library[count++].value); while (getchar() != '\n') { continue; } if (count < MAXBKS) { printf("Enter the next title.\n"); } } if (count > 0) { printf("Here is the list of your books:\n"); for (index = 0; index < count; index++) { printf("%d: %s by %s: $%.2f\n", index + 1, library[index].title, library[index].author, library[index].value); } } else puts("No books? Too bad.\n"); while (1) { int item; char ch; printf("Which book do you want to delete or modify(-1 to exit)?"); scanf("%d", &item); while (getchar() != '\n') { continue; } if (item == -1) { break; } printf("[D]elete or [M]dify?"); scanf("%c", &ch); while (getchar() != '\n') { continue; } if (ch == 'D') { library[item - 1].delete = 1; } else { printf("Please enter the book title.\n"); s_gets(library[item - 1].title, MAXTITL); printf("Now enter the author.\n"); s_gets(library[item - 1].author, MAXAUTL); printf("Now enter the value.\n"); scanf("%f", &library[item - 1].value); while (getchar() != '\n') { continue; } } } // 关闭再以只读打开文件后可以完全清空文件 fclose(pbooks); if ((pbooks = fopen("book.dat", "w")) == NULL) { printf("Can't open boot.dat file\n"); } for (int i = 0; i < count; i++) { if (library[i].delete != 1) { fwrite(&library[i], size, 1, pbooks); } } puts("Done.\n"); fclose(pbooks); return; } // practice 8 #define TOTAL_SEAT 12 struct seat { int seat_number; int book; char first_name[128]; char last_name[128]; }; void show_empty_seat(struct seat *airplane) { int seats = 0; for (int i = 0; i < TOTAL_SEAT; i++) { if (airplane[i].book == 0) { seats++; } } printf("There are %d empty seats.\n", seats); return; } void list_empty_seat(struct seat *airplane) { printf("List empty seat:"); for (int i = 0; i < TOTAL_SEAT; i++) { if (airplane[i].book ==0) { printf(" %d", airplane[i].seat_number); } } printf("\n"); } void show_alphabetical(struct seat *airplane) { printf("Show alphabetical:\n"); for (int i = 0; i < TOTAL_SEAT; i++) { if (airplane[i].book == 1) { printf("%d booked by %s %s.\n", airplane[i].seat_number, airplane[i].first_name, airplane[i].last_name); } } } void book_seat(struct seat *airplane) { int seat_number = 0; while (1) { printf("Enter the seat number you want to book:"); scanf("%d", &seat_number); seat_number -= 1; while (getchar() != '\n'); if (airplane[seat_number].book == 1) { printf("Seat %d is not empty, please enter again.\n", seat_number+1); } else { break; } } printf("Enter the customer first name:"); s_gets(airplane[seat_number].first_name, 128); printf("Enter the customer last name:"); s_gets(airplane[seat_number].last_name, 128); airplane[seat_number].book = 1; return; } void delete_seat(struct seat *airplane) { int seat_number; printf("Enter the seat assigment you want to delete:"); scanf("%d", &seat_number); getchar(); airplane[seat_number-1].book = 0; } void p14_8(void) { struct seat airplane_seats[TOTAL_SEAT] = { {0, 0, '\0', '\0'} }; char choice; for (int i = 0; i < TOTAL_SEAT; i++) { airplane_seats[i].seat_number = i + 1; airplane_seats[i].book = 0; airplane_seats[i].first_name[0] = '\0'; airplane_seats[i].last_name[0] = '\0'; } while (1) { printf("To choose a function, enter its letter lable:\n"); printf("a) Show number of empty seats\n"); printf("b) Show list of empty seats\n"); printf("c) Show alphabetical list of seats\n"); printf("d) Assign a customer to a seat assigment\n"); printf("e) Delete a seat assignment\n"); printf("f) Quit\n"); scanf("%c", &choice); getchar(); if (choice == 'f') { break; } switch (choice) { case 'a': show_empty_seat(airplane_seats); break; case 'b': list_empty_seat(airplane_seats); break; case 'c': show_alphabetical(airplane_seats); break; case 'd': book_seat(airplane_seats); break; case 'e': delete_seat(airplane_seats); break; default: break; } } } // practice 9 // 确认座位分配选项不知道用来做什么的,没理解,所以就不做了 #define TOTAL_FLIGHT 4 void p14_9(void) { struct seat airplane_seats[TOTAL_FLIGHT][TOTAL_SEAT] = { { 0, 0, '\0', '\0' } }; struct seat *flight_select = NULL; char choice; for (int i = 0; i < TOTAL_FLIGHT; i++) { for (int j = 0; j < TOTAL_SEAT; j++) { airplane_seats[i][j].seat_number = j + 1; airplane_seats[i][j].book = 0; airplane_seats[i][j].first_name[0] = '\0'; airplane_seats[i][j].last_name[0] = '\0'; } } while (1) { char flight[4]; printf("Which flight you want to select: 102, 311, 444 or 519: "); scanf("%s", flight); getchar(); if (strcmp("102", flight) == 0) { flight_select = airplane_seats[0]; } else if (strcmp("311", flight) == 0) { flight_select = airplane_seats[1]; } else if (strcmp("444", flight) == 0) { flight_select = airplane_seats[2]; } else if (strcmp("519", flight) == 0) { flight_select = airplane_seats[3]; } while (1) { printf("To choose a function, enter its letter lable:\n"); printf("a) Show number of empty seats\n"); printf("b) Show list of empty seats\n"); printf("c) Show alphabetical list of seats\n"); printf("d) Assign a customer to a seat assigment\n"); printf("e) Delete a seat assignment\n"); printf("f) Quit\n"); scanf("%c", &choice); getchar(); if (choice == 'f') { break; } switch (choice) { case 'a': show_empty_seat(flight_select); break; case 'b': list_empty_seat(flight_select); break; case 'c': show_alphabetical(flight_select); break; case 'd': book_seat(flight_select); break; case 'e': delete_seat(flight_select); break; default: break; } } } } // practice 10 void p14_10a(void) { printf("p14_10a()\n"); } void p14_10b(void) { printf("p14_10b()\n"); } void p14_10c(void) { printf("p14_10c()\n"); } void p14_10(void) { char choice = 0; void(*fun[3])(void) = { p14_10a, p14_10b, p14_10c }; printf("Enter you choice:\n"); printf("a) p14_10a()\n"); printf("b) p14_10b()\n"); printf("c) p14_10c()\n"); scanf("%c", &choice); getchar(); switch (choice) { case 'a': fun[0](); break; case 'b': fun[1](); break; case 'c': fun[2](); break; default: break; } } // practice 11 double mul2(double x) { return (x * 2.0); } double mul1(double x) { return (x * x); } void transform(double *source, double *target, int num, double (*fun)(double)) { for (int i = 0; i < num; i++) { target[i] = fun(source[i]); } } void p14_11(void) { double source[100]; double target[100]; for (int i = 0; i < 100; i++) { source[i] = i; } printf("Use sqr:\n"); transform(source, target, 100, sqrt); for (int i = 0; i < 100; i++) { printf("%.2lf ", target[i]); if ((i + 1) % 10 == 0) { printf("\n"); } } printf("Use sin:\n"); transform(source, target, 100, sin); for (int i = 0; i < 100; i++) { printf("%.2lf ", target[i]); if ((i + 1) % 10 == 0) { printf("\n"); } } printf("Use mul2:\n"); transform(source, target, 100, mul2); for (int i = 0; i < 100; i++) { printf("%.2lf ", target[i]); if ((i + 1) % 10 == 0) { printf("\n"); } } printf("Use sqr:\n"); transform(source, target, 100, mul1); for (int i = 0; i < 100; i++) { printf("%.2lf ", target[i]); if ((i + 1) % 10 == 0) { printf("\n"); } } } int main(int argc, char **argv) { p14_11(); while (getchar()); return 0; } |
此文为博主原创文章,转载请注明出处
我也不知确认座位分配是干嘛用的 ,刚还想看博主的是怎么做的,原来也不知道