《C语言程序设计》实验指导书答案 下载本文

fclose(fp); fclose(fp1);

system(\}

6. //用产生随机数的方法产生:100个商场的序号(1到500之间)、每个商场一年四个季度每个季度的销售金额(单位万元),

//将这些数据存于数据文件,然后利用该文件求出每个商场一年的销售总金额。 #include #include #include struct market{ long no;

long quarter1; long quarter2; long quarter3; long quarter4; };

main() {

int i;

struct market m[101]; /*time_t t;

srand((unsigned) time(&t)); for (i=1; i<=100; i++) {

m[i].no=rand()P0;

m[i].quarter1=rand()0; m[i].quarter2=rand()0; m[i].quarter3=rand()0; m[i].quarter4=rand()0; }

FILE *fp;

if((fp=fopen(\ {

printf(\ exit(0); }

for(i=1;i<=100;i++)

fwrite(&m[i],sizeof(struct market),1,fp);//写入文件后看是乱码 fclose(fp);*/ FILE *fp1;

if((fp1=fopen(\

{

printf(\ exit(0); }

for (i=1; i<=100; i++) {

fread(&m[i],sizeof(struct market),1,fp1);

printf(\万)\\n\r2,m[i].quarter3,m[i].quarter4,(m[i].quarter1+m[i].quarter2+m[i].quarter3+m[i].quarter4)/4.0); }

system(\}