注册 登录
编程论坛 C++教室

一道c++編程題 好久都沒有找出來哪裡出錯 麻煩各位高手幫忙看看

phillip160 发布于 2012-05-11 15:41, 587 次点击
只有本站会员才能查看附件,请 登录

程序代码:
#include"iostream.h"
#include"math.h"
int jiecheng(int x)
{
    int j,k;
    for(j=1,k=1;j<=x;j++)
        k=k*j;
    return k;
}
double fun(double x,double eps=1e-6)
{
    double s;
    int i;
    for(i=1,s=x;pow(x,2*i+1)/jiecheng(2*i+1)>eps;i++)
    {
        s=s+pow(-1,i)*pow(x,2*i+1)/jiecheng(2*i+1);
    }
    s=s+pow(-1,i)*pow(x,2*i+1)/jiecheng(2*i+1);
    return s;
}
void main()
{
    double x;
    cout<<"Input x please    x=";
    cin>>x;
    cout<<'\n'<<fun(x)<<'\n';
}
8 回复
#2
phillip1602012-05-11 15:42
各位好人幫幫忙啊
#3
phillip1602012-05-11 15:48
在线等求帮忙
#4
唯我独魔2012-05-11 16:28
冒个泡,你哪里有问题,我帮你
#5
唯我独魔2012-05-11 16:40
程序代码:
for(i=1,s=x;pow(x,2*i+1)/jiecheng(2*i+1)>eps;i++)
    {
        s=s+pow(-1,i)*pow(x,2*i+1)/jiecheng(2*i+1);
    }
    s=s+pow(-1,i)*pow(x,2*i+1)/jiecheng(2*i+1);
s=s+pow(-1,i)*pow(x,2*i+1)/jiecheng(2*i+1);最后一句去掉,你写两句干吗?
#6
傻瓜菜2012-05-11 23:23
程序代码:
#include<stdio.h>
#include<math.h>
double eps=0.000001;
double cheng(int x)
{
    int j;
double k;
    for(j=1,k=1;j<=x;j++)
        k=k*j;
    return k;
}
double fun(double x)
{
    double s=x,u=1;
    int i;
    for(i=1;u>eps;i++)
    {
u=pow(x,2*i+1)/cheng(2*i+1);
        s=s+pow(-1,i)*pow(x,2*i+1)/cheng(2*i+1);
    }
    s=s+pow(-1,i)*pow(x,2*i+1)/cheng(2*i+1);
    return s;
}
void main()
{
    double x;
    scanf("%lf",&x);
    printf("%lf\n",fun(x));
}
#7
liudw22012-05-12 11:00
代码格式请注意,fun函数第二个S赋值就删了吧,还有fun函数在main里面要修改下
#8
wagnyin092012-05-14 11:35
pow(-1,i)会出现重载错误,添加double a=-1,再使用pow(a,i)
#9
荡气回肠2012-05-15 22:02
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float sum, term, x;
    int sign = 1, k = 2;
   
    cout << "Please input x:" << endl;
    cin >> x;

    sum = x;
    term = x;            
   
    while ( fabs(term) >= pow(10, -6) ) {
        k = 2 * k - 1;
        term *= x * x / k / ( k - 1);
        sign = -sign;
        sum += sign * term;
    }
    cout << sum << endl;

    return 0;
}

[ 本帖最后由 荡气回肠 于 2012-5-16 21:19 编辑 ]
1