zoukankan      html  css  js  c++  java
  • 5-7 点到原点的距离(多态)

    给出下面的一个基类框架:

    class Point
    {
     protected:
    int dimension;//点的维数,最多不超过100维
    private:
     int point_length[100];//点数组
    public:
    Point ();//构造函数根据需要重载
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    }
    

    以Point为基类建一个派生类Point_2D,增加以下数据成员:

    float x;//2D平面上点的x坐标
    float y;//2D平面上点的y坐标
    

    增加以下成员函数:

    Point_2D类的无参和参数化构造函数
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    

    以Point为基类建一个派生类Point_3D,增加以下数据成员:

    float x;//3D平面上点的x坐标
    float y;//3D平面上点的y坐标
    float y;//3D平面上点的z坐标
    

    增加以下成员函数:

    Point_3D类的无参和参数化构造函数
    float distance( );//计算当前点到原点的距离
    void display();//输出点
    

    生成上述类并编写主函数,要求主函数有一个基类Point指针数组pt,数组元素不超过10个

    Point *pt [10];
    

    主函数根据输的点信息,相应建一个广义点对象类对象或Point_3D类对象或Point_2D类,并且取址按序赋给基类指针数组元素,最后遍历基类Point指针数组 pt,计算每一个点到原点的距离。

    输入格式:

    测试输包含一个测试用例,每个测试用例占一行,每一行给出一个点的基本信息,每行的第一个数字为当前点的类型,1为广义点,1后面输入一个数字m表示该点维数,后面跟随m个数字为m个坐标,2为2维点,后面跟随两个数字,分别为x和y,3为3维点,后面跟随三个数字分别为x、y、z。

    提示:应用虚函数实现多态

    输入样例:

    1 5 1 1 1 1 1
    2 3 4
    3 1 2 2
    0
    

    输出样例:

    Distance from Point(1,1,1,1,1) to original point is 2.23607
    Distance from Point(3,4) to original point is 5
    Distance from Point(1,2,2) to original point is 3


    • 时间限制:400ms
    • 内存限制:64MB
    • 代码长度限制:16kB
    • 判题程序:系统默认
    • 作者:余春艳
    • 单位:浙江大学
    #include<iostream>
    #include<cmath>
    using namespace std;
    //广义点 
    class Point
    {
    protected:
        int dimension;//点的维数,最多不超过100维
    private:
        float point_length[100];//点数组
    public:
        Point ( ){};//构造函数根据需要重载
        Point (int a , float b[]) : dimension(a){
            //这里实现传指针赋值给数组
            for(int i=0;i<a;i++)
            {
                point_length[i] = b[i];
            }
        };
        virtual float distance( ){
            float ans=0;
            for(int i=0;i<dimension;i++)
            {
                ans += (point_length[i] * point_length[i]);
            }
            return sqrt(ans);
        };
        virtual void display( ){
        //Distance from Point(1,1,1,1,1) to original point is 2.23607
        if(dimension == 1 ){
            cout<<"Distance from Point"<<point_length[0]<<" to original point is " << fabs(point_length[0]) <<endl;
        }
        else{
            cout<<"Distance from Point(";
            for(int i=0;i<dimension-1;i++)
            {
                cout<<point_length[i]<<",";
            }
            cout<<point_length[dimension-1]<<") to original point is "<<distance()<<endl;
            };        
        }
    
    };
    //二维点 
    class Point_2D : public Point{
    protected:
        float x;//2D平面上点的x坐标
        float y;//2D平面上点的y坐标
    public:    
        Point_2D( ) { };//无参和参数化构造函数
        Point_2D(float a,float b) : Point( ) , x(a) , y(b){ dimension = 2; };
        float distance( ) { return sqrt(x*x + y*y); };//计算当前点到原点的距离
        void display( ) { 
        //Distance from Point(3,4) to original point is 5
            cout<<"Distance from Point("<<x<<","<<y<<") to original point is "<<distance()<<endl;
        };//输出点
    }; 
    //以Point为基类建一个派生类Point_3D,增加以下数据成员:
    //三维点 
    
    class Point_3D : public Point{
    protected:
        float x;//3D平面上点的x坐标
        float y;//3D平面上点的y坐标
        float z;//3D平面上点的z坐标
    public:/**/
        Point_3D( ){ };//无参和参数化构造函数
        Point_3D(float a,float b,float c) : Point( ) , x(a) , y(b) , z(c) { dimension = 3; };
        float distance( ) { return sqrt( x*x + y*y + z*z ); };//计算当前点到原点的距离
        void display( ){
        //Distance from Point(1,2,2) to original point is 3
        cout<<"Distance from Point("<<x<<","<<y<<","<<z<<") to original point is "<<distance()<<endl;
        } ;//输出点
    };
    //****注意!派生类的成员成员函数要有定义,不然编译不过“returned 1” 
    
    
    int main()
    {
        int type,count=0;
        Point *pt [10];
        
        while(cin>>type){
            if(type == 0)break;                                            
            switch(type){
            case 1:{
                int len;
                float hello[100];
                cin>>len;
                for(int i=0;i<len;i++){
                    cin>>hello[i];
                } 
                pt[count++] = new Point(len,hello);             
                break;
            }
            case 2:{
                float a,b;
                cin>>a>>b;
                pt[count++] = new Point_2D(a,b);            
                break;
            }
            case 3:{
                float a,b,c;
                cin>>a>>b>>c;
                pt[count++] = new Point_3D(a,b,c);
                break;
            }
            }
        }
        for(int i=0;i<count;i++) 
        {
            pt[i]->display();
        }
        return 0;
    }
    个人分享,欢迎指导,未经允许,请勿转载。谢谢!
  • 相关阅读:
    redis客户端windows版中文乱码解决方案
    nginx做负载均衡,怎么在有宕机情况出现时保证网站的响应速度
    支付宝同步和异步验签结果不一致的解决方法
    @ResponseBody中文乱码解决方案
    [javamail]AUTH LOGIN failed;Invalid username or password报错
    Could not load driverClass ${driverClassName} 的解决方案
    eclipse中,maven报错maven.multiModuleProjectDirectory system property is not set
    spring bean初始化和销毁方法
    关于静态资源是否应该放到WEB-INF目录
    使用Jedis出现Connection refused的解决方案
  • 原文地址:https://www.cnblogs.com/hello-OK/p/7045737.html
Copyright © 2011-2022 走看看