`
java-mans
  • 浏览: 11446505 次
文章分类
社区版块
存档分类
最新评论

第十二周实验指导--任务四--类的组合与继承

 
阅读更多

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:类的组合与继承

* 作 者: 雷恒鑫
* 完成日期: 2012 年 04月 29 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:由下面派生类Student1对基类Student的继承……
* 问题描述:(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<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);
    //double get_d(){return d;}
	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;
}

运行结果:



以上程序有一点小小的问题:

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);
}
应该这样改一下:

void crossover_point1(Point &p1,Circle &c1,Point &p4,Point &p5)
{
double n;
n=c1.get_x ()+(sqrt((p1.get_x ()-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 ()-(sqrt((p1.get_x ()-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 ()+(sqrt((p1.get_y ()-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 ()-(sqrt((p1.get_y ()-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);
}

虽然运行结果一样,但是还是改后的程序比较鲁棒。

以下是改后的源代码:

#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);  
    //double get_d(){return d;}  
    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 ()+(sqrt((p1.get_x ()-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 ()-(sqrt((p1.get_x ()-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 ()+(sqrt((p1.get_y ()-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 ()-(sqrt((p1.get_y ()-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;  
}  

运行结果:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics