翊瑋 發問時間: 電腦與網際網路程式設計 · 1 0 年前

將c語言改成c++

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define TRUE 1

double exponential (double);

int factorial (int);

int main (void)

{

double x;

double sum = 0;

printf( "enter value of x: ");

scanf( "%lf", &x);

sum += exponential(x);

printf( "Taylor Series of e^x is approximately = %.6f\n", sum);

system("PAUSE");

return 0;

}

double exponential(double x)

{

double e;

int n;

n=0;

while (TRUE)

{

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

if( e < 0.000001)

break;

printf ( "%3f\t", e);

n++;

}

return e;

}

int factorial (int n)

{

int factorial=1;

while(n > 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;

    }

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

還有問題?馬上發問,尋求解答。