请朋友们帮我指点一下(谢谢):编C++程序求e的x次方的值执行相当怪异,

诺希亚 2009-02-13 10:06:39
e(x)=1+x(1)/1!+x(2)/2!+.....+x(n)/n!
编程求e(x)的值
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
using namespace std;


int main()
{
double product=1,quotient=0,x=0,value=0;
ofstream outfile("C:\\out.OUT");
while(x>-1&&x<1)
{
outfile<<"Enter x: "<<endl;
cin>>x;
outfile<<"x="<<setprecision(2)<<x<<endl;

for(int n=1;n<=500;n++)
{
product*=n;
quotient=pow(x,n)/product;
value+=quotient;
}

value+=1;
outfile<<"the sum of e of the x-the power is "<<setprecision(5)<<fixed<<value<<endl;
}
outfile.close();
return 0;
}

结果输出为:
Enter x:
x=0.01
the sum of e of the x-the power is 1.01005
Enter x:
x=0.02
the sum of e of the x-the power is 2.01005
Enter x:
x=0.03
the sum of e of the x-the power is 3.01005
Enter x:
x=0.04
the sum of e of the x-the power is 4.01005
Enter x:
x=0.05
the sum of e of the x-the power is 5.01005
Enter x:
x=0.06
the sum of e of the x-the power is 6.01005
Enter x:
x=0.07
the sum of e of the x-the power is 7.01005
Enter x:
x=0.08
the sum of e of the x-the power is 8.01005
Enter x:
x=0.09
the sum of e of the x-the power is 9.01005
Enter x:
x=0.10
the sum of e of the x-the power is 10.01005
Enter x:
x=0.20
the sum of e of the x-the power is 11.01005
Enter x:
x=0.30
the sum of e of the x-the power is 12.01005
Enter x:
x=0.40
the sum of e of the x-the power is 13.01005
Enter x:
x=0.50
the sum of e of the x-the power is 14.01005
Enter x:
x=0.60
the sum of e of the x-the power is 15.01005
Enter x:
x=0.70
the sum of e of the x-the power is 16.01005
Enter x:
x=0.80
the sum of e of the x-the power is 17.01005
Enter x:
x=0.90
the sum of e of the x-the power is 18.01005
Enter x:

另一次输出为:
Enter x:
x=0.1
the sum of e of the x-the power is 1.10517
Enter x:
x=0.20
the sum of e of the x-the power is 2.10517
Enter x:
x=0.30
the sum of e of the x-the power is 3.10517
Enter x:
x=0.40
the sum of e of the x-the power is 4.10517
Enter x:
x=0.50
the sum of e of the x-the power is 5.10517
Enter x:
x=0.60
the sum of e of the x-the power is 6.10517
Enter x:
x=0.70
the sum of e of the x-the power is 7.10517
Enter x:
x=0.80
the sum of e of the x-the power is 8.10517
Enter x:
x=0.90
the sum of e of the x-the power is 9.10517
Enter x:
哪位朋友帮我指点一下,哪里出错了
非常感谢
...全文
855 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
T_mon 2009-02-13
  • 打赏
  • 举报
回复

int main()
{
double product=1,quotient=0,x=0,value=0;
ofstream outfile("C:\\out.OUT");
while(x>-1&&x <1)
{
outfile < <"Enter x: " < <endl;
cin>>x;
outfile < <"x=" < <setprecision(2) < <x < <endl;
product=1,quotient=0,x=0,value=0;//注意
for(int n=1;n <=500;n++)
{
product*=n;
quotient=pow(x,n)/product;
value+=quotient;
}

value+=1;
outfile < <"the sum of e of the x-the power is " < <setprecision(5) < <fixed < <value < <endl;
}
outfile.close();
return 0;
}

T_mon 2009-02-13
  • 打赏
  • 举报
回复
int main() 
{
double product=1,quotient=0,x=0,value=0;
ofstream outfile("C:\\out.OUT");
while(x>-1&&x <1)
{
outfile < <"Enter x: " < <endl;
cin>>x;
outfile < <"x=" < <setprecision(2) < <x < <endl;
product=1,quotient=0,x=0,value=0; //这里需要重新赋值,并且没有必要算500项
for(int n=1;n <=500;n++)
{
product*=n;
quotient=pow(x,n)/product;
value+=quotient;
}

value+=1;
outfile < <"the sum of e of the x-the power is " < <setprecision(5) < <fixed < <value < <endl;
}
outfile.close();
return 0;
}
waizqfor 2009-02-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 huih303 的帖子:]
e(x)=1+x(1)/1!+x(2)/2!+.....+x(n)/n!
编程求e(x)的值
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;


int main()
{
double product=1,quotient=0,x=0,value=0;
ofstream outfile("C:\\out.OUT");
while(x>-1&&x <1)
{
outfile < <"Enter x: " < <endl;
cin>>x;
outfile < <"x=" < <setprecision(2) < <x < <endl;

f…
[/Quote]
LZ变量定义的时候
double product=1,quotient=0,x=0,value=0;
product的范围太小了吧 精度会不够的吧 换个精度高的
youdang45 2009-02-13
  • 打赏
  • 举报
回复
试了下

product会溢出,用long double能解决

还有就是处理下一个输入的x前要将product置1, value置0
sagegz 2009-02-13
  • 打赏
  • 举报
回复
当然还得考虑溢出问题了.
youdang45 2009-02-13
  • 打赏
  • 举报
回复
似乎不是我说的问题
sagegz 2009-02-13
  • 打赏
  • 举报
回复
LZ哪里有问题呢?
您的第一次输入都是2位小数,第二次输入都是1位小数,结果当然2次不一样啦!
youdang45 2009-02-13
  • 打赏
  • 举报
回复
处理完第一个输入的x后product要恢复到1
ltc_mouse 2009-02-13
  • 打赏
  • 举报
回复
product能存得下500!吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧