急急急·····这道编程题怎么做啊?···
- 提问者网友:疯子也有疯子的情调
- 2021-05-06 13:18
- 五星知识达人网友:洎扰庸人
- 2021-05-06 13:25
//编写程序,从考生文件夹下的数据文件data565.DAT中读取200个整数至数组中,
//求出这些数中能被3整除或能被5整除的数的算术平均值,
//并将结果按格式"%.2f\n"写入考生文件夹下的sj565.c文件中。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
static int i = 0;
static float count = 0;
static float sum = 0;
struct student
{
int math;
int english;
int history;
};
struct student stu[MAXSIZE];
void write()
{
int judge;
FILE *wri;
if( (wri = fopen( "stu.dat","w" )) == NULL)
{
printf( "创建文件失败!!!");
exit(0);
}
loop:
printf( "请输入数学成绩:" );
scanf( "%d",&stu[i].math );
while( stu[i].math < 0 || stu[i].math > 100 )
{
printf( "请输入数学成绩:" );
scanf( "%d", &stu[i].math );
}
printf( "请输入英语成绩:" );
scanf( "%d",&stu[i].english );
while( stu[i].english < 0 || stu[i].english > 100 )
{
printf( "请输入英语成绩:" );
scanf( "%d", &stu[i].english );
}
printf( "请输入历史成绩:" );
scanf( "%d", &stu[i].history );
while( stu[i].history < 0 || stu[i].history > 100 )
{
printf( "请输入历史成绩:" );
scanf( "%d", &stu[i].history );
}
fwrite( &stu[i], sizeof( struct student ), 1, wri );
printf( "是否还要输入是(1)否(0):" );
scanf( "%d", &judge );
if( judge == 1 )
{
i++;
goto loop;
}
fclose( wri );
}
float read()
{
FILE *rea;
if( ( rea = fopen( "stu.dat","r")) == NULL )
{
printf( "打开文件失败!!!" );
exit(0);
}
while( i >= 0 )
{
fread( &stu[i], sizeof( struct student ), 1, rea );
if( stu[i].math % 3 ==0 || stu[i].math % 5 ==0 )
{
sum+= stu[i].math;
count++;
}
if( stu[i].english % 3 ==0 || stu[i].english % 5 ==0 )
{
sum+= stu[i].math;
count++;
}
if( stu[i].history % 3 ==0 || stu[i].history % 5 ==0 )
{
sum+= stu[i].history;
count++;
}
i--;
}
fclose( rea );
printf( "一共有%0.0f个能被3或能被5整除的数据!!\n", count );
if( count != 0)
{
return sum / count;
}
else
{
printf( "被除数为0\n");
return -1;
}
}
int main( void )
{
float receive;
FILE *fp;
write();
receive = read();
if( (fp = fopen( "avg.dat", "w" )) == NULL)
{
printf( "创建文件失败 !!! ");
exit(0);
}
fputc( receive, fp );
fclose( fp );
printf( "平均数=%2.2f\n", receive );
return EXIT_SUCCESS;
}
不知道是不是。。。。