C++学生生日差值计算(运算符重载)

题目描述
定义一个学生类Student,包含该学生的姓名、出生年、月、日 ,重定义
“-”号实现两个学生之间相差多少天的比较。并利用重载的“-”运算符,求所有学生中年龄相差最大的两个人的名字以及相差天数。
输入
第一行:输入所需要输入的学生个数;
第二行开始,依次输入每个学生的姓名、出生年、月、日。 输出 输出年龄相差最大的两个人的名字以及相差天数。
样例输入
3
Tom 1995 1 1
Joe 1995 2 28
Jimmy 1996 1 8
样例输出
Tom和Jimmy年龄相差最大,为372天。

#include<iostream>
#include <cstring>
using namespace std;
class Date {
	private:
		int year,month,day;
	public:
		Date() {
			year=1;
			month=1;
			day=1;
		}
		void set_Date(int y,int m,int d) {
			year=y;
			month=m;
			day=d;
		}
		int compare(Date &a) {
			int temp1=0,temp2=0;  	//两个日期当年的天数 
			int m[12]= {31,29,31,30,31,30,31,31,30,31,30,31};
			int n[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
			if((year%4==0&&year%100!=0)||(year%400==0)) {		//判断闰年 
				for(int i=0; i<month-1; i++) {
					temp1+=m[i];		
				}
				temp1+=day;
			} else {
				for(int i=0; i<month-1; i++) {
					temp1+=n[i];
				}
				temp1+=day;
			}
			if((a.year%4==0&&a.year%100!=0)||(a.year%400==0)) {		//判断闰年 
				for(int i=0; i<a.month-1; i++) {
					temp2+=m[i];
				}
				temp2+=a.day;
			} else {
				for(int i=0; i<a.month-1; i++) {	
					temp2+=n[i];
				}
				temp2+=a.day;
			}
			if(year==a.year) {		//年份相同直接相减 
				temp1-=temp2;
				if(temp1<0) {
					temp1=-temp1;
				}
				return temp1;		//返回一个正数 
			} else {
				int max,min;
				if(year>a.year) {	//判断谁的年份比较大 
					max=year;
					min=a.year;
					while(max-min) {		//依次把相差年份的天数相加 
						if((min%4==0&&min%100!=0)||(min%400==0)) {
							temp1+=366;		
						} else {
							temp1+=365;		
						}
						min++;
					}
					return temp1-temp2;		
				} else {			//原理同上 
					max=a.year;
					min=year;
					while(max-min) {
						if((min%4==0&&min%100!=0)||(min%400==0)) {
							temp2+=366;
						} else {
							temp2+=365;
						}
						min++;
					}
					return temp2-temp1;
				}
			}
		}
};
class Student {
	private:
		char *name;
		Date birthday;
	public:
		Student() {
		}
		void set(char *n,int y,int m,int d) {
			birthday.set_Date(y,m,d);
			name=new char[strlen(n)+1];
			strcpy(name,n);
		}
		int operator-(Student &s) {
			int temp;
			temp=birthday.compare(s.birthday);
			return temp;
		}
		void print_name() {
			cout<<name;
		}
		virtual ~Student() {
			if(name!=NULL)
				delete name;
		}
};
int main() {
	int t,y,m,d,day=0,max=0,s1=0,s2=1;
	char name[20];
	Student *s;
	cin>>t;
	s=new Student[t];
	for(int i=0; i<t; i++) {
		cin>>name>>y>>m>>d;
		s[i].set(name,y,m,d);
	}
	for(int i=0; i<t-1; i++)
		for(int j=1; j<t; j++) {
			day=s[i]-s[j];
			if(day>max) {
				max=day;
				s1=i;
				s2=j;
			}
		}
	s[s1].print_name();
	cout<<"和";
	s[s2].print_name();
	cout<<"年龄相差最大,为"<<max<<"天。";
}

注意

在主函数new了一个Student s[t]数组,最后不需要delete s,已经在类里面写了析构函数,系统会自动调用析构函数
在这里插入图片描述

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值