当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

使用C++实现椭圆周长的计算

    我们数学上经常遇到的一个问题,椭圆周长的计算,今天我们用程序来实现一下看看吧。

 // use 2nd one

// calculate circumference of an ellipse

#include \"iostream\"

using namespace std;

#define _USE_MATH_DEFINES

#include \"cmath\"

const int LIMIT = 10; // items number

double circumference(double semimajor, double semiminor);

int main()

{

     double res;

     res = circumference(1, 2);

     cout << \"The circumference is: \" << res << endl;

}
double circumference(double semimajor, double semiminor)

{
double item[LIMIT];

     double e;

     if(semimajor >= semiminor)

     {

         e = sqrt(1 - pow(semiminor / semimajor, 2));

     }

     else

     {

         e = sqrt(1 - pow(semimajor / semiminor, 2));

     }

     double l_deno, l_numa, pe, r_deno, mo, ne;
int i;

     for(int j = 1; j < LIMIT; ++j)

     {

         for(i = 1, l_deno = l_numa = mo = 1, ne = 2; i < j + 1;

              ++i, mo += 2, ne += 2)

         {

              l_numa *= mo;

              l_deno *= ne;

         }

         pe = pow(e, 2 * j);
r_deno = 2 * j - 1;

 

         item[j - 1] = l_numa * l_numa * pe /(l_deno * l_deno * r_deno);

     }

 

     double tmp = 0.0;

     for(int k = 0; k < LIMIT - 1; ++k)

     {

         tmp += item[k];

     }

     double l = 2 * M_PI * semimajor * (1 - tmp);
 return l;

}

相关内容
赞助商链接