| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3157 人关注过本帖
标题:想请问大神如何将调用多个函数,第一个调用用来读取一个txt文档的数据,第二 ...
只看楼主 加入收藏
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
哇感谢!!!
那如果我不止一个Input和一个Output,而是有三个m,b的值不同的输入txt文档,分别输出到三个输出文档的话要怎么改呢?
就是输入是Input1 Input2 Input3
输出是Output1 Output2 Output3
2018-01-13 16:53
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10589
专家分:43132
注 册:2014-5-20
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>

int _Read(char *path, int *m, int *b)
{
    FILE *fp = fopen(path, "r");
    if (fp==NULL)
    {
        printf("Cannot open the file!!");
        return 0;
    }
    if (fscanf(fp,"m=%d b=%d",m,b) != 2)
    {
        fclose(fp);
        printf("Cannot read the data!!");
        return 0;
    }
    fclose(fp);
    return 1;
}

int _BitTest(unsigned int *a, int n, int bit)
{
    return (a[n]>>bit)&1;
}

void _BitSet(unsigned int *a, int n, int bit)
{
    a[n] |= 1<<bit;
}

void _Coordinates(unsigned int *c, int m, int b)
{
    int y, x;
    for (x=0; x<20; ++x)
    {
        y = m*x+b;
        if (y>=0 && y<20)
            _BitSet(c,y,(19-x));
    }
}

int _Write(char *path, unsigned int *c, int m, int b)
{
    FILE *fp = fopen(path,"w");
    if (fp==NULL)
    {
        printf("Cannot open the file!!");
        return 0;
    }
    fprintf(fp,"filename:%s\n\n", path);
    fprintf(fp,"Linear equation:y=mx+b,which m=%d,b=%d,Output1\n",m,b);
    fprintf(fp,"-------------------------------------------------\n\n");
    fprintf(fp,"The coordinates of the linear equation :");
    int i, j;
    for(i=19; i>=0; --i)
    {
        j = m*i+b;
        if (j>=0 && j<20)
            fprintf(fp, "(%d,%d)", i, j);
    }
    fprintf(fp, "\n\n\n\n");
    for (i=19; i>=0; --i)
    {
        for (j=19; j>=0; --j)
            fprintf(fp, "%c", _BitTest(c,i,j)?'*':'_');
        fprintf(fp, "\n");
    }
    fclose(fp);
    return 1;
}

int _test(char *inPath, char *outPath)
{
    int m, b;
    if (!_Read(inPath, &m, &b))
        return 0;
    unsigned int c[20]= {0};
    _Coordinates(c, m, b);
    _Write(outPath, c, m, b);
    return 1;
}

int main(void)
{
    _test("Input1.txt", "Output1.txt");
    _test("Input2.txt", "Output2.txt");
    _test("Input3.txt", "Output3.txt");
    printf("The data is written to the end!!\n");
    system("PAUSE");
    return 0;
}
2018-01-13 17:27
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
为什么会显示 cannot open the file 呢?
2018-01-13 18:43
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10589
专家分:43132
注 册:2014-5-20
收藏
得分:0 
以下是引用shelleytse在2018-1-13 18:43:58的发言:

为什么会显示 cannot open the file 呢?

这个要问自己
那几个数据文件有吗
2018-01-13 18:55
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
有的哇 但我在两台电脑上运行都说cannot open the file
请问大神你能运行出来吗?
2018-01-13 19:08
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10589
专家分:43132
注 册:2014-5-20
收藏
得分:0 
以下是引用shelleytse在2018-1-13 19:08:09的发言:

有的哇 但我在两台电脑上运行都说cannot open the file
请问大神你能运行出来吗?

可能是文件路径问题
如果那几个数据文件与程序代码文件不在一起的,要写明路径。
2018-01-13 19:33
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
噢噢原来是这样我之前分开放了 现在可以读了,但是三个Output都是空白,输出不了东西欸
2018-01-13 20:45
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10589
专家分:43132
注 册:2014-5-20
收藏
得分:0 
以下是引用shelleytse在2018-1-13 20:45:21的发言:

噢噢原来是这样我之前分开放了 现在可以读了,但是三个Output都是空白,输出不了东西欸

参考6楼
2018-01-13 22:02
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2018-01-13 22:54
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10589
专家分:43132
注 册:2014-5-20
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

2018-01-14 06:19
快速回复:想请问大神如何将调用多个函数,第一个调用用来读取一个txt文档的数据 ...
数据加载中...
 
   



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

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