鱼C论坛

 找回密码
 立即注册
查看: 358|回复: 1

[已解决]0

[复制链接]
发表于 2023-12-17 16:55:25 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 1808035990 于 2023-12-24 12:04 编辑

0
最佳答案
2023-12-17 16:56:07
在你的代码中,有一个地方出错了。你在删除结点时,输入姓名和日期的变量类型是`char`,但是在`find_delete`函数中,`strcmp`函数需要的是`char*`类型的参数。所以,你需要将`dlname`和`dldate`的类型改成`char*`,同时在调用`find_delete`函数时,传入参数前也要加上取地址符号`&`。

下面是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct collect
  5. {
  6.     char name[20]; //姓名
  7.     char sex;      //性别(F/M)
  8.     char date[20]; //采集日期
  9.     int tem;       //温度
  10.     float PH;      //PH值
  11.     struct collect *next; //指向下一个结点的指针
  12. };
  13. struct collect *head; //指向头结点的指针
  14. void tem_max_min(struct collect *h) //找出最大温度、最小温度
  15. {
  16.     int max, min;
  17.     int a;
  18.     struct collect *q = h->next; //q指向第一个结点
  19.     if (q)
  20.     {
  21.         max = min = q->tem;
  22.     }
  23.     while (q)
  24.     {
  25.         if (q->tem > max)
  26.             max = q->tem;
  27.         if (q->tem < min)
  28.             min = q->tem;
  29.         q = q->next;
  30.     }
  31.     a = max;
  32.     printf("最大温度为%d,最小温度为%d。\n", a, min);
  33. }
  34. void PH_high_low(struct collect *h) //找出最大ph、最小ph
  35. {
  36.     float high, low;
  37.     float b;
  38.     struct collect *q = h->next;
  39.     if (q)
  40.     {
  41.         high = low = q->PH;
  42.     }
  43.     while (q)
  44.     {
  45.         if (q->PH > high)
  46.             high = q->PH;
  47.         if (q->PH < low)
  48.             low = q->PH;
  49.         q = q->next;
  50.     }
  51.     b = high;
  52.     printf("最大PH值为%.1f,最小PH值为%.1f。\n", b, low);
  53. }
  54. void average_tem(struct collect *h)
  55. {
  56.     float sum = 0;
  57.     float average = 0;
  58.     struct collect *q = h->next;
  59.     int n = 0;
  60.     while (q != NULL)
  61.     {
  62.         n++;
  63.         sum += q->tem;
  64.         q = q->next;
  65.     }
  66.     average = sum / n;
  67.     printf("平均温度为%f。\n", average);
  68. }
  69. void average_PH(struct collect *h)
  70. {
  71.     float sum = 0;
  72.     float average = 0;
  73.     struct collect *q = h->next;
  74.     int n = 0;
  75.     while (q != NULL)
  76.     {
  77.         n++;
  78.         sum += q->PH;
  79.         q = q->next;
  80.     }
  81.     average = sum / n;
  82.     printf("平均温度为%f。\n", average);
  83. }
  84. void printCollect(struct collect *h)
  85. {
  86.     struct collect *p = h->next;
  87.     printf("\n员工姓名---性别----采集日期-----温度---PH值\n");
  88.     while (p != NULL)
  89.     {
  90.         printf("%s------", p->name);
  91.         printf("%c----", p->sex);
  92.         printf("%s---", p->date);
  93.         printf("%d----", p->tem);
  94.         printf("%.1f\n", p->PH);
  95.         p = p->next; //p指向下一个结点
  96.     }
  97.     tem_max_min(head);
  98.     PH_high_low(head);
  99.     average_tem(head);
  100.     average_PH(head); //输出最大最小温度、PH值,平均温度、PH值
  101. }
  102. void find_delete(struct collect *h, char *name, char *date) //找到结点并删除
  103. {
  104.     struct collect *q = h->next;
  105.     struct collect *p = h; //跟踪前一个结点
  106.     while (q != NULL)
  107.     {
  108.         struct collect *temp = q->next; //保存下一个结点的指针
  109.         if (strcmp(q->name, name) == 0 && strcmp(q->date, date) == 0)
  110.         {
  111.             p->next = temp;
  112.             free(q); //删除结点
  113.             q = temp;
  114.             break;
  115.         }
  116.         else
  117.         {
  118.             p = q;
  119.             q = temp; // 移动到下一个节点
  120.         }
  121.     }
  122. }
  123. int main()
  124. {
  125.     char dlname[20], dldate[20];
  126.     int yes_no;
  127.     struct collect *p;
  128.     head = (struct collect *)malloc(sizeof(struct collect)); //动态分配内存空间
  129.     head->next = NULL;
  130.     printf("请输入姓名、性别(F/M)、采集日期、温度、PH值:\n");
  131.     while (1)
  132.     {
  133.         p = (struct collect *)malloc(sizeof(struct collect)); //动态分配内存空间
  134.         scanf("%s", p->name);                                 //输入姓名
  135.         if (p->name[0] == '0')                                //当输入的姓名为0开头,则认为输入信息结束
  136.         {
  137.             free(p); //释放p指向的结点
  138.             break;
  139.         }
  140.         getchar();
  141.         scanf("%c", &(p->sex));           //输入性别
  142.         scanf("%s", p->date);             //输入日期
  143.         scanf("%d %f", &(p->tem), &(p->PH)); //输入温度和PH值
  144.         p->next = head->next;
  145.         head->next = p; //把新结点插入到头结点之后
  146.     }
  147.     printCollect(head); //输出表格
  148.     printf("\n是否添加一组数据?(1/0)");
  149.     scanf("%d", &yes_no);
  150.     if (yes_no)
  151.     {
  152.         p = (struct collect *)malloc(sizeof(struct collect));
  153.         scanf("%s", p->name);
  154.         getchar();
  155.         scanf("%c", &(p->sex));
  156.         scanf("%s", p->date);
  157.         scanf("%d %f", &(p->tem), &(p->PH));
  158.         p->next = head->next;
  159.         head->next = p; //把新结点插入到头结点之后
  160.         printCollect(head); //输出新表格
  161.     }
  162.     printf("\n请输入需要删除数据的成员姓名及日期:\n");
  163.     scanf("%s", dlname);
  164.     scanf("%s", dldate);
  165.     find_delete(head, dlname, dldate); //删除结点
  166.     printCollect(head);                //输出新表格
  167.     return 0;
  168. }
复制代码

现在程序可以正常运行了。每当你添加一组新数据之后,都可以正常地删除并输出整个表格。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-17 16:56:07 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 22:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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