鱼C论坛

 找回密码
 立即注册
查看: 1331|回复: 3

[已解决]老师布置的一道题求助大佬

[复制链接]
发表于 2020-12-20 23:20:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
COVID-19疫情仍然不能放松,同学们晚自习进入某个实验楼仍然需要进行扫码,现在假设扫码的时候通过预设和二维码联网及传感器设备,会采集到进入楼宇的每位同学相关信息(姓名、性别、手机号、进楼时间、体温)。要求编程实现如下内容:
(1):现在用键盘输入(模拟数据采集)上述同学的5个信息,然后保存到一个数据文件中(不少于10个学生记录);输入时随机输入,然后按进楼的时间顺序排好后存储到文件中。(要把文件打开拍照上传)。
(2):现假设某个同学A第二天核酸检测阳性,现在要在(1)文件中找出很有可能与该同学同时乘一部电梯的前后相差不超过3分钟的所有同学,并将他们和A的3个信息姓名、手机号和进楼时间输出到屏幕上。(这个同学们在设置时设置好,保证在前后都有人)。

老师布置的一道题求助大佬,新人结构体还行,文件是真的懵啊。
望大佬指点下。
最佳答案
2020-12-21 09:23:38

  1. //Shibo
  2. //2020/12/20
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #define MAX_STUDENTS 1000

  7. typedef struct student{
  8.     char name[15];
  9.     char gen[4];
  10.     char phone_number[11];
  11.     int time;
  12.     float temp;
  13.     struct student* next;
  14. }student;

  15. /** Function */
  16. void common_list();
  17. void init(student* ptr);
  18. struct student* add_student(student* head);
  19. void searching(student* ptr);
  20. struct student* add_one_student(struct student *list);
  21. void file_out(student* ptr, FILE* fp);
  22. void free_function(struct student* list);

  23. int main(void){
  24.     //File pointer.
  25.     FILE* fp = fopen("output.txt","w");

  26.     //Student pointer.
  27.     struct student* head = NULL;
  28.     int common = 0;

  29.     while(common != 4){
  30.         //Print menu.
  31.         common_list();

  32.         //Read common.
  33.         scanf("%d",&common);

  34.         switch(common){
  35.             case 1:
  36.                 head = add_student(head);
  37.                 break;
  38.             case 2:
  39.                 searching(head);
  40.                 break;
  41.             case 3:
  42.                 file_out(head,fp);
  43.                 break;
  44.             case 4:
  45.                 free_function(head);
  46.                 return 0;
  47.             default:
  48.                 printf("错误输入\n");
  49.                 break;
  50.         }
  51.     }

  52.     return 0;
  53. }

  54. void common_list(){
  55.     printf("--------欢迎来到 COVID-19 监控系统-------\n");
  56.     printf("1. 添加学生\n");
  57.     printf("2. 查找学生\n");
  58.     printf("3. 输出文件\n");
  59.     printf("4. 退出系统\n\n");
  60. }


  61. struct student* add_student(student* head){

  62.     printf("你想添加多少个学生?\n");
  63.     int num_st;
  64.     scanf("%d",&num_st);

  65.     for(int i = 0; i<num_st; i++){
  66.         printf("学生%d: \n",i+1);
  67.         head = add_one_student(head);
  68.     }

  69.     return head;
  70. }

  71. struct student* add_one_student(struct student *list) {

  72.     struct student *pNew = NULL;

  73.     // Create the new node.
  74.     pNew = malloc(sizeof(student));

  75.     //Read in student information.
  76.     printf("\t请输入学生姓名:");
  77.     scanf("%s",pNew->name);

  78.     printf("\t请输入学生性别: ");
  79.     scanf("%s",pNew->gen);

  80.     printf("\t请输入学生手机号:");
  81.     scanf("%s",pNew->phone_number);

  82.     printf("\t请输入学生进楼时间:(XX:XX)");
  83.     scanf("%d",&pNew->time);


  84.     printf("\t请输入学生体温:");
  85.     scanf("%f",&pNew->temp);
  86.     pNew->next = NULL;
  87. //printf("\n\n");

  88.     // if list is empty then this becomes the first node.
  89.     if (list == NULL)
  90.         return pNew;

  91.     // Insert to front (special case)
  92.     if (pNew->time < list->time) {
  93.         pNew->next = list;
  94.         return pNew;
  95.     }

  96.     // Store front of the linked list
  97.     struct student *current = list;

  98.     // Iterate so that current points to the node BEFORE the one we want to do the insert.
  99.     while (current->next != NULL && current->next->time < pNew->time)
  100.         current = current->next;

  101.     // Links new node to its successor (which is right after current).
  102.     pNew->next = current->next;

  103.     // Links current to the new node.
  104.     current->next = pNew;

  105.     // Return a pointer to the edited list.
  106.     return list;
  107. }


  108. void searching(student* ptr){
  109.     if(ptr == NULL){
  110.         printf("空列表,请先输入学生信息\n\n\n");
  111.         return;
  112.     }

  113.     printf("请输入搜索姓名: ");
  114.     char name[20];
  115.     scanf("%s",name);

  116.     //Find the time.
  117.     student* current = ptr;
  118.     while(current!=NULL && strcmp(current->name,name)==1)
  119.         current = current->next;
  120.     if(current == NULL){
  121.         printf("查无此人\n");
  122.         return;
  123.     }
  124.     int time = current->time;

  125.     //Go over.
  126.     current = ptr;
  127.     //Go through all node print out all people we want.
  128.     while(current!=NULL){
  129.         if(abs(time-current->time)<=3)
  130.             printf("姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
  131.         // i++ step.
  132.         current = current->next;
  133.     }
  134. }

  135. void file_out(student* ptr, FILE* fp){
  136.     fprintf(fp,"--------欢迎来到 COVID-19 监控系统-------\n");
  137.     student* current = ptr;
  138.     //Go through all node print out all people we want.
  139.     while(current!=NULL){
  140.         //Output.
  141.         fprintf(fp,"姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
  142.         // i++ step.
  143.         current = current->next;
  144.     }
  145. }

  146. void free_function(struct student* list){
  147.     if (list != NULL) {
  148.         free_function(list->next);
  149.         free(list);
  150.     }
  151. }
复制代码


Test Case:
  1. 1
  2. 1
  3. 小甲鱼

  4. 188888888
  5. 1234
  6. 39.0
  7. 2
  8. 小甲鱼
  9. 1
  10. 3
  11. 黑夜

  12. 166666666
  13. 2359
  14. 35.4
  15. 康小泡

  16. 132323232
  17. 1200
  18. 37.0
  19. 小姨

  20. 1199
  21. 36.9
  22. 3
  23. 4
复制代码


Cmd:
  1. --------欢迎来到 COVID-19 监控系统-------
  2. 1. 添加学生
  3. 2. 查找学生
  4. 3. 输出文件
  5. 4. 退出系统

  6. 1
  7. 你想添加多少个学生?
  8. 1
  9. 学生1:
  10.         请输入学生姓名:小甲鱼
  11.         请输入学生性别: 男
  12.         请输入学生手机号:188888888
  13.         请输入学生进楼时间:(XX:XX)1234
  14.         请输入学生体温:39.0
  15. --------欢迎来到 COVID-19 监控系统-------
  16. 1. 添加学生
  17. 2. 查找学生
  18. 3. 输出文件
  19. 4. 退出系统

  20. 2
  21. 请输入搜索姓名: 小甲鱼
  22. 姓名;小甲鱼     电话:188888888          进楼时间: 12:34
  23. --------欢迎来到 COVID-19 监控系统-------
  24. 1. 添加学生
  25. 2. 查找学生
  26. 3. 输出文件
  27. 4. 退出系统

  28. 1
  29. 你想添加多少个学生?
  30. 3
  31. 学生1:
  32.         请输入学生姓名:黑夜
  33.         请输入学生性别: 男
  34.         请输入学生手机号:166666666
  35.         请输入学生进楼时间:(XX:XX)2359
  36.         请输入学生体温:35.4
  37. 学生2:
  38.         请输入学生姓名:康小泡
  39.         请输入学生性别: 女
  40.         请输入学生手机号:132323232
  41.         请输入学生进楼时间:(XX:XX)1200
  42.         请输入学生体温:37.0
  43. 学生3:
  44.         请输入学生姓名:小姨
  45.         请输入学生性别: 女
  46.         请输入学生手机号:1199
  47.         请输入学生进楼时间:(XX:XX)36.9
  48.         请输入学生体温:--------欢迎来到 COVID-19 监控系统-------
  49. 1. 添加学生
  50. 2. 查找学生
  51. 3. 输出文件
  52. 4. 退出系统

  53. 3
  54. --------欢迎来到 COVID-19 监控系统-------
  55. 1. 添加学生
  56. 2. 查找学生
  57. 3. 输出文件
  58. 4. 退出系统

  59. 4

  60. Process returned 0 (0x0)   execution time : 2.690 s
  61. Press any key to continue.
复制代码


File Out:
  1. --------欢迎来到 COVID-19 监控系统-------
  2. 姓名;小姨           电话:1199                进楼时间: 00:36
  3. 姓名;康小泡         电话:132323232           进楼时间: 12:00
  4. 姓名;小甲鱼         电话:188888888           进楼时间: 12:34
  5. 姓名;黑夜           电话:166666666           进楼时间: 23:59
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-21 09:23:38 | 显示全部楼层    本楼为最佳答案   

  1. //Shibo
  2. //2020/12/20
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #define MAX_STUDENTS 1000

  7. typedef struct student{
  8.     char name[15];
  9.     char gen[4];
  10.     char phone_number[11];
  11.     int time;
  12.     float temp;
  13.     struct student* next;
  14. }student;

  15. /** Function */
  16. void common_list();
  17. void init(student* ptr);
  18. struct student* add_student(student* head);
  19. void searching(student* ptr);
  20. struct student* add_one_student(struct student *list);
  21. void file_out(student* ptr, FILE* fp);
  22. void free_function(struct student* list);

  23. int main(void){
  24.     //File pointer.
  25.     FILE* fp = fopen("output.txt","w");

  26.     //Student pointer.
  27.     struct student* head = NULL;
  28.     int common = 0;

  29.     while(common != 4){
  30.         //Print menu.
  31.         common_list();

  32.         //Read common.
  33.         scanf("%d",&common);

  34.         switch(common){
  35.             case 1:
  36.                 head = add_student(head);
  37.                 break;
  38.             case 2:
  39.                 searching(head);
  40.                 break;
  41.             case 3:
  42.                 file_out(head,fp);
  43.                 break;
  44.             case 4:
  45.                 free_function(head);
  46.                 return 0;
  47.             default:
  48.                 printf("错误输入\n");
  49.                 break;
  50.         }
  51.     }

  52.     return 0;
  53. }

  54. void common_list(){
  55.     printf("--------欢迎来到 COVID-19 监控系统-------\n");
  56.     printf("1. 添加学生\n");
  57.     printf("2. 查找学生\n");
  58.     printf("3. 输出文件\n");
  59.     printf("4. 退出系统\n\n");
  60. }


  61. struct student* add_student(student* head){

  62.     printf("你想添加多少个学生?\n");
  63.     int num_st;
  64.     scanf("%d",&num_st);

  65.     for(int i = 0; i<num_st; i++){
  66.         printf("学生%d: \n",i+1);
  67.         head = add_one_student(head);
  68.     }

  69.     return head;
  70. }

  71. struct student* add_one_student(struct student *list) {

  72.     struct student *pNew = NULL;

  73.     // Create the new node.
  74.     pNew = malloc(sizeof(student));

  75.     //Read in student information.
  76.     printf("\t请输入学生姓名:");
  77.     scanf("%s",pNew->name);

  78.     printf("\t请输入学生性别: ");
  79.     scanf("%s",pNew->gen);

  80.     printf("\t请输入学生手机号:");
  81.     scanf("%s",pNew->phone_number);

  82.     printf("\t请输入学生进楼时间:(XX:XX)");
  83.     scanf("%d",&pNew->time);


  84.     printf("\t请输入学生体温:");
  85.     scanf("%f",&pNew->temp);
  86.     pNew->next = NULL;
  87. //printf("\n\n");

  88.     // if list is empty then this becomes the first node.
  89.     if (list == NULL)
  90.         return pNew;

  91.     // Insert to front (special case)
  92.     if (pNew->time < list->time) {
  93.         pNew->next = list;
  94.         return pNew;
  95.     }

  96.     // Store front of the linked list
  97.     struct student *current = list;

  98.     // Iterate so that current points to the node BEFORE the one we want to do the insert.
  99.     while (current->next != NULL && current->next->time < pNew->time)
  100.         current = current->next;

  101.     // Links new node to its successor (which is right after current).
  102.     pNew->next = current->next;

  103.     // Links current to the new node.
  104.     current->next = pNew;

  105.     // Return a pointer to the edited list.
  106.     return list;
  107. }


  108. void searching(student* ptr){
  109.     if(ptr == NULL){
  110.         printf("空列表,请先输入学生信息\n\n\n");
  111.         return;
  112.     }

  113.     printf("请输入搜索姓名: ");
  114.     char name[20];
  115.     scanf("%s",name);

  116.     //Find the time.
  117.     student* current = ptr;
  118.     while(current!=NULL && strcmp(current->name,name)==1)
  119.         current = current->next;
  120.     if(current == NULL){
  121.         printf("查无此人\n");
  122.         return;
  123.     }
  124.     int time = current->time;

  125.     //Go over.
  126.     current = ptr;
  127.     //Go through all node print out all people we want.
  128.     while(current!=NULL){
  129.         if(abs(time-current->time)<=3)
  130.             printf("姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
  131.         // i++ step.
  132.         current = current->next;
  133.     }
  134. }

  135. void file_out(student* ptr, FILE* fp){
  136.     fprintf(fp,"--------欢迎来到 COVID-19 监控系统-------\n");
  137.     student* current = ptr;
  138.     //Go through all node print out all people we want.
  139.     while(current!=NULL){
  140.         //Output.
  141.         fprintf(fp,"姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
  142.         // i++ step.
  143.         current = current->next;
  144.     }
  145. }

  146. void free_function(struct student* list){
  147.     if (list != NULL) {
  148.         free_function(list->next);
  149.         free(list);
  150.     }
  151. }
复制代码


Test Case:
  1. 1
  2. 1
  3. 小甲鱼

  4. 188888888
  5. 1234
  6. 39.0
  7. 2
  8. 小甲鱼
  9. 1
  10. 3
  11. 黑夜

  12. 166666666
  13. 2359
  14. 35.4
  15. 康小泡

  16. 132323232
  17. 1200
  18. 37.0
  19. 小姨

  20. 1199
  21. 36.9
  22. 3
  23. 4
复制代码


Cmd:
  1. --------欢迎来到 COVID-19 监控系统-------
  2. 1. 添加学生
  3. 2. 查找学生
  4. 3. 输出文件
  5. 4. 退出系统

  6. 1
  7. 你想添加多少个学生?
  8. 1
  9. 学生1:
  10.         请输入学生姓名:小甲鱼
  11.         请输入学生性别: 男
  12.         请输入学生手机号:188888888
  13.         请输入学生进楼时间:(XX:XX)1234
  14.         请输入学生体温:39.0
  15. --------欢迎来到 COVID-19 监控系统-------
  16. 1. 添加学生
  17. 2. 查找学生
  18. 3. 输出文件
  19. 4. 退出系统

  20. 2
  21. 请输入搜索姓名: 小甲鱼
  22. 姓名;小甲鱼     电话:188888888          进楼时间: 12:34
  23. --------欢迎来到 COVID-19 监控系统-------
  24. 1. 添加学生
  25. 2. 查找学生
  26. 3. 输出文件
  27. 4. 退出系统

  28. 1
  29. 你想添加多少个学生?
  30. 3
  31. 学生1:
  32.         请输入学生姓名:黑夜
  33.         请输入学生性别: 男
  34.         请输入学生手机号:166666666
  35.         请输入学生进楼时间:(XX:XX)2359
  36.         请输入学生体温:35.4
  37. 学生2:
  38.         请输入学生姓名:康小泡
  39.         请输入学生性别: 女
  40.         请输入学生手机号:132323232
  41.         请输入学生进楼时间:(XX:XX)1200
  42.         请输入学生体温:37.0
  43. 学生3:
  44.         请输入学生姓名:小姨
  45.         请输入学生性别: 女
  46.         请输入学生手机号:1199
  47.         请输入学生进楼时间:(XX:XX)36.9
  48.         请输入学生体温:--------欢迎来到 COVID-19 监控系统-------
  49. 1. 添加学生
  50. 2. 查找学生
  51. 3. 输出文件
  52. 4. 退出系统

  53. 3
  54. --------欢迎来到 COVID-19 监控系统-------
  55. 1. 添加学生
  56. 2. 查找学生
  57. 3. 输出文件
  58. 4. 退出系统

  59. 4

  60. Process returned 0 (0x0)   execution time : 2.690 s
  61. Press any key to continue.
复制代码


File Out:
  1. --------欢迎来到 COVID-19 监控系统-------
  2. 姓名;小姨           电话:1199                进楼时间: 00:36
  3. 姓名;康小泡         电话:132323232           进楼时间: 12:00
  4. 姓名;小甲鱼         电话:188888888           进楼时间: 12:34
  5. 姓名;黑夜           电话:166666666           进楼时间: 23:59
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-21 16:41:34 | 显示全部楼层
大佬牛批,不过我用DEVC++运行时92行那里要改成pNew=(struct student*)malloc(sizeof(student));就是把malloc返回的void指针转换一下才能运行,看了好久才懂的...还是感谢大佬的指点了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 00:17:38 | 显示全部楼层
猫儿恋上鱼 发表于 2020-12-21 16:41
大佬牛批,不过我用DEVC++运行时92行那里要改成pNew=(struct student*)malloc(sizeof(student));就是把mall ...

编译器不一样的原因吧,我用的codeblock一般不报错。不要用visual stadio 写C能烦死
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 20:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表