(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
已提供代码:
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;
return 0;
}
(5)在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。自编main函数完成测试。
(6)与圆心相连的直线:给定一点p,其与圆心相连成的直线,会和圆有两个交点,如图。在上面定义的Point(点)类和Circle(圆)类基础上,设计一种方案,输出这两点的坐标。
提示:
(6)与圆心相连的直线:给定一点p,其与圆心相连成的直线,会和圆有两个交点,如图。在上面定义的Point(点)类和Circle(圆)类基础上,设计一种方案,输出这两点的坐标。
提示:
解答代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称: test.cpp
*作 者:满星辰
*完成日期:2015 年 5 月 25 日
*版本号:v1.0
*/
#include <iomanip>
#include <cstring>
#include <cmath>
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const double PI=3.14;
class Circle;
class Point
{
protected:
double x,y;
public:
Point(double xx=0,double yy=0):x(xx),y(yy) {}
friend istream& operator >>(istream& input,Point p);
friend ostream& operator <<(ostream& output,Point p);
friend double locate(Point p,Circle c);
friend void jiaodian(Point p,Circle c);
};
istream& operator >>(istream& input,Point p)
{
cin>>p.x>>p.y;
return input;
}
ostream& operator <<(ostream& output,Point p)
{
cout<<"("<<p.x<<","<<p.y<<")"<<endl;
return output;
}
class Circle:public Point
{
protected:
double r;
public:
Circle(double xx,double yy,double rr):Point(xx,yy),r(rr) {}
friend istream& operator >>(istream& input,Circle c);
friend ostream& operator <<(ostream& output,Circle c);
bool operator > (Circle &c);
bool operator < (Circle &c);
bool operator >= (Circle &c);
bool operator <= (Circle &c);
bool operator == (Circle &c);
bool operator != (Circle &c);
friend double locate(Point p,Circle c);
friend void jiaodian(Point p,Circle c);
};
istream& operator >>(istream& input,Circle c)
{
cin>>c.x>>c.y>>c.r;
return input;
}
ostream& operator <<(ostream& output,Circle c)
{
cout<<"圆心:"<<"("<<c.x<<","<<c.y<<")"<<endl;
cout<<"半径:"<<c.r<<endl;
}
bool Circle::operator > (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1>s2)
return true;
else
return false;
}
bool Circle::operator < (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1<s2)
return true;
else
return false;
}
bool Circle::operator >= (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1>=s2)
return true;
else
return false;
}
bool Circle::operator <= (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1<=s2)
return true;
else
return false;
}
bool Circle::operator == (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1==s2)
return true;
else
return false;
}
bool Circle::operator != (Circle &c)
{
double s1=PI*r*r;
double s2=PI*c.r*c.r;
if(s1!=s2)
return true;
else
return false;
}
double locate(Point p,Circle c)
{
double l=sqrt((p.x-c.x)*(p.x-c.x)+(p.y-c.y)*(p.y-c.y))-c.r;
return l;
}
void jiaodian(Point p,Circle c)
{
Point p1,p2;
p1.x=c.x+sqrt(c.y*c.y/(1+(c.y-p.y)*(c.y-p.y)/((c.x-p.x)*(c.x-p.x))));
p1.y=(c.y-p.y)/(c.x-p.x)*(p1.x-p.x)+p.y;
p2.x=c.x-sqrt(c.y*c.y/(1+(c.y-p.y)*(c.y-p.y)/((c.x-p.x)*(c.x-p.x))));
p2.y=(c.y-p.y)/(c.x-p.x)*(p1.x-p.x)+p.y;
cout<<"点"<<p<<"与圆"<<c<<"的圆心相连的直线交圆的两点为:"<<endl;
cout<<p1<<p2<<endl;
}
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;
if (c1>c2) cout<<"c1>c2"<<endl;
if (c1<c2) cout<<"c1<t2"<<endl;
if (c1==c2) cout<<"c1=c2"<<endl;
if (c1!=c2) cout<<"c1≠c2"<<endl;
if (c1>=c2) cout<<"c1≥c2"<<endl;
if (c1<=c2) cout<<"c1≤c2"<<endl;
cout<<endl;
jiaodian(p1,c1);
return 0;
}
心得:
其实完全没有看起来那么难= ,=
一步一步做下去也挺条理清晰的
图片: