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

三个类如下设计:类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

 

 

-----------------------------------------------------------------------

              参考代码

-----------------------------------------------------------------------

  1 #include<iostream>
  2 #include<cmath>
  3 using namespace std;
  4 //实现Time类 
  5 class Time
  6 {
  7     private:
  8         int hh;
  9         int mm;
 10         int ss;
 11     public:
 12         Time()//无参构造函数 
 13         {
 14             hh = 0;
 15             mm = 0;
 16             ss = 0;
 17         }
 18         void set(int h,int m,int s)//赋值成员数据 
 19         {
 20             hh=h;
 21             mm=m;
 22             ss=s;
 23         }
 24         friend int operator-(Time ,Time);
 25         friend istream& operator>>(istream &,Time &);
 26 };
 27 //重载>>
 28 istream & operator>>(istream & set,Time &t)
 29 {
 30     set>>t.hh>>t.mm>>t.ss;
 31     return set;
 32 }
 33 //重载-
 34 int operator-(Time t1,Time t2)
 35 {
 36     int totalSecond1=t1.hh*3600+t1.mm*60+t1.ss;
 37     int totalSecond2=t2.hh*3600+t2.mm*60+t2.ss;
 38     return totalSecond2-totalSecond1; 
 39 } 
 40 //实现date类  
 41 class date
 42 {
 43     private:
 44         int year;
 45         int month;
 46         int day;
 47     public:
 48         date()
 49         {
 50             year = 0;
 51             month = 0;
 52             day = 0;
 53         }
 54         void set(int y,int m,int d)
 55         {
 56             year=y;
 57             month=m;
 58             day=d;
 59         }
 60         friend int operator-(date, date);
 61         friend istream& operator>>(istream &,date &); 
 62 };
 63 //重载>>
 64 istream & operator>>(istream & set,date &d)
 65 {
 66     set>>d.year>>d.month>>d.day;
 67     return set;
 68 }
 69 //重载-
 70 int operator-(date d1,date d2)
 71 {
 72     int totalDay1=d1.year*365+d1.month*30+d1.day;
 73     int totalDay2=d2.year*365+d2.month*30+d2.day;
 74     return totalDay1-totalDay2;
 75 } 
 76 //实现point
 77 class Point
 78 {
 79 private:
 80     int x;
 81     int y;
 82 public:
 83     Point()
 84     {
 85         x=y=0;
 86     }
 87     void set(int a,int b)
 88     {
 89         x=a;
 90         y=b;        
 91     }
 92     friend double operator-(Point,Point);
 93     friend istream& operator>>(istream &,Point &);
 94 };
 95 //重载>>
 96 istream & operator>>(istream & set,Point &p)
 97 {
 98     set>>p.x>>p.y;
 99     return set;
100 }
101 //重载-
102 double operator-(Point p1,Point p2)
103 {
104     return sqrt((1.0*p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
105 } 
106 int main()
107 {
108     int classType;
109     while(cin>>classType)
110     {
111         if(classType==0)break;
112         switch(classType)
113         {
114             case 1://int
115                 {
116                     int a,b;
117                     cin>>a>>b;
118                     cout<<abs(b-a)<<endl;
119                 }break;
120             case 2://double
121                 {
122                     double a,b;
123                     cin>>a>>b;
124                     cout<<abs(b-a)<<endl;
125                 }break;
126             case 3://Point
127                 {
128                     Point a,b;
129                     cin>>a>>b;
130                     cout<<abs(b-a)<<endl;
131                 }break;
132             case 4://Time
133                 {
134                     Time a,b;
135                     cin>>a>>b;
136                     cout<<abs(b-a)<<endl;
137                 }break;
138             case 5://date
139                 {
140                     date a,b;
141                     cin>>a>>b;
142                     cout<<abs(b-a)<<endl;
143                 }break;
144         }    
145     } 
146     return 0;
147 } 

 

欢迎指教,一起学习!

未经本人允许,请勿转载!

谢谢!

posted @ 2017-06-01 01:34  hello_OK  阅读(1263)  评论(0编辑  收藏  举报