| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:文件问题,大家帮忙看看
只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:7 
文件问题,大家帮忙看看
求n以内(不包括n)同时能被3和7整除的所有自然数之和的平方根s,并作为函数值返回,最后结果s输出到文件out.dat中。例如若n为1000时,函数值应为:s=153.909064。
怎么将函数的返回值输出到文件中
搜索更多相关主题的帖子: 自然数 平方根 
2011-07-13 21:23
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
/*
*求n以内(不包括n)同时能被3和7整除的所有自然数之和的平方根s,并作为函数值返回,最后结果s输出到文件out.dat中。例如若n为1000时,函数值应为:s=153.909064。
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double countValue(int n)
{
    int i,sum;
    double s;

    i=1;
    sum=0;

    while(i<n)
    {
        if((i%3==0)&&(i%7==0))
        {
            sum=sum+i;
            i++;
        }
    s=sqrt(sum);
    if(s<0)
        s=-s;

    return s;
    }
}
int main()
{
    int n;
    FILE *fp;
    printf("please input the value of n:\n");
    scanf("%d",&n);
    countValue(n);

    char ch,*fname="D:\\out.dat";
    if((fopen(fname,"w"))==NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
   
    fprintf(fp,"%lf",countValue(n));
    fclose(fp);
   
return 0;

}
2011-07-14 00:13
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
求指点
2011-07-14 00:14
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:20 
fp = fopen(fname, "w");
    if(fp == NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
你的countValue函数设计有问题。

我们都在路上。。。。。
2011-07-14 11:18
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
查了好久没查出,求指出
2011-07-14 21:12
wjm22
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:44
专家分:118
注 册:2011-7-5
收藏
得分:0 
是不是要加个等于n
2011-07-14 21:55
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
"不包括n"
2011-07-14 22:23
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double countValue(int n)
{
    int i,sum;
    double s;

    i=1;
    sum=0;

    while(i<n)
    {
        if((i%3==0)&&(i%7==0))
        {
            sum=sum+i;
       
        }
    i++;       
    s=sqrt((double)sum);
    if(s<0)
        s=-1*s;

    }
        return s;
}
int main()
{
    int n;
    FILE *fp;

    char *fname="out.dat";
    printf("please input the value of n:\n");
    scanf("%d",&n);
    //countValue(n);


    if((fp=fopen(fname,"w"))==NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
   
    fprintf(fp,"%f",countValue(n));
    fclose(fp);
   
return 0;

}
正确版的
2011-07-14 23:07
快速回复:文件问题,大家帮忙看看
数据加载中...
 
   



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

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