Title

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

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

两个类如下设计:类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<iostream>
#include<stdio.h>
using namespace std;

//class Time;
//class date;

class Time{
	private:
		int hh;
		int mm;
		int ss;
		int total;
	public:
		Time(){
			
		};
	    Time(int h,int m,int s):hh(h),mm(m),ss(s){
	    	this->cal_total();
		}	
	    void cal_total()
		{
			this->total=this->hh*3600+this->mm*60+this->ss;
		}
		int get_total()
		{
			return this->total;
		}
		void print()
		{
			cout<<this->hh<<" "<<this->mm<<" "<<this->ss<<endl;
		}
		bool operator>(Time& out)
		{
			if(this->get_total()>out.get_total())
			   return true;
			else return false;
		}
		bool operator<(Time& out)
		{
			if(this->get_total()<out.get_total())
			   return true;
			else return false;
		}
};

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){
			this->cal_total();
		}
		
		void cal_total()
		{
		    this->total=this->year*365+month*30+day;
		}
		int get_total()
		{
			return this->total;
		}
		
		void print()
		{
			cout<<this->year<<" "<<this->month<<" "<<this->day<<endl;
		}
		
			bool operator>(date& out)
		{
			if(this->get_total()>out.get_total())
			   return true;
			else return false;
		}
		bool operator<(date& out)
		{
			if(this->get_total()<out.get_total())
			   return true;
			else return false;
		}
};

template <class T>
int maxn(T x[], int len)
{
	int pos=0;
	for(int i=0;i<len;i++)
	{
		if(x[i]>x[pos])
		{
			pos=i;
		}
	}
	return pos;
}

int main()
{
	int intArray[100];
    double douArray[100];
	Time timeArray[100];
    date dateArray[100];
	int op;
	while(1)
	{
		cin>>op;
		if(op==-1)
		  break;
		int cnt=0;
		switch(op)
	    {
	    	
	    	case 1:
	    		{
	    		     int x;
	    		     while(1)
	    		     {
	    		     	cin>>x;
	    		     	if(x==0)
	    		     	   break;
	    		     	intArray[cnt++]=x;  
					 }
					 int pos= maxn(intArray,cnt);
					 cout<<intArray[pos]<<endl;
					 break;
				}
			case 2:
				{
					 double x;
	    		     while(1)
	    		     {
	    		     	cin>>x;
	    		     	if(x==0)
	    		     	   break;
	    		     	douArray[cnt++]=x;  
					 }
					 int pos= maxn(douArray,cnt);
					 cout<<douArray[pos]<<endl;
					 break;
				}
			case 3:
				{
					 int h,m,s;
	    		     while(1)
	    		     {
	    		     	cin>>h;
	    		     	if(h==0)
	    		     	   break;
	    		     	   
	    		     	cin>>m>>s;
	    		     	timeArray[cnt++]=Time(h,m,s);  
					 }
					 int pos= maxn(timeArray,cnt);
					 timeArray[pos].print();
					 break;
				}
			case 4:
				{
					 int y,m,d;
	    		     while(1)
	    		     {
	    		     	cin>>y;
	    		     	if(y==0)
	    		     	   break;
	    		     	   
	    		     	cin>>m>>d;
	    		     	dateArray[cnt++]=date(y,m,d);  
					 }
					 int pos= maxn(dateArray,cnt);
					 dateArray[pos].print();
					 break;
				}
		}
	}
	return 0;
}

注意

如果使用了#include<bits/stdc++.h>,最好命名不要太显而易见。time这个命名就有点问题。

小总结

模板函数

结构

template <class T>
返回类型 函数名(参数列表)
{
    内容
}

使用

直接函数名(参数)

二元运算符重载

		bool operator运算符(类名& out)
		{
			if(比较的内容)
			   return true;
			else return false;
		}
posted @ 2021-05-29 20:10  BeautifulWater  阅读(377)  评论(0编辑  收藏  举报