gpt4 book ai didi

c - 如何在 C 中打开 .txt 文件并连续将方程应用于每一行

转载 作者:行者123 更新时间:2023-11-30 16:47:13 25 4
gpt4 key购买 nike

我必须为大学编写一个代码,其中我必须读取一个包含三列的 .txt 文件:一天中的小时、太阳辐射、雨量计(毫米),并根据以下内容连续将方程应用于每一行用户给出的信息。我设法构建了将方程应用于用户一次选择的单行的代码。我如何将该方程应用于每一行并同时显示它们?

我正在使用的 .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

我的代码如下(请原谅不完美的地方,我是编程初学者):

#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);
}

最佳答案

 #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 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);

int i;
for(i = 0; i < 24; i++)
{
energ= (pt*radiation[i]); // 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 ", i, radiation[i], rain[i], energk);

}
scanf("%d", &a);
}
fclose(arq);
}

关于c - 如何在 C 中打开 .txt 文件并连续将方程应用于每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com