请教一下这个文本文档的问题。。。
该程序是要求输入一个电源电压和两个电阻值,把他们写入curres文本,然后读取他们,进行电流和功率的计算。I=U/(R1+R2),P=I^2*R.然后在新的文本powcur上列出结果。我写的这个程序,在输入电压和电阻之后,电脑进行了计算,电流的结果也出现在了curres文本里,但是为何程序就不再进行下去了?请前辈们帮忙看一下。。。原题:Given is a circuit with two resistors (R1 and R2) in series, connected to a voltage source (U).
U and R1 are being entered and then a number of values for R2 are being entered. While entering the value(s)
for R2 the current I flowing through it is calculated.
Write a program that for each value of R2 being entered writes this value and the current I to a textfile
(to stop the program, enter 0 for R2).
Next, write a program that reads this textfile and calculates for each pair (I,R2) the power P = I2R.
The triplet (I,R2,P) is to be written to a new textfile.
After all values are written, the content of this new file is to be ptited to the screen.
我写的如下:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
FILE *fout,*fin;
float R1,R2,I,P1,P2;
float U,P;
float fRes, fCur;
fout = fopen("curres.txt", "w");
printf("Please enter U and R1: ");
scanf("%f %f", &U,&R1);
while(U < 0.00f || R1 < 0.00f)
{
printf("Invalid!\nPlease enter U and R1: ");
scanf("%f %f", &U,&R1);
}
printf("Please enter R2:\n");
while(1)
{
scanf("%f", &R2);
if(!R2)break;
if(R2 < 0.00f)
printf("Invalid!\n");
else
fprintf(fout,"%f %f\n",R2, U/(R1+R2));
}
fin = fopen("curres.txt", "r");
if (fin == NULL)
{
printf("ERROR: the file is not opened!\n");
exit (0);
}
fout = fopen("powcur.txt", "w");
if (fout == NULL)
{
printf("ERROR: the file is not opened!\n");
exit (0);
}
while(fscanf(fin,"%f%f",&fRes, &fCur) != EOF)
{
float fPow = fCur*fCur*fRes;
printf("%f*%f*%f = %f\n",fCur, fCur, fRes, fPow);
fprintf(fout,"%f %f %f\n", fRes, fCur, fPow);
}
fclose(fin);
fclose(fout);
printf("Press any key to continue. . .\n");
getch();
return 0;
}