试图计算e ^ x的估计值,其中x是用户输入的整数

时间:2010-12-07 21:40:06

标签: c++

  

可能重复:
  Calculating e^x without using any functions

我的尝试

#include <iostream>
#include <iomanip>
using namespace std;

double myE(int num);

int main(){

    double e = myE(10);
    cout<<"e is "<<fixed<<showpoint<<setprecision(5)<<e<<endl;
    return 0;
}
double myE(int num){

    double e = 0;
    double factorial = 1;
 int x;
 cout<<"Enter a value for x in the expression e^x:"<<endl;
 cin>>x;

    for(int j = 0; j <= num; j++){

        for(int i = 1; i <= j; i++){

            factorial = factorial * i; 
        }
  for(int k = 0; k <= x; k++){


        e = e + x / factorial; 
        factorial = 1;
  }}

    return e; 
}

我一直得到错误的答案..我做错了什么

0 个答案:

没有答案