C语言:把分数最低的学生数据放入数组b所指的数组中,-从键盘输入若干字符串,写入文件myfile4中,用-1作字符输入结束的标志,

时间:2023-12-12 11:01:50

//学生记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组中,fun函数:把分数最低的学生数据放入数组b所指的数组中,分数最低的学生可能不止一个。人数返回。

 #include <stdio.h>
#define N 16
typedef struct
{ char num[];
int s;
} STREC;
int fun( STREC *a, STREC *b )
{
STREC *c;
c = a;
int min = c->s;
int s = ;
for (int i = ; i < N; i++)
{
c++;
if (c->s < min) min = c->s;
}
for (int j = ; j < N; j++)
{
if (a->s == min)
{
s++;
*b = *a;//注意赋值方式
b++;
}
a++;
}
return s;
} void main()
{ STREC s[N]={{"GA05",},{"GA03",},{"GA02",},{"GA04",},
{"GA01",},{"GA07",},{"GA08",},{"GA06",},
{"GA015",},{"GA013",},{"GA012",},{"GA014",},
{"GA011",},{"GA017",},{"GA018",},{"GA016",}};
STREC h[N];
int i,n;FILE *out ;
n=fun( s,h );
printf("The %d lowest score :\n",n);
for(i=;i<n; i++)
printf("%s %4d\n",h[i].num,h[i].s);
printf("\n");
out = fopen("out.dat","w") ;
fprintf(out, "%d\n",n);
for(i=;i<n; i++)
fprintf(out, "%4d\n",h[i].s);
fclose(out);
}

//从键盘输入若干字符串,写入文件myfile4中,用-1作字符输入结束的标志,然后将文件的内容显示在屏幕上,文件的读写分别由函数ReadText和WriteText实现。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
void WriteText(FILE *);
void ReadText(FILE *);
void main()
{ FILE *fp;
if((fp=fopen("myfile4.txt","w"))==NULL)
{ printf(" open fail!!\n"); exit();}
WriteText(fp);
fclose(fp);
if((fp=fopen("myfile4.txt","r"))==NULL)
{ printf(" open fail!!\n"); exit();}
ReadText(fp);
fclose(fp);
}
/**********found**********/
void WriteText(FILE *fw)
{ char str[];
printf("\nEnter string with -1 to end :\n");
gets(str);
while(strcmp(str,"-1")!=) {
/**********found**********/
fputs(str,fw); fputs("\n",fw);//fputs函数向指定文件中写入一个字符串。
gets(str);
}
}
void ReadText(FILE *fr)
{ char str[];
printf("\nRead file and output to screen :\n");
fgets(str,,fr);
while( !feof(fr) ) {
/**********found**********/
printf("%s",str);
fgets(str,,fr);
}
}