• C语言问题解决实例1


    1.问题1,截图如下:

    代码运行截图:

    解决对应代码如下:

    1. #include
    2. #include
    3. #define MAX_LINE_LENGTH 100
    4. struct Event {
    5. int age;
    6. int id;
    7. int year;
    8. int month;
    9. int day;
    10. char name[20];
    11. };
    12. int main() {
    13. FILE* file;
    14. char line[MAX_LINE_LENGTH];
    15. // Open the file
    16. file = fopen("A.txt", "r");
    17. if (file == NULL) {
    18. printf("Unable to open the file.\n");
    19. return 1;
    20. }
    21. printf("------------------------------------------\n");
    22. // Read and print each line until the end of the file
    23. while (fgets(line, sizeof(line), file)) {
    24. struct Event event;
    25. sscanf(line, "%d,%d,%d,%d,%d,%s[^\n]", &event.age, &event.id, &event.year, &event.month, &event.day, event.name);
    26. printf("%d岁以下,%s,%d,%d/%d/%d\n", event.age, event.name, event.id, event.year, event.month, event.day);
    27. printf("------------------------------------------\n");
    28. }
    29. // Close the file
    30. fclose(file);
    31. return 0;
    32. }

    2.问题2,截图如下:

    代码运行及截图如下:

    解决对应代码如下:

    1. #include
    2. #include
    3. #include
    4. #define MAX_LINE_LENGTH 100
    5. struct Event {
    6. int age;
    7. int id;
    8. int year;
    9. int month;
    10. int day;
    11. char name[20];
    12. };
    13. void findMatchingEvents(int age, int id) {
    14. FILE* file = fopen("A.txt", "r");
    15. if (file == NULL) {
    16. printf("无法打开文件\n");
    17. return;
    18. }
    19. char line[MAX_LINE_LENGTH];
    20. bool found = false;
    21. while (fgets(line, MAX_LINE_LENGTH, file)) {
    22. struct Event event;
    23. sscanf(line, "%d,%d,%d,%d,%d,%s[^\n]", &event.age, &event.id, &event.year, &event.month, &event.day, event.name);
    24. if (age <= event.age && event.id == id) {
    25. printf("能参加的项目:%s\n", event.name);
    26. found = true;
    27. break;
    28. }
    29. }
    30. fclose(file);
    31. if (!found) {
    32. printf("没有找到符合条件的项目\n");
    33. }
    34. }
    35. int main() {
    36. int age, id;
    37. printf("请输入年龄:");
    38. scanf("%d", &age);
    39. printf("请输入工号:");
    40. scanf("%d", &id);
    41. findMatchingEvents(age, id);
    42. return 0;
    43. }

    3.问题3,截图如下:

    代码运行对应截图如下:

    解决对应代码如下:

    1. #include
    2. #include
    3. #include
    4. #define MAX_LINE_LENGTH 100
    5. struct Event {
    6. int age;
    7. int id;
    8. int year;
    9. int month;
    10. int day;
    11. char name[20];
    12. };
    13. //查找1
    14. void findMatchingEvents(int age, int id) {
    15. FILE* file = fopen("A.txt", "r");
    16. if (file == NULL) {
    17. printf("无法打开文件\n");
    18. return;
    19. }
    20. char line[MAX_LINE_LENGTH];
    21. bool found = false;
    22. int iLine = 1;
    23. while (fgets(line, MAX_LINE_LENGTH, file)) {
    24. struct Event event;
    25. sscanf(line, "%d,%d,%d,%d,%d,%s[^\n]", &event.age, &event.id, &event.year, &event.month, &event.day, event.name);
    26. if (age <= event.age && event.id == id) {
    27. printf("能参加的项目:%s,编号:%d\n", event.name, iLine);
    28. found = true;
    29. break;
    30. }
    31. iLine++;
    32. }
    33. fclose(file);
    34. if (!found) {
    35. printf("没有找到符合条件的项目\n");
    36. }
    37. }
    38. //保存字符串到文件中
    39. void savecontent(char* str) {
    40. FILE* file = fopen("B.txt", "w");
    41. if (file == NULL) {
    42. printf("无法打开文件\n");
    43. return ;
    44. }
    45. fputs(str, file);
    46. fclose(file);
    47. }
    48. //查找2
    49. void findMatchingEvents2(int nameid) {
    50. FILE* file = fopen("A.txt", "r");
    51. if (file == NULL) {
    52. printf("无法打开文件\n");
    53. return;
    54. }
    55. char line[MAX_LINE_LENGTH], line2[MAX_LINE_LENGTH];
    56. bool found = false;
    57. int iLine = 1;
    58. while (fgets(line, MAX_LINE_LENGTH, file)) {
    59. if (nameid == iLine) {
    60. struct Event event;
    61. sscanf(line, "%d,%d,%d,%d,%d,%s[^\n]", &event.age, &event.id, &event.year, &event.month, &event.day, event.name);
    62. sprintf(line2,"教师工号:%d,项目编号:%d,项目比赛时间:%d/%d/%d\n", event.id, nameid, event.year, event.month, event.day);
    63. printf("%s", line2);//屏幕输出
    64. savecontent(line2);//将line2内容写入文本文件B.txt中
    65. found = true;
    66. break;
    67. }
    68. else
    69. {
    70. iLine++;
    71. }
    72. }
    73. fclose(file);
    74. if (!found) {
    75. printf("没有找到符合条件的项目\n");
    76. }
    77. }
    78. int main() {
    79. int age, id,nameid;
    80. printf("请输入年龄:");
    81. scanf("%d", &age);
    82. printf("请输入工号:");
    83. scanf("%d", &id);
    84. findMatchingEvents(age, id);
    85. printf("请输入项目编号:");
    86. scanf("%d", &nameid);
    87. findMatchingEvents2(nameid);
    88. return 0;
    89. }

    解析:

    这上面三个问题,主要是用到的功能有:文件的读写,结构体,循环,字符串键盘输入读取,屏幕输出,字符串查找判断,字符串赋值等操作。这是基本操作,主要是考查学生的基本功。如有不足之处,欢迎大家来讨论指正。

  • 相关阅读:
    全渠道电商 | 国内知名的药妆要如何抓住风口实现快速增长?
    游戏录屏软件推荐,教你录制高清游戏视频
    Vue+NodeJS上传图片到腾讯云Cos
    mariadb弱口令登录
    leecode#只出现一次数字#环形链表
    图像主题颜色提取(Median cut)
    【Make YOLO Great Again】YOLOv1-v7全系列大解析(输入侧篇)
    大二Web课程设计:HTML+CSS学校静态网页设计——南京师范大学泰州学院(11页)
    一级造价工程师(安装)- 计量笔记 - 第六章第二节自动控制系统
    为什么说C++太复杂(复杂到哪了?)
  • 原文地址:https://blog.csdn.net/airen3339/article/details/133863323