数据的最大值问题(重载+函数模板)

两个类如下设计:类time有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载>(<)(大于号或者小于号)的成员函数。

要求设计一个函数模板template <class T> double maxn(T x[], int len) 对int,float,time和date或者其他类型的数据,返回最大值。

主函数有如下数据成员:

int intArray[100];

double douArray[100];time timeArray[100];

date dateArray[100];

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

输入格式:

每行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为time类型,4为date类型,若为整型元素,接着输入整型数据,以0结束。若为浮点型元素,接着输入浮点型数据,以0结束。若为time型元素, 输入time型数据(hh1 mm1 ss1 hh2 mm2 ss2),以0结束。若为date型数据,输入date型数据(year1 month1 day1 year2 month2 day2),以0结束。输入-1时表示全体输入结束。

  1 #include <iostream>
  2 using namespace std;
  3 
  4 class time
  5 {
  6 private:
  7     int hh, mm, ss;
  8 public:
  9     time()
 10     {
 11         hh = 0;
 12         mm = 0;
 13         ss = 0;
 14     }; //构造函数
 15     time(int h, int m, int s): hh(h), mm(m), ss(s) {};  //构造函数
 16     void settime(int h, int m, int s)
 17     {
 18         hh = h, mm = m, ss = s;
 19     };
 20     friend time operator - (time A, time B);
 21     friend bool operator > (time A, time B);
 22     friend ostream& operator << (ostream &output, time &A);
 23 };
 24 
 25 time operator - (time A, time B)    //在该程序中无实际用途,忽略
 26 {
 27     return time(A.hh - B.hh, A.mm - B.mm, A.ss - B.ss); //实际情况需要考虑负数情况
 28 }
 29 
 30 bool operator > (time A, time B)    //重载大于号,用于比较
 31 {
 32     return A.hh * 3600 + A.mm * 60 + A.ss > B.hh * 3600 + B.mm * 60 + B.ss;
 33 }
 34 
 35 ostream& operator << (ostream &output, time &A)     //重载输出流函数,方便输出
 36 {
 37     output << A.hh << " " << A.mm << " " << A.ss;
 38     return output;
 39 }
 40 
 41 class date
 42 {
 43 private:
 44     int year, month, day;
 45 public:
 46     date() {};
 47     date(int y, int m, int d): year(y), month(m), day(d) {};
 48     void setdate(int y, int m, int d)
 49     {
 50         year = y, month = m, day = d;
 51     };
 52     friend bool operator > (date A, date B);
 53     friend ostream& operator << (ostream &output, date &A);
 54 };
 55 
 56 bool operator > (date A, date B)        //重载大于号
 57 {
 58     return A.year * 365 + A.month * 30 + A.day > B.year * 365 + B.month * 30 + B.day;
 59 }
 60 
 61 ostream& operator << (ostream &output, date &A)        //重载输出流,方便输出
 62 {
 63     output << A.year << " " << A.month << " " << A.day;
 64     return output;
 65 }
 66 
 67 template<class T>
 68 T maxn(T x[], int len)        //模板函数
 69 {
 70     T temp = x[0];
 71 
 72     for(int i(1); i < len; i++)        //冒泡比较
 73         if(x[i] > temp)
 74             temp = x[i];
 75 
 76     return temp;
 77 }
 78 
 79 int main()
 80 {
 81     int intArray[100], flag;
 82     double doubleArray[100];
 83     time timeArray[100];
 84     date dateArray[100];
 85     cin >> flag;
 86 
 87     while(flag != -1)
 88     {
 89         switch(flag)
 90         {
 91             case 1:
 92                 {
 93                     int Max, a, i(0);
 94 
 95                     while(cin >> a, a)
 96                         intArray[i++] = a;
 97 
 98                     Max = maxn(intArray, i);
 99                     cout << Max << endl;
100                     break;
101                 }
102 
103             case 2:
104                 {
105                     double Max, a;
106                     int i(0);
107 
108                     while(cin >> a, a)
109                         doubleArray[i++] = a;
110 
111                     Max = maxn(doubleArray, i);
112                     cout << Max << endl;
113                     break;
114                 }
115 
116             case 3:
117                 {
118                     time Max;
119                     int a, b, c, i(0);
120 
121                     while(cin >> a, a)
122                     {
123                         cin >> b >> c;
124                         timeArray[i++].settime(a, b, c);
125                     }
126 
127                     Max = maxn(timeArray, i);
128                     cout << Max << endl;
129                     break;
130                 }
131 
132             case 4:
133                 {
134                     date Max;
135                     int a, b, c, i(0);
136 
137                     while(cin >> a, a)
138                     {
139                         cin >> b >> c;
140                         dateArray[i++].setdate(a, b, c);
141                     }
142 
143                     Max = maxn(dateArray, i);
144                     cout << Max << endl;
145                     break;
146                 }
147         }
148 
149         cin >> flag;
150     }
151 
152     return 0;
153 }
原文地址:https://www.cnblogs.com/wzzdeblog/p/10827593.html