大学のコードを書く必要があります。ここでは、時間、太陽放射、Rain meter(mm)を入力し、ユーザーから与えられた情報に基づいて連続して各行に式を適用します。私は一度にユーザーが選択した1行の式を適用するコードを作成しました。この式をすべての行に適用して同時に表示するにはどうしたらいいですか?.txtファイルを開き、Cで連続して各行に方程式を適用する方法
私が使用している.txtファイルは次のとおりです。
0 -0.82 17.2
1 2386 14.4
2 1980 0.22
3 470 0.0
4 2731 0.2
5 1531 0.0
6 3084 0.4
7 2197 0.4
8 313 0.0
9 1428 0.0
10 47.01 0.0
11 138.8 0.0
12 328.4 0.0
13 431.2 0.4
14 419.6 0.2
15 447.8 0.8
16 572.2 0.2
17 801.5 0.0
18 849.1 0.0
19 561.1 0.0
20 376.6 0.0
21 122.9 0.0
22 -1.69 0.0
23 -1.46 0.0
私のコードは次の通りである(unpolishnessを許してください、私はプログラミングでbegginerよ):
#include <stdio.h>
#define efi 0.83 //83% , standart solar cell eficiency
int h,a;
int potency (int x, int y, int z)
{
return (x*y*z);
}
int hour[24], i=0;
float radiation[24], rain[24], energ,energk,pt,p,e,ep,ee;
int main()
{
FILE *arq;
arq = fopen("file2.txt", "r");
if(arq == NULL) {
printf("Error! unable to open file! \n");
getchar();
}
else {
printf("File open... \n");
printf("Reading file...\n");
while (!feof(arq)){
fscanf(arq,"%d %f %f\n", &hour[i], &radiation[i], &rain[i]);
i++;
}
printf("Type desired hour \n");
scanf("%d", &h);
printf("type board area m^2\n");
scanf("%d", &a);
printf("type maximum board pontency in WATTS/M^2\n");
scanf("%f", &p);
ep=p*efi; //Board potential after calculated its standart eficiency
printf("Total eficiency of the board is %.2f %", ep);
pt= a*ep;
printf("Total board potential is %.2f\n", pt);
energ= (pt*radiacao[h]); // generated energy
energk=(float)energ/1000; // generated energy in kw
printf("At %d hoour, there is an average of %.2f KW/m^2 radiation\n %.2f mm of rain\n and the total energy produced by the board will be %.2f Kw \n ", hour[h], radiation[h], rain[h], energk);
printf("\n\n\n press <enter> to finish");
getchar();
}
fclose(arq);
}
ステップ1:fscanf(arq、%d%f%f \ n "、&hour [i]、&radiation [i]、&rain [i]);の戻り値をチェックする。 - それが3であるかどうかを確認する。 – chux
または 'while(!feof(arq)){'?コードループが25回繰り返されるのはなぜですか? 25回目の反復を防ぐ方がよい。 – chux
私の教授=/ –