C语言期末考试编程大题求教

发布网友

我来回答

1个回答

热心网友

#include <stdio.h>

#include <malloc.h>

#include <string.h>


typedef struct info

{

int no;

char name[20];

char s;

float score;

struct info *next;

}NODE; 


NODE *creat()

{

NODE *head = NULL;

NODE *p,*r =NULL;

char str[100] = {0};

NODE tmp = {0};

gets(str);

sscanf(str,"%d %s %c %f", &(tmp.no), tmp.name, &(tmp.s), &(tmp.score));

while(strlen(str)>5)

{

p = (NODE*)malloc(sizeof(NODE));

memcpy(p,&tmp,sizeof(NODE));

if (NULL==head)

{

head = p;

}else

{

r->next = p;

}

r = p;

gets(str);

sscanf(str,"%d %s %c %f", &(tmp.no), tmp.name, &(tmp.s), &(tmp.score));

}

r->next = NULL;

return head;

}


NODE *findmax(NODE *p)

{

NODE tmp = {0};

NODE *ptr = NULL;

while (p)

{

if (p->score > tmp.score)

{

memcpy(&tmp,p,sizeof(NODE));

ptr = p;

}

p = p->next;

}

return ptr;

}


void prn(NODE *p)

{

printf("no<%d> name<%s> sex<%c> score<%f>", p->no,p->name,p->s,p->score);

}


void freelst(NODE *pHead)

{

NODE *p;


while (pHead)

{

p = pHead;

pHead = pHead->next;

free(p);

p = NULL;

}

}


void main()

{

NODE *head,*p;

head=creat();     //创建list

p=findmax(head); //查找max

prn(p);  //输出信息

freelst(head);  //释放申请到的内存空间

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
1.846748s