c++程序设计报告(四)

来源:互联网 发布:汽车电脑编程设备 编辑:程序博客网 时间:2024/06/03 15:48

/*
*实验内容:  

* 程序的版权和版本声明部分  

* Copyright (c) 2011, 烟台大学计算机学院学生  

* All rights reserved.  

* 文件名称:* 作 者: 杨坛坛  

* 完成日期: 2012 年 5 月 09日  

* 版 本号: vc2008

*问题描述:

(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要的输入输出函数
(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0 圆外;
(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;
(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出

*/

 

#include <iostream>#include <string.h>#include <cmath>#define PI 3.14using namespace std;class Point{public:double x,y;public:Point(double x0,double y0){x=x0;y=y0;}Point(){x=0;y=0;}~ Point(){}double Getx(){return x;}double Gety(){return y;}    friend ostream&operator << (ostream&,Point&);};ostream&operator << (ostream&output,Point&p){output<<"点的坐标为:"<<"("<<p.Getx()<<","<<p.Gety()<<")";return output;}class Circle:public Point{protected:double r;public:Circle(double x0,double y0,double r1):Point(x0,y0){r=r1;}~Circle(){}double Getr(){return r;}friend int locate(Point&, Circle&);friend bool operator>(Circle& c1,Circle& c2);    friend bool operator<(Circle& c1,Circle& c2);    friend bool operator>=(Circle& c1,Circle& c2);    friend bool operator<=(Circle& c1,Circle& c2);    friend bool operator==(Circle& c1,Circle& c2);    friend bool operator!=(Circle& c1,Circle& c2);friend void crossover_point1(Point&,Circle&,Point&,Point&);    friend ostream&operator << (ostream&,Circle&);};ostream&operator<<(ostream&output,Circle&C){output<<"圆心坐标为:"<<"("<<C.Getx()<<","<<C.Gety()<<")"<<"半径为:"<<C.Getr();return output;}int locate(Point& p, Circle& c){double t;t=sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));if(t>c.Getr()){return 1;}if(t==c.Getr()){return 0;}if(t<c.Getr()){return -1;}}bool operator>(Circle& c1,Circle& c2){double ares1,ares2;ares1=PI*c1.Getr()*c1.Getr();ares2=PI*c2.Getr()*c2.Getr();if(ares1>ares2){return true;}elsereturn false;}bool operator<(Circle& c1,Circle& c2){double ares1,ares2;ares1=PI*c1.Getr()*c1.Getr();ares2=PI*c2.Getr()*c2.Getr();if(ares1<ares2){return true;}elsereturn false;} bool operator==(Circle& c1,Circle& c2) {double ares1,ares2;ares1=PI*c1.Getr()*c1.Getr();ares2=PI*c2.Getr()*c2.Getr();if(ares1==ares2){return true;}elsereturn false; }bool operator>=(Circle& c1,Circle& c2){if(c1>c2&&c1==c2){return true;}else{return false;}}  bool operator<=(Circle& c1,Circle& c2){    if(c1<c2&&c1==c2){return true;}else{return false;}}  bool operator!=(Circle& c1,Circle& c2){if(c1==c2){return false;}else{return true;}}void crossover_point1(Point&p,Circle&c,Point&p4,Point&p5){p4.x=c.Getx()+(p.Getx()-c.Getx()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));p5.x=c.Getx()-(p.Getx()-c.Getx()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));    p4.y=c.Gety()+(p.Gety()-c.Gety()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));    p5.y=c.Gety()-(p.Gety()-c.Gety()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));}int main( ){Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1Point 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: 圆心坐标为:(3,2)半径为:4点p1: 点的坐标为:(1,1)点p1在圆c1之内
点p2: 点的坐标为:(3,-2)点p2在圆c1之上
点p3: 点的坐标为:(7,3)点p3在圆c1之外

圆c1: 圆心坐标为:(3,2)半径为:4小于
不等于
圆c2: 圆心坐标为:(3,2)半径为:4
点p1: 点的坐标为:(1,1)与圆c1: 圆心坐标为:(3,2)半径为:4的圆心相连,与圆交于两点
,分别是:
交点: 点的坐标为:(-1.91935,-1.1305)交点: 点的坐标为:(7.91935,5.1305)
请按任意键继续. . .

原创粉丝点击