C语言编程题,求助大神
#include <iostream>
#include <time.h>
unsigned int CalcDayOfMonth(unsigned int year, unsigned int month);
bool IsLeapYear(unsigned int year);
struct Time
{
unsigned int year;
unsigned int month;
unsigned int day;
void setTime(unsigned int y = 1900, unsigned int m = 1, unsigned int d= 1)
{
year = y; month = m;
if (d > CalcDayOfMonth(year, month))
day = CalcDayOfMonth(year, month);
else
day = d;
}
Time operator-(Time& t)
{
Time tmp;
if (t > * this)
{
tmp = t;
t = *this;
*this = tmp;
}
else
tmp = *this;
if (tmp.day >= t.day)
{
tmp.day -= t.day;
}
else
{
tmp.day = CalcDayOfMonth(tmp.year, tmp.month-1) + tmp.day-t.day;
tmp.month--;
}
if (tmp.month >= t.month)
tmp.month -= t.month;
else
{
tmp.month = tmp.month + 12 - t.month;
tmp.year--;
}
tmp.year -= t.year;
Time tmp1 = t;
int d=0;
while (!(*this==tmp1))
{
tmp1.day++;
d++;
if (CalcDayOfMonth(tmp1.year, tmp1.month) < tmp1.day)
{
tmp1.day = 1;
tmp1.month++;
}
if (tmp1.month > 12)
{
tmp1.month = 1;
tmp1.year++;
}
}
printf("累计相差:%d天\n", d);
return tmp;
}
bool operator==(Time& t)
{
bool b=this->day == t.day && this->month == t.month && this->year == t.year;
return b;
}
bool operator>(Time& t)
{
if (this->year > t.year)
return true;
else if (this->year == t.year)
{
if (this->month > t.month)
return true;
else if (this->month == t.month)
{
if (this->day > t.day)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
};
void show(Time t)
{
printf_s("%d年-%u月-%u日\n", t.year, t.month, t.day);
}
bool IsLeapYear(unsigned int year)
{
if ((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0))
return true;
return false;
}
unsigned int CalcDayOfMonth(unsigned int year, unsigned int month)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return IsLeapYear(year) ? 29 : 28;
}
return 0;//返回0则表示月份输入不正确
}
int main()
{
Time t1, t2;
printf_s("请输入两个时间(格式:年 月 日):\n");
scanf_s("%u %u %u", &t1.year, &t1.month, &t1.day);
scanf_s("%u %u %u", &t2.year, &t2.month, &t2.day);
show(t1 - t2);
}