求助C语言一道..555~~
N名学生的不重复成绩已在main()函数中放入一个带头结点的链表结构中,h指向链表的头结点。请编写函数fun(),它的功能是:找出学生的最高分,由函数值返回。 #include "stdio.h" #include "stdio.h" #define N 8 struct student {float score; struct student *next; }; typedef struct student STRUS; float fun(STRUS *h) { } STRUS *creat(float *s) {STRUS *h,*p,*q; int i=0; h=p=(STRUS *)malloc(sizeof(STRUS)); p->score=0; while (i<N) {q=(STRUS *)malloc(sizeof(STRUS)); q->score=s[i++]; p->next=q; p=q; } p->next=NULL; return h; } outlist(STRUS *h) {STRUS *p; p=(*h).next; printf("head"); do {printf("->%5.1f",(*p).score); p=(*p).next; } while (p!=NULL); printf("\n\n"); } main() {float s[N]={67,100,86,99.5,91.8,34,76,84},max; STRUS *h; h=creat(s); outlist(h); max=fun(h); printf("max=%5.1f\n",max); }
#include #define m 8 typedef struct ide { int score;//不知道你上面怎么是float型的; struct ide *next; }lnode; lnode *creat(lnode *h,int a[m]) { lnode *L,*R; L=new lnode; L->next=NULL; h=L; for(i=0;iscore=a[i]; //本人看上面的程序好像只与分数有关,与 //人无关,否则要再定义一个结构体数组 L=new lnode; R->next=L; //令前结点指向后结点 } delete L; //明显最后多给了一个结点 R->next=NULL; return h; } int fun(lnode *h) { lnode *p; p=h; int max; max=p->score; while(p->next!=NULL) { p=p->next; if(maxscore) max=p->score; } return max; } void main() { int a[m]={67,100,86,99。
5,91。
8,34,76,84},max; lnode *h; h=creat(h,int a[m]); max=fun(h); cout<
这样也可以: float fun(STRUS *h) { float m=0; STRUS *p=(*h).next; while(p!=NULL) { if(mscore) m=p->score; p=p->next; } return m; }
float fun(STRUS *h){ STRUS *p=h; float max=p->score; while((p=p->next)->next) if((p->score)>max) max=p->score; return max; } 定义一个中间变量记录最大值就可以了. (p=p->next)->next 这个表达式是判断是否最后一个结点。这是最简洁的方法了
答:#include "stdio.h" #include "conio.h" #include "string.h" #define N 100 void mai...详情>>
问:挺简单的一C函数,不过我是初学者,暂时还不会写,请好心人多多帮忙!
答:int mystrlen(char *str) { int n = 0; while (*str) { n++; str++;} return n; } voi...详情>>