N名学生的成绩已经在主函数中放入一个带有头结点的链表结构中,h指向链表的头结点,编写函数fun,其功能是:找出学生的最高分,并由函数值返回

#include <stdio.h>
#include <stdlib.h>
#define   N   8
struct  slist
{  double   s;
   struct slist  *next;
};
typedef  struct slist  STREC;
double  fun( STREC *h  )
{

double max=h->s;
while(h!=NULL)
{
if(max<h->s)
max=h->s;
h=h->next;
}
return max;
}

STREC * creat( double *s)
{ STREC  *h,*p,*q;   int  i=0;
  h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
  while(i<N)
  { q=(STREC*)malloc(sizeof(STREC));
    q->s=s[i]; i++; p->next=q; p=q;
  }
  p->next=0;
  return  h;
}
outlist( STREC *h)
{ STREC  *p;
  p=h->next;   printf("head");
  do
  { printf("->%2.0f",p->s);p=p->next;}
  while(p!=0);
  printf("\n\n");
}
main()
{  double  s[N]={85,76,69,85,91,72,64,87}, max;void NONO ();
   STREC  *h;
   h=creat( s );   outlist(h);
   max=fun( h );
   printf("max=%6.1f\n",max);
   NONO();
}
void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  FILE *in, *out ;
  int i,j ; double  s[N],max;
  STREC *h ;
  in = fopen("in.dat","r") ;
  out = fopen("out.dat","w") ;
  for(i = 0 ; i < 10 ; i++) {
    for(j=0 ; j < N; j++) fscanf(in, "%lf,", &s[j]) ;
    h=creat( s );
    max=fun( h );
    fprintf(out, "%6.1lf\n", max) ;
  }
  fclose(in) ;
  fclose(out) ;
}

  

posted on 2017-09-14 21:42  jun俊  阅读(1417)  评论(0)    收藏  举报

导航