PTA 7-3 数据的最大值问题(重载+函数模板) (20分) c++

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

AC代码

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
#include<iostream>
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;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值