| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 311 人关注过本帖
标题:帮忙寻找一下这个程序的错误,谢谢
只看楼主 加入收藏
Areik
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2009-9-28
结帖率:87.5%
收藏
已结贴  问题点数:10 回复次数:1 
帮忙寻找一下这个程序的错误,谢谢
题目要求是
   Write a program, that after the entering of a real number  x  and a real number  eps (the accuracy) computes
    the value for sin_x  (until  |Term(n)|<eps complies) , using factoring over  n terms.
    Print  x,  sin_x, sin(x) and the n-th term. Use functions Fac() and Term() according to:

    sin_x = Term1 + Term2 + Term3 + …  =  x - x3/3! + x5/5! - x7/7! + ….     [ example:  Fac(3) = 1.2.3 ]


#include <math.h>
#include <stdio.h>
int Fac(unsigned int i)
{
    unsigned int fac = 1;
    while(i)
        fac *= i--;
    return fac;
}
double Term(double x, unsigned int i, char last_sign)
{
    if(last_sign)
    {
        last_sign = 0;
        return pow(x,(double)i)/(double)(Fac(i));
    }
    else
    {
        last_sign = 1;
        return (-1.00)*pow(x,(double)i)/(double)(Fac(i));
    }
}

#define DEG2RAD(x) x*3.1415926/180.00f
int main()
{
   double x,eps;
   double sum=1;
   double term;
   unsigned int i;
   char last_sign = 0;

   printf("Please input your x:\t");
   scanf("%lf",&x);
   printf("Please input your eps:\t");
   scanf("%lf",&eps);
   i = 1;
   term = x;
   while(term < eps)
   {
       sum += term;
       i+=2;
       term = Term(x,i,last_sign);
   }
   printf("x is %lf, sin_x is %lf, and sin(x) is %lf\n", x, sum, sin(x));
   return 0;
}


当输入X为1,eps为0.5时,最终给出的SIN_X答案为1。老师说这个是错误的,请前辈们帮忙看一下该如何修改.谢谢








搜索更多相关主题的帖子: factoring entering include number terms 
2009-10-25 20:28
ouyangouyang
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:273
专家分:579
注 册:2009-10-8
收藏
得分:10 
int Fac(unsigned int i)
{
    unsigned int fac = 1;
    while(i)
        fac *= i--; /*这句写得比较费解,可改成fac*=i;i--;*/
    return fac;
}
double Term(double x, unsigned int i, char last_sign)
{
    if(last_sign)
    {
        last_sign = 0;
        return pow(x,(double)i)/(double)(Fac(i));
    }
    else
    {
        last_sign = 1;
        return (-1.00)*pow(x,(double)i)/(double)(Fac(i)); /*里面的i变量没必要改成double类型直接就是unsingned int*/
    }
}
#define DEG2RAD(x) x*3.1415926/180.00f /*如果答案错了肯定就是这句你后面的sin(x),应该为sin(DE2RAD(x))*/
我说的不知道对不对。

多少恨, 昨夜梦魂中。 还似旧时游上苑, 车如流水马如龙; 花月正春风!
2009-10-26 08:40
快速回复:帮忙寻找一下这个程序的错误,谢谢
数据加载中...
 
   



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

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