void MAX(Code *L) { Code *p=L->next; int i; int maxmon=0; char maxnum=0,maxphone=0,maxname=0,maxsex=0; while(p!=NULL) { for(i=0;i<10;i++) { if(p->mon>maxmon) maxnum=p->mon; maxnum=*p->num; maxname=*p->name; maxphone=*p->phone; maxsex=*p->sex; p=p->next; } } printf("积分最高客户信息为:客户号:%d\t姓名:%s\t电话:%d\t性别:%c\t积分:%d\n",maxnum,maxname,maxphone,maxsex,maxmon); }
时间: 2024-03-18 15:40:08 浏览: 20
根据您给出的代码,我看到这是一个函数定义,函数名为MAX,参数为指向Code结构体的指针L。函数的作用是找到链表中积分最高的客户,并输出该客户的信息。
函数体中的while循环遍历链表,for循环用于比较每个节点的积分大小,找到最高积分的节点,同时记录该节点的客户号、姓名、电话和性别。
最后使用printf函数输出积分最高的客户的信息。
但是,这段代码中存在一个问题,就是在for循环中的if语句中,只更新了maxnum和maxmon,而没有更新其他变量。因此,输出的结果可能会出现错误。
建议修改为以下代码:
```
void MAX(Code *L) {
Code *p=L->next;
int i;
int maxmon=0;
char maxnum=0,maxphone=0,maxname[20]={0},maxsex=0;
while(p!=NULL)
{
for(i=0;i<10;i++)
{
if(p->mon>maxmon)
{
maxmon=p->mon;
maxnum=*p->num;
strcpy(maxname, p->name);
maxphone=*p->phone;
maxsex=*p->sex;
}
p=p->next;
}
}
printf("积分最高客户信息为:客户号:%d\t姓名:%s\t电话:%d\t性别:%c\t积分:%d\n",maxnum,maxname,maxphone,maxsex,maxmon);
}
```
修改后的代码中,在if语句中更新了maxnum、maxmon、maxname、maxphone和maxsex,保证输出的结果正确。同时,由于姓名可能超过一个字符,因此将maxname定义为字符数组,并使用strcpy函数复制节点的姓名信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)