• 数据的间距问题(重载+函数模板)


    数据的间距问题(重载+函数模板) (60 分)

    三个类如下设计:类cTime有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类point有两个数据成员,x,y分别坐标,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载-(减号)的成员函数。 要求设计一个函数模板template <class T> double dist(T a, T b) 对int,float,cTime,point和date或者其他类型的数据,返回间距。

    其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于cTime和date类型,数据在转换成ss或者day后进行运算。

    输入格式:

    每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,4,为time类型,5为date类型,若为整型元素,接着输入两个整型数据,

    若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 x2 y2),若为time型元素, 输入两个cTime型数据(hh1 mm1 ss1 hh2 mm2 ss2),若为date型数据,输入两个date型数据(year1 month1 day1 year2 month2 day2)。输入0时标志输入结束。

    输出格式:

    对每个输入,每行输出一个间距值。

    样例输入:

    1 2 5
    
    4 18 21 22 18 20 31
    
    3 2 4 5 9
    
    5 2013 5 14 2013 5 15
    
    2 2.2 9.9
    
    0
    

    样例输出:

    3
    
    51
    
    5.83095
    
    1
    
    7.7
    

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    class cTime{
    	private:
    		int hh;
    		int mm;
    		int ss;
    		int total;
    	public:
    		cTime(){
    			
    		}
    		cTime(int h,int m,int s):hh(h),mm(m),ss(s){
    			total=hh*3600+mm*60+ss;
    		}
    		
    		int get_total()
    		{
    			return this->total;
    		}
    		
    		int  operator-(cTime& out)
    		{
    			return this->total-out.get_total();
    		}
    };
    
    class point{
    	private:
    		double x;
    		double y;
    	public:
    		point(){
    			
    		}
    		point(double ox,double oy):x(ox),y(oy){
    		
    		}
            double operator-(point& out)
            {
            	return sqrt((this->x-out.x)*(this->x-out.x)+(this->y-out.y)*(this->y-out.y));
    		}
    };
    class date{
    	private:
    		int year;
    		int month;
    		int day;
    		int total;
    	public:
    		date(){
    			
    		}
    		date(int y,int m,int d):year(y),month(m),day(d)
    		{
    		    total = year*365+month*30+day;	
    		}
    		
    		int get_total()
    		{
    			return this->total;
    		}
    		
    		int operator-(date& out)
    		{
    			return this->total-out.total;
    		}
    };
    
    template <class T>
    double dist(T a,T b)
    {
    	return abs(a-b);
    }
    
    int main()
    {
    	int ops;
    	while(1)
    	{
    		cin>>ops;
    		if(ops==0)
    		   break;
    		switch(ops)
    		{
    			case 1:
    				{   
    				    int x,y;
    				    cin>>x>>y;
    				    cout<<dist(x,y)<<endl;
    					break;
    				}
    			case 2:
    				{
    					double x,y;
    					cin>>x>>y;
    					cout<<dist(x,y)<<endl;
    					break;
    				}
    			case 3:
    				{
    					point x,y;
    					double ax,ay,bx,by;
    					cin>>ax>>ay>>bx>>by;
    					x=point(ax,ay);y=point(bx,by);
    					cout<<dist(x,y)<<endl;
    					break;
    				}
    			case 4:
    				{
    					cTime x,y;
    					int hh1,mm1,ss1,hh2,mm2,ss2;
    					cin>>hh1>>mm1>>ss1>>hh2>>mm2>>ss2;
    					x=cTime(hh1,mm1,ss1);
    					y=cTime(hh2,mm2,ss2);
    					cout<<dist(x,y)<<endl;
    					break;
    				}
    			case 5:
    				{
    					date x,y;
    					int hh1,mm1,ss1,hh2,mm2,ss2;
    					cin>>hh1>>mm1>>ss1>>hh2>>mm2>>ss2;
    					x=date(hh1,mm1,ss1);
    					y=date(hh2,mm2,ss2);
    					cout<<dist(x,y)<<endl;
    					break;
    				}
    		}
    	}
    	return 0;
    }
    

    心得体会

    • learning in coding
  • 相关阅读:
    php使用时间戳保存时间的意义
    php输出控制函数存在的意义
    php中foreach使用引用的陷阱
    mac下php添加openssl扩展
    gitlab配置自动同步
    lnmp集成环境tp nginx vhost配置
    上传文件中文文件名乱码的解决方法以及iconv函数的使用
    php返回数据格式
    怎样让Git忽略当前已经更改的文件
    connect() php-cgi.sock failed (2: No such file or directory)
  • 原文地址:https://www.cnblogs.com/BeautifulWater/p/14826314.html
Copyright © 2020-2023  润新知