7-1 数据的最大值问题(重载+函数模板) (60 分)
两个类如下设计:类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 4 5 9 3 7 0
2 4.4 5.5 6.9 3.2 2.7 0
3 18 21 22 18 20 31 18 21 49 0
4 2013 5 14 2013 5 15 2013 4 1 0
-1
样例输出:
9
6.9
18 21 49
2013 5 15
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ture true #define flase false class Time{ public : void setx(double a1,double b1,double c1){ h=a1; m=b1; s=c1; } bool operator >(Time b){ double sum1,sum2; sum1=h*3600+m*60+s; sum2=b.h*3600+b.m*60+b.s; if(sum1>sum2){ return ture; } else return flase; } friend ostream& operator <<(ostream&os ,Time&); private: double h,m,s; }; ostream& operator <<(ostream&os,Time&a){ os<<a.h<<" "<<a.m<<" "<<a.s; } class Date{ public : void setx(double a1,double b1,double c1){ h=a1; m=b1; s=c1; } bool operator >(Date b){ double sum1,sum2; sum1=h*365+m*30+s; sum2=b.h*365+b.m*30+b.s; if(sum1>sum2){ return ture; } else return flase; } friend ostream& operator <<(ostream& ,Date&); private: double h,m,s; }; ostream& operator <<(ostream&os ,Date&a){ os<<a.h<<" "<<a.m<<" "<<a.s; } template <class T> double dist(T a[],int n){ T maxx=a[0]; for(int i=1;i<n;i++){ if(a[i]>maxx){ maxx=a[i]; } } cout<<maxx<<endl; return 0; } int main() { int n; cin>>n; int intArray[100]; double douArray[100]; Time timeArray[100]; Date dateArray[100]; while(n!=-1){ if(n==1){ int a,b=0; cin>>a; while(a){ intArray[b]=a; b++; cin>>a; } dist(intArray,b); } else if(n==2){ double a;int b=0; cin>>a; while(a){ douArray[b]=a; b++; cin>>a; } dist(douArray,b); } else if(n==3){ double a1,b1,c1;int b=0; cin>>a1; while(a1){ cin>>b1>>c1; timeArray[b].setx(a1,b1,c1); cin>>a1; b++; } dist(timeArray,b); } else if(n==4){ double a1,b1,c1;int b=0; cin>>a1; while(a1){ cin>>b1>>c1; dateArray[b].setx(a1,b1,c1); cin>>a1; b++; } dist(dateArray,b); } cin>>n; } return 0; }