作者Isuzel (Isuzel)
看板NSYSU-CSE97
標題Re: [討論] HW7功課討論
時間Thu Dec 2 05:28:28 2004
※ 引述《executor1107 (我都在喇塞嘛><)》之銘言:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *finput,*foutput;
int c,temp,i,total;
float avg,sum=0;
/*randomize(); */
printf("Input the number of the numbers:");
scanf("%d",&total);
finput=fopen("input.txt","w+");
if(finput != NULL)
/* 這兩行其實可以合併成
if((finput = fopen("input.txt","w+"))) */
{
for(i=0;i<total;i++)
{
temp=rand();
printf(" %d \n",temp);
fprintf(finput,"%d\n",temp);
}
}
else
printf("Error 01:Couldn't open input.txt in writing phase.");
fclose(finput);
finput=fopen("input.txt","r+");
if(finput != NULL)
{
foutput=fopen("output.txt","w+");
if(foutput != NULL)
{
for(c=0;c<total;c++)
/*我記得助教是用feof吧...不過如果真的用了的話會產生意料外的結果
並且這裡沒必要再搞一個c出來。用之前的i不就好了嗎? */
{
fscanf(finput,"%d",&temp);
fprintf(foutput,"%d\n",temp);
sum = sum + temp;
/* sum+=tmp; */
}
}
else
printf("Error 03:Couldn't open output.txt in writing phase.");
}
else
printf("Error 02:Couldn't open input.txt in reading phase.");
avg=sum/(float)total;
printf("The average of the numbers is %0.2f\n",avg);
fprintf(foutput,"The %d numbers' average is %f\n",total,avg);
fclose(finput);
fclose(foutput);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.117.186.150