椭圆的面积为:S=π×a×b(其中a,b分别是椭圆的长半轴,短半轴的长) 注:#include< iomanip >是C++中的标准库,其成员函数主要是对是C++中输出流格式的限定,
#include <iostream>
#include <math.h>
#include <iomanip>
#define PI 3.1415926
using namespace std;
class Ellipse
{
private:
int x1,y1,x2,y2;
public:
Ellipse(int xx1,int yy1,int xx2,int yy2);
double Area();
int GetX1(){return x1;}
int GetY1(){return y1;}
int GetX2(){return x2;}
int GetY2(){return y2;}
};
Ellipse::Ellipse(int xx1,int yy1,int xx2,int yy2)
{
x1 = xx1; y1 = yy1; x2 = xx2; y2 = yy2;
}
double Ellipse::Area()
{
return (double)( PI * fabs(x2-x1) * fabs(y2-y1) / 4 );
}
int main()
{
cout<<"请输入点的位置"<<endl;
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
Ellipse e(x1,y1,x2,y2);
cout << fixed << setprecision(4) << e.Area() << endl;(可以将fixed和setprecision()省略掉)
return 0;
|