|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 1808035990 于 2023-12-24 12:04 编辑
0
在你的代码中,有一个地方出错了。你在删除结点时,输入姓名和日期的变量类型是`char`,但是在`find_delete`函数中,`strcmp`函数需要的是`char*`类型的参数。所以,你需要将`dlname`和`dldate`的类型改成`char*`,同时在调用`find_delete`函数时,传入参数前也要加上取地址符号`&`。
下面是修改后的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct collect
- {
- char name[20]; //姓名
- char sex; //性别(F/M)
- char date[20]; //采集日期
- int tem; //温度
- float PH; //PH值
- struct collect *next; //指向下一个结点的指针
- };
- struct collect *head; //指向头结点的指针
- void tem_max_min(struct collect *h) //找出最大温度、最小温度
- {
- int max, min;
- int a;
- struct collect *q = h->next; //q指向第一个结点
- if (q)
- {
- max = min = q->tem;
- }
- while (q)
- {
- if (q->tem > max)
- max = q->tem;
- if (q->tem < min)
- min = q->tem;
- q = q->next;
- }
- a = max;
- printf("最大温度为%d,最小温度为%d。\n", a, min);
- }
- void PH_high_low(struct collect *h) //找出最大ph、最小ph
- {
- float high, low;
- float b;
- struct collect *q = h->next;
- if (q)
- {
- high = low = q->PH;
- }
- while (q)
- {
- if (q->PH > high)
- high = q->PH;
- if (q->PH < low)
- low = q->PH;
- q = q->next;
- }
- b = high;
- printf("最大PH值为%.1f,最小PH值为%.1f。\n", b, low);
- }
- void average_tem(struct collect *h)
- {
- float sum = 0;
- float average = 0;
- struct collect *q = h->next;
- int n = 0;
- while (q != NULL)
- {
- n++;
- sum += q->tem;
- q = q->next;
- }
- average = sum / n;
- printf("平均温度为%f。\n", average);
- }
- void average_PH(struct collect *h)
- {
- float sum = 0;
- float average = 0;
- struct collect *q = h->next;
- int n = 0;
- while (q != NULL)
- {
- n++;
- sum += q->PH;
- q = q->next;
- }
- average = sum / n;
- printf("平均温度为%f。\n", average);
- }
- void printCollect(struct collect *h)
- {
- struct collect *p = h->next;
- printf("\n员工姓名---性别----采集日期-----温度---PH值\n");
- while (p != NULL)
- {
- printf("%s------", p->name);
- printf("%c----", p->sex);
- printf("%s---", p->date);
- printf("%d----", p->tem);
- printf("%.1f\n", p->PH);
- p = p->next; //p指向下一个结点
- }
- tem_max_min(head);
- PH_high_low(head);
- average_tem(head);
- average_PH(head); //输出最大最小温度、PH值,平均温度、PH值
- }
- void find_delete(struct collect *h, char *name, char *date) //找到结点并删除
- {
- struct collect *q = h->next;
- struct collect *p = h; //跟踪前一个结点
- while (q != NULL)
- {
- struct collect *temp = q->next; //保存下一个结点的指针
- if (strcmp(q->name, name) == 0 && strcmp(q->date, date) == 0)
- {
- p->next = temp;
- free(q); //删除结点
- q = temp;
- break;
- }
- else
- {
- p = q;
- q = temp; // 移动到下一个节点
- }
- }
- }
- int main()
- {
- char dlname[20], dldate[20];
- int yes_no;
- struct collect *p;
- head = (struct collect *)malloc(sizeof(struct collect)); //动态分配内存空间
- head->next = NULL;
- printf("请输入姓名、性别(F/M)、采集日期、温度、PH值:\n");
- while (1)
- {
- p = (struct collect *)malloc(sizeof(struct collect)); //动态分配内存空间
- scanf("%s", p->name); //输入姓名
- if (p->name[0] == '0') //当输入的姓名为0开头,则认为输入信息结束
- {
- free(p); //释放p指向的结点
- break;
- }
- getchar();
- scanf("%c", &(p->sex)); //输入性别
- scanf("%s", p->date); //输入日期
- scanf("%d %f", &(p->tem), &(p->PH)); //输入温度和PH值
- p->next = head->next;
- head->next = p; //把新结点插入到头结点之后
- }
- printCollect(head); //输出表格
- printf("\n是否添加一组数据?(1/0)");
- scanf("%d", &yes_no);
- if (yes_no)
- {
- p = (struct collect *)malloc(sizeof(struct collect));
- scanf("%s", p->name);
- getchar();
- scanf("%c", &(p->sex));
- scanf("%s", p->date);
- scanf("%d %f", &(p->tem), &(p->PH));
- p->next = head->next;
- head->next = p; //把新结点插入到头结点之后
- printCollect(head); //输出新表格
- }
- printf("\n请输入需要删除数据的成员姓名及日期:\n");
- scanf("%s", dlname);
- scanf("%s", dldate);
- find_delete(head, dlname, dldate); //删除结点
- printCollect(head); //输出新表格
- return 0;
- }
复制代码
现在程序可以正常运行了。每当你添加一组新数据之后,都可以正常地删除并输出整个表格。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|