# 將c語言改成c++

#include &amp;lt;stdio.h&amp;gt;

#include &amp;lt;stdlib.h&amp;gt;

#include &amp;lt;math.h&amp;gt;

#define TRUE 1

double exponential (double);

int factorial (int);

int main (void)

{

double x;

double sum = 0;

printf( &amp;quot;enter value of x: &amp;quot;);

scanf( &amp;quot;%lf&amp;quot;, &amp;amp;x);

sum += exponential(x);

printf( &amp;quot;Taylor Series of e^x is approximately = %.6f&amp;#92;n&amp;quot;, sum);

system(&amp;quot;PAUSE&amp;quot;);

return 0;

}

double exponential(double x)

{

double e;

int n;

n=0;

while (TRUE)

{

e = pow(x, n) / factorial(n);

if( e &amp;lt; 0.000001)

break;

printf ( &amp;quot;%3f&amp;#92;t&amp;quot;, e);

n++;

}

return e;

}

int factorial (int n)

{

int factorial=1;

while(n &amp;gt; 1)

{

factorial *= n;

n--;

}

return factorial;

}

#include

#include

#include

#define TRUE 1

double exponential (double);

int factorial (int);

### 1 個解答

• 最佳解答

如果只是要改，你敢交？原程式根本是錯的！而且不只錯一個地方！

#include <cmath>

#include <cstdlib>

#include <iostream>

#include <iomanip>

using namespace std;

int factorial(int n)

{ int factorial = 1;

while (n >= 2)

factorial *= n--;

return factorial;

}

double exponential(double x)

{ double exp, err;

int n;

exp = n = 0;

while ((err = pow(x, n) / factorial(n)) >= .000001 && n < 13)

{ exp += err;

cout << fixed << setprecision(3) << exp << '\n';

n++;

}

return exp;

}

int main (void)

{ double x;

cout << "enter value of x: ";

cin >> x;

cout << "Taylor Series of e^x is approximately = " << fixed << setprecision(6) << exponential(x) << '\n';

system("PAUSE");

return 0;

}

原程式我找到的錯都改了。你試試看吧！