| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 752 人关注过本帖
标题:请教一下这个文本文档的问题。。。
只看楼主 加入收藏
Areik
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2009-9-28
结帖率:87.5%
收藏
已结贴  问题点数:10 回复次数:4 
请教一下这个文本文档的问题。。。
该程序是要求输入一个电源电压和两个电阻值,把他们写入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;
}
搜索更多相关主题的帖子: 文本 文档 
2009-12-18 13:15
sidooh
Rank: 4
等 级:业余侠客
帖 子:121
专家分:265
注 册:2009-6-26
收藏
得分:5 
在fin = fopen("curres.txt", "r");之前

加上

fclose(fout);


测试通过的代码
程序代码:
#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));
    }
    fclose(fout);

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

2009-12-18 13:31
Areik
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2009-9-28
收藏
得分:0 
谢谢您的回复,我试过了,依然是那样的情况,我在输入完U和R1,R2之后,按回车键,依然没有关闭curres.txt,就停在这里不动了。
图片附件: 游客没有浏览图片的权限,请 登录注册
2009-12-23 16:13
hnliji1107
Rank: 4
等 级:业余侠客
帖 子:53
专家分:216
注 册:2009-10-23
收藏
得分:5 
LZ的程序主要是输入R2时的循环用错了,还有第一次文件打开后没有关闭。以下是修改后的程序:
程序代码:
#include <stdio.h>

int main(void)
{   
    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: ");
    scanf("%f", &R2);

    if(R2 < 0.00f)
        printf("Invalid!\n");
    else
        fprintf(fout,"%f\n%f\n",R2, U/(R1+R2));
    fclose(fout);/* 注意 */

    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\n%f\n",&fRes, &fCur) != EOF)
    {
        float fPow = fCur*fCur*fRes;
        printf("\n%f*%f*%f = %f\n",fCur, fCur, fRes, fPow);
        fprintf(fout,"%f\n%f\n%f\n", fRes, fCur, fPow);
    }
    fclose(fin);
    fclose(fout);
    printf("\nPress any key to continue...");
    getch();
    return 0;
} 
图片附件: 游客没有浏览图片的权限,请 登录注册
2009-12-24 17:54
sidooh
Rank: 4
等 级:业余侠客
帖 子:121
专家分:265
注 册:2009-6-26
收藏
得分:0 
你的问题描述说的很清楚了

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).

并不是停在那里不动了,而是等待下一次输入,一直不停的输入R2,直到R2为0,程序结束,R2是指内的各个电阻
2009-12-24 18:25
快速回复:请教一下这个文本文档的问题。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019615 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved