第十二周上机任务4

来源:互联网 发布:java string得到其中 编辑:程序博客网 时间:2024/04/29 19:52
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:renwu.cpp                              * 作    者:宋本一                              * 完成日期:2012 年 5 月 6 日* 版 本 号:12.3       * 对任务及求解方法的描述部分* 输入描述:略 * 问题描述:略 * 程序输出:略* 程序头部的注释结束*/#include<iostream>    #include<Cmath>   using namespace std;  const double pi=3.1415926;  class Point //定义坐标点类    {    public:    Point(){x=0;y=0;}    Point(double x0,double y0) {x=x0; y=y0;}     ~Point ()    {    cout<<"Point类析构函数执行完毕(Destructor function performs finished)"<<endl;    }    double get_x(){return x;}    double get_y(){return y;}    void set_x(double n){ x=n;}    void set_y(double n){y=n;}    friend ostream &operator << (ostream & output, Point & c);     private:    double x,y;   //点的横坐标和纵坐标    };    ostream &operator << (ostream & output, Point & c)  {  output<<"该点的横坐标为:"<<c.x<<"    "<<"纵坐标为:"<<c.y<<endl;     return output;    }  class Circle: public Point   //利用坐标点类定义圆类, 其基类的数据成员表示圆的中心    {    public:    Circle(double xx,double yy,double dd): Point(xx,yy) ,d(dd){}//构造函数    ~Circle()    {   cout<<"Circle类析构函数执行完毕(Destructor function performs finished)"<<endl;  }    friend ostream &operator << (ostream & output, Circle & c);  friend double locate(Point &,Circle &);  friend void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5);  bool operator > (Circle &t);      bool operator < (Circle &t);      bool operator >= (Circle &t);      bool operator <= (Circle &t);      bool operator == (Circle &t);      bool operator != (Circle &t);      private:    double d;    }; void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5)  {  double n;  n=c1.get_x ()+((p1.get_x ()-c1.get_x ())*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))));  p4.set_x (n);  n=c1.get_x ()-((p1.get_x ()-c1.get_x ())*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))));  p5.set_x (n);  n=c1.get_y ()+((p1.get_y ()-c1.get_y ())*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))));  p4.set_y (n);  n=c1.get_y ()-((p1.get_y ()-c1.get_y ())*c1.d )/(sqrt((p1.get_x ()-c1.get_x ())*(p1.get_x ()-c1.get_x ())+((p1.get_y ()-c1.get_y ())*(p1.get_y ()-c1.get_y ()))));  p5.set_y (n);  }  ostream &operator << (ostream & output, Circle & c)  {  output<<"圆的半径为:"<<c.d<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;      return output;      }  double locate(Point &p,Circle &c)  {  double s,d,m;  s=(c.get_x()-p.get_x () )*(c.get_x()-p.get_x () )+(c.get_y ()-p.get_y () )*(c.get_y ()-p.get_y () );  m=sqrt(s);  d=m-c.d ;  return d;  }  bool Circle::operator > (Circle &t)    {      double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;  if(s1>s2 )    return true;    else    return false;    }   bool Circle::operator < (Circle &t)     {    double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;  if(s1<s2 )     return true;    else    return false;    }  bool Circle::operator >= (Circle &t)    {      double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;   if (s1<s2)      return false;      return true;      }      bool Circle::operator <= (Circle &t)    {      double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;   if (s1>s2)      return false;      return true;      }      bool Circle::operator == (Circle &t)     {    double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;   if (s1<s2)      return false;      if (s1>s2)      return false;      return false;    }    bool Circle::operator != (Circle &t)    {    double s1,s2;  s1=pi*d*d;  s2=pi*t.d*t.d;   if (s1==s2)      return false;    return true;    }    int main( )  {  Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1  Point p1(1,1),p2(3,-2),p3(7,3);  //分别位于c1内、上、外      cout<<"圆c1: "<<c1;  cout<<"点p1: "<<p1;  cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;  cout<<"点p2: "<<p2;  cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;    cout<<"点p3: "<<p3;  cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;  cout<<endl; cout<<"圆c1: "<<c1;  if(c1>c2) cout<<"大于"<<endl;  if(c1<c2) cout<<"小于"<<endl;   if(c1>=c2) cout<<"大于等于"<<endl;  if(c1<=c2) cout<<"小于等于"<<endl;   if(c1==c2) cout<<"等于"<<endl;   if(c1!=c2) cout<<"不等于"<<endl;   cout<<"圆c2: "<<c1;  cout<<endl;       Point p4,p5;  crossover_point1(p1,c1, p4, p5);      cout<<"点p1: "<<p1;  cout<<"与圆c1: "<<c1;  cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;  cout<<"交点: "<<p4;  cout<<"交点: "<<p5;  cout<<endl;   system("PAUSE");  return 0;  }  


运行结果:

圆c1: 圆的半径为:4圆的圆心为(3,2)
点p1: 该点的横坐标为:1    纵坐标为:1
点p1在圆c1之内
点p2: 该点的横坐标为:3    纵坐标为:-2
点p2在圆c1之上
点p3: 该点的横坐标为:7    纵坐标为:3
点p3在圆c1之外

圆c1: 圆的半径为:4圆的圆心为(3,2)
小于
小于等于
不等于
圆c2: 圆的半径为:4圆的圆心为(3,2)

点p1: 该点的横坐标为:1    纵坐标为:1
与圆c1: 圆的半径为:4圆的圆心为(3,2)
的圆心相连,与圆交于两点,分别是:
交点: 该点的横坐标为:-0.577709    纵坐标为:0.211146
交点: 该点的横坐标为:6.57771    纵坐标为:3.78885

请按任意键继续. . .

 

原创粉丝点击