对于类与对象的简单运用(代码)

来源:互联网 发布:3d棋牌游戏源码 编辑:程序博客网 时间:2024/06/09 17:17

// Note:Your choice is C++ IDE
#include <iostream>
#include <cmath>
using namespace std;
class Point                             //直角座标系的Point(点)类
{
   
 public:
   double x;                           //将x,y设为公用变量,以便派生类函数可以访问
   double y;
   Point(){x=0;y=0;}                     //默认构造函数
   Point(double X,double Y){x=X;y=Y;}    //实现初始化的构造函数
   double Distance(Point,Point);         //类中的成员函数
   void midPoint(Point,Point,Point);
   void slope(Point,Point);
};

double Point::Distance(Point c1,Point c2)              //成员函数Distance计算两点间距离
{
 double l;
 l=sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));       //计算公式
 return (l);
}

void Point::midPoint(Point c1,Point c2,Point c3)        //成员函数midpoint计算两点的中点坐标
{
    c3.x=(c1.x+c2.x)/2;
    c3.y=(c1.y+c2.y)/2;
    cout<<"两点的中点坐标是:("<<c3.x<<","<<c3.y<<")"<<endl;
}

void Point::slope(Point c1,Point c2)                   //成员函数slope计算两点的斜率
{
 double n;
 n=(c2.y-c1.y)/(c2.x-c1.x);
 cout<<"两点的斜率:"<<n<<endl;
}

class Circle:private Point                         //以Point类为基类,派生出一个Circle(圆)类
{ public:
     double r;                                  //新增数据变量半径r
 public:
  Circle(double X,double Y,double R):Point(X,Y) {r=R;}
      void circumference(Circle);     
   void area(Circle);
   void check(double,double,Circle);
   void judge(double,Circle,Circle);
};

void Circle::circumference(Circle R1)                //成员函数circumference计算圆的周长
{ double C,p=3.141;
  C=2*p*R1.r;                                   
  cout<<"圆的周长:"<<C<<endl;
}

void Circle::area(Circle R1)                          //成员函数area计算圆的面积
{
  double S,p=3.141;
  S=R1.r*R1.r;
  cout<<"圆的面积:"<<S<<endl;
}

void Circle::check(double m,double L,Circle R1)         //成员函数check判断点在圆内、圆上、圆外 
{
    if(m==R1.r)
   {cout<<"所以c1(1,1)在以(0,0)为圆心且以2为半径的圆R1上"<<endl;}              //用if语句进行判断
 if(m<R1.r)
   {cout<<"所以c1(1,1)在以(0,0)为圆心且以2为半径的圆R1内"<<endl;}
 if(m>R1.r)
   {cout<<"所以c1(1,1)在以(0,0)为圆心且以2为半径的圆R1外"<<endl;}
 if(L==R1.r)
   {cout<<"所以c2(0,2)在以(0,0)为圆心且以2为半径的圆R1上"<<endl;}
 if(L<R1.r)
   {cout<<"所以c2(0,2)在以(0,0)为圆心且以2为半径的圆R1内"<<endl;}
 if(L>R1.r)
   {cout<<"所以c2(0,2)在以(0,0)为圆心且以2为半径的圆R1外"<<endl;}
}
void Circle::judge(double t,Circle R1,Circle R2)      //判断两圆的位置关系
{  
 cout<<"两圆R1与R2的半径分别为"<<R1.r<<","<<R2.r<<endl;
 if(R1.r<R2.r)R1.r=R2.r;
 if(t==(R1.r+R2.r))
 {cout<<"所以圆R1与圆R2外切"<<endl;}
 if(t==(R1.r-R2.r))
 {cout<<"所以圆R1与圆R2内切"<<endl;}
 if(t<(R1.r+R2.r)&&t>(R1.r-R2.r))
 {cout<<"所以圆R1与圆R2相交"<<endl;}
 if(t>(R1.r+R2.r))
 {cout<<"所以圆R1与圆R2外离"<<endl;}
 if(t<(R1.r-R2.r)&&t>0)
 {cout<<"所以圆R1与圆R2内含"<<endl;}
 if(t==0)
 {cout<<"所以圆R1与圆R2同心"<<endl;}
 
}
class Line:private Point                       //以Point类为基类,派生出一个Line(线)类
{private:
 double k;
 double b;
 public:
                                                   //构造函数
      Line(double k0,double b0):k(k0),b(b0){}
      Line(double x0,double y0,double k0):Point(x0,y0),k(k0){}
      Line(double x1,double y1,double k0,double b0):Point(x1,y1),k(k0),b(b0){}
 
 void judge_valuea(Line,Line);        
 void judge_valueb(Line,Point);
 double Dis(Line,Point);
 void judge_valuec(Line,double,Point,Circle);
 void display_l();
 void line_q(Point,Point,Circle);
};
void Line::display_l()
      {
      if(b>0)cout<<"直线 Y="<<k<<"*X+"<<b;
         else if(b==0) cout<<"直线 Y="<<k<<"*X";
          else cout<<"直线 Y="<<k<<"*X"<<b;
      }

void Line::judge_valuea( Line L1,Line L2)        //判断两直线关系
{
 if(L1.k==L2.k)cout<<"两线平行"<<endl;
 else cout<<"两线相交"<<endl;
 cout<<"两线相交的交点为"<<"("<<(L1.b-L2.b)/(L2.k-L1.k)<<","   //计算两线的交点坐标
 <<((L1.b-L2.b)/(L2.k-L1.k))*L1.k+L1.b<<")"<<endl;
}
void Line::judge_valueb(Line L4,Point c1)        //判断点与直线的关系
{
 if(c1.y==L4.k*c1.x+L4.b)cout<<"点c1(1,1)在线y="<<L4.k<<"*x+"<<L4.b<<"上"<<endl;
 else cout<<"点c1(1,1)不在线y="<<L4.k<<"*x+"<<L4.b<<"上"<<endl;
}
double Line::Dis(Line L3,Point c5)                //计算点到直线的距离
{
 return(fabs((L3.k*c5.x-c5.y+L3.b)/(sqrt(1.0+L3.k*L3.k))));    //点到直线的距离公式
}

void Line::judge_valuec(Line ,double i,Point c5,Circle R2)                    //判断线与圆的关系
{
 double a,b1,c;
 long double x1,x2,y1,y2;
 
 a=1+k*k;                                            //计算线与圆的交点,通过圆与线方程联立求得
 b1=2*k*(b-c5.y)-2*c5.x;            
 c=(b-c5.y)*(b-c5.y)+c5.x*c5.x-R2.r*R2.r;
 x1=((-b1)+sqrt(b1*b1-4*a*c))/2*a;
 y1=k*x1+b;
 x2=((-b1)-sqrt(b1*b1-4*a*c))/2*a;
 y2=k*x2+b;
 if(i==2)
 {
  cout<<"所以线与圆的位置关系为相切"<<endl;
     cout<<"线与圆的切点为("<<x1<<","<<y2<<")"<<endl;
 }
 if(i<2)
 {   
  cout<<"所以线与圆的位置关系为相交"<<endl;
     cout<<"线与圆的交点为:"<<"交点一("<<x1<<","<<y1<<");交点二("<<x2<<","<<y2<<")"<<endl;
   
 }
    if(i>2)cout<<"所以线与圆的位置关系为相离"<<endl;

}

void  Line::line_q(Point c5,Point c6,Circle R2)
 {
 double s,t;
 double g; 
 double k1=(c6.y-c5.y)/(c6.x-c5.x);
 double m,n;
 s=sqrt((c6.x-c5.x)*(c6.x-c5.x)+(c6.y-c5.y)*(c6.y-c5.y));
    t=sqrt((s*s-R2.r*R2.r));
    g=R2.r/t;
    m=(g+k1)/(1-g*k1);
    n=(g+m)/(1-g*m);
    double b1=c5.y-m*c5.x;
    double b2=c5.y-n*c5.x;
    Line L9(m,b1);
    Line L_1(n,b2);
    cout<<"与圆相切的直线分别为:";
    L9.display_l();
    cout<<"     "<<endl;
    L_1.display_l();
}


class Triangle:private Point             //以Point类为基类,派生出一个Triangle(三角形)类
{private:
 double a;
 double b;
 double c;
 
 public:
    Triangle(){a=0;b=0;c=0;}
    Triangle(double a0,double b0,double c0){a=a0;b=b0;c=c0;}
    Triangle(double x3,double y3,double x4,double y4,double x5,double y5)
    {Point c3(x3,y3);
     Point c4(x4,y4);
     Point c5(x5,y5);
     a=c3.Distance(c3,c4);
     b=c3.Distance(c3,c5);
     c=c4.Distance(c4,c5);
    }
    void judge_valued();
    void judge_valuee(Triangle,Point,Point,Point);
};
 void Triangle::judge_valued()
    {
     if(a+b>c&&a+c>b&&b+c>a)
       {
        cout<<"此三边能够成三角形!"<<endl<<"三角形的面积为:";
        double l=(a+b+c)/2;
         double s=sqrt(l*(l-a)*(l-b)*(l-c));
         cout<<s<<endl;
        }
      else
       { 
        cout<<"输入错误,不能构成三角形故不能进行计算!"<<endl;
       }
    }
void Triangle::judge_valuee(Triangle T1,Point c5,Point c7,Point c8)
    {
     Point c1=c7;
     Point c2=c8;
   
     double a=c1.Distance(c1,c2);
     double b=c1.Distance(c1,c5);
     double c=c2.Distance(c2,c5);
     Triangle t(a,b,c);
     cout<<"由直线与圆的交点坐标和圆心所构成的";
     t.judge_valued();
    }


 
int main()                                   //主函数
{ double j,m,L,t,i;
    Point c1(1,1),c2(0,2),c3,c4(0,0),c5(2,3),c6(5,6),c7(1,2),c8(2,6);
    Circle R1(0,0,2),R2(2,3,2);       //设置圆R1和圆R2的值
    Line L1(0,0,3,-1),L2(0,0,2,2);
    Line L4(0,0,1,1);               
    Line L3(0,0,1,-1);
    Line L5(0,0,0,1);
    j=c1.Distance(c1,c2);                    //通过对象c1调用另一个对象c2进行运算执行函数Distance
      cout<<"两点的距离是:"<<j<<endl;
 c3.midPoint(c1,c2,c3);
 c1.slope(c1,c2);
 R1.circumference(R1);
 R1.area(R1);
 m=c1.Distance(c1,c4);
   cout<<"点c1(1,1)到圆心(0,0)的距离是:"<<m<<endl;
 L=c2.Distance(c2,c4);
   cout<<"点c2(0,2)到圆心(2,3)的距离是:"<<L<<endl;
 R1.check(m,L,R1);
 t=c4.Distance(c4,c5);
   cout<<"两圆心的距离为:"<<t<<endl;                //计算两圆的圆心的距离
     R1.judge(t,R1,R2);                            //通过对象R1进行判断
 cout<<"线L1:y=3x-1与线L2:y=2x+2的关系为:";                   
      L1.judge_valuea(L1,L2);
    L4.judge_valueb(L4,c1);
    i=L3.Dis(L3,c5);                            //计算圆心到直线的距离
      cout<<"以2为半径的圆R2的圆心(2,3)到直线L3:y=x-1的距离为:"<<i<<endl;
      L3.judge_valuec(L3,i,c5,R2);
    i=L5.Dis(L5,c5);
      cout<<"以2为半径的圆R2的圆心(2,3)到直线L5:y=1的距离为:"<<i<<endl; 
      L5.judge_valuec(L5,i,c5,R2);
    Triangle T1(2,3,1,2,2,6);
      T1.judge_valuee(T1,c5,c7,c8);
    L3.line_q(c5,c6,R2);                         
 return 0;
}

原创粉丝点击