| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3060 人关注过本帖
标题:想请问大神如何将调用多个函数,第一个调用用来读取一个txt文档的数据,第二 ...
只看楼主 加入收藏
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
 问题点数:0 回复次数:27 
想请问大神如何将调用多个函数,第一个调用用来读取一个txt文档的数据,第二个调用的用来将这些数据进行一些运算,第三个调用的用来将运算好的数据写入另一个txt文档
我现在可以直接写可以运行出来,但要调用函数我就不是很会啊 求教!!

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    FILE *fptr1;
    fptr1 = fopen ("Input1.txt","r");
    if (fptr1==NULL){
        printf("Cannot open the file!!");   
    }
    int m,b;  
    while(!feof(fptr1)){
        fscanf(fptr1,"m=%d b=%d\n",&m,&b);
    }
 fclose(fptr1);
    printf("The data is written to the end!!");
    system("PAUSE");
    fptr1 = fopen ("Output1.txt","w");
{
fprintf(fptr1,"filename:Output1\n\n");
fprintf(fptr1,"Linear equation:y=mx+b,which m=%d,b=%d,Output1\n",m,b);
fprintf(fptr1,"-------------------------------------------------\n\n");
fprintf(fptr1,"The coordinates of the linear equation :");
int i,j;
  for(j=19;j>=0;j--){
      for(i=0;i<20;i++){
      if(j==m*i+b){
          fprintf(fptr1,"(%d,%d)",i,j);
      }
}}
 fprintf(fptr1,"\n\n\n\n");

  for(j=19;j>=0;j--){
      for(i=0;i<20;i++){
      if(j==m*i+b){
      fprintf(fptr1,"*");
          }
    else{
    fprintf(fptr1,"_");
}
    }
          fprintf(fptr1,"\n");
      }}}

[此贴子已经被作者于2018-1-6 15:22编辑过]

搜索更多相关主题的帖子: 调用 txt 文档 数据 fprintf 
2018-01-06 15:11
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:0 
不用分三个过程,可以边读边算边写。
2018-01-06 23:20
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
可是老师要求的是要我们调用函数来写,调用三个函数分别写。请问如果要调用函数的话可以怎么写呢
2018-01-09 18:42
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:0 
具体说说:
1、读取txt文档的数据格式,举例
2、取出数据的运算方法,举例
3、运算结果写txt文档的数据格式,举例
2018-01-09 19:35
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
1.读取文档的数据格式
   因为我这道题要输出直线,直线的方程式可以表示为y=mx+b,m是斜率,b是截距,所以我的读取txt文档里面会写:m=多少,b=多少  

2取出数据的运算方法
 我先用两个for循环
for(j=19;j>=0;j--){
      for(i=0;i<20;i++){
定义坐标轴上x和y的范围,就是20*20的大小,然后如果坐标经过y=mx+b这条直线的,我就把这些坐标弄到输出文档里,并且经过这条直线的坐标我就用*来表示,如果坐标不经过这条直线的,我就_来表示

3.输出txt文档的格式就是这样
filename:Output1

Linear equation:y=mx+b,which m=2,b=5,Output1
-------------------------------------------------

The coordinates of the linear equation :(7,19)(6,17)(5,15)(4,13)(3,11)(2,9)(1,7)(0,5)



_______*____________
____________________
______*_____________
____________________
_____*______________
____________________
____*_______________
____________________
___*________________
____________________
__*_________________
____________________
_*__________________
____________________
*___________________
____________________
____________________
____________________
____________________
____________________

而且我需要读取三个txt文档,再分别输出到三个输出txt文档中,三个不同的读取文档里写的都是m=多少,b=多少,但就是m和b的值有所不同,所以输出文档中显示的就是三条不同的直线
2018-01-11 17:36
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:0 
不清楚文件里的数据格式就不能确定如何去读取数据。
给个文件看看好了。
2018-01-11 22:38
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2018-01-12 00:09
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

第一张图是要读取的文档格式,第二张图是运算结果写txt档的数据格式
2018-01-12 00:13
shelleytse
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2018-1-6
收藏
得分:0 
求大佬指教啊,编程小白真是毫无头绪😭😭😭
2018-01-12 18:01
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

#include <stdio.h>
#include <stdlib.h>

int _Read(int *m, int *b)
{
    FILE *fp = fopen("Input1.txt", "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(unsigned int *c, int m, int b)
{
    FILE *fp = fopen("Output1.txt","w");
    if (fp==NULL)
    {
        printf("Cannot open the file!!");
        return 0;
    }
    fprintf(fp,"filename:Output1\n\n");
    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 main(void)
{
    int m, b;
    if (!_Read(&m, &b))
        return 0;
    unsigned int c[20]= {0};
    _Coordinates(c, m, b);
    _Write(c, m, b);
    printf("The data is written to the end!!\n");
    system("PAUSE");
    return 0;
}
2018-01-13 15:47
快速回复:想请问大神如何将调用多个函数,第一个调用用来读取一个txt文档的数据 ...
数据加载中...
 
   



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

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