//模块化程序设计的思想
//设计一个年月日的程序,实现输入年月日,输出是这一年的第几天。
//首先考虑的是否为闰年,然后考虑这是哪一月的哪一天。
//最后考虑是总共的天数
#include"pch.h"
#include<stdio.h>
#include<iostream>
using namespace std;
/*bool Is_LeapYear(int year)//判断是否为闰年,用bool类型。返回True还是False
{
if ((year % 4 == 0 & year % 100 == 0) ||( year % 400 == 0))
return true;
else
return false;
}
*/
bool Is_LeapYear(int year)//另一种写法
{
return ((year % 4 == 0 & year % 100 == 0) || (year % 400 == 0));
}
// 31 28 31 30 31 30 31 31 30 31 30 31
// 1 2 3 4 5 6 7 8 9 10 11 12
/*int GetMon_Day(int year, int mon)//希望返回这一年这一月有多少天
{
int day = 0;
switch (mon)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
day = 31; break;
case 4:case 6:case 9:case 11:
day = 30; break;
default:
if (Is_LeapYear(year))
day = 29;
else
day = 28;
}
return day;
}*/
int GetMon_Day(int year, int mon)//另一种写法:查表法
{
int arr[13] = { 29,31,30,31,30,31,30,31,31,30,31,30,31 };
if (mon == 2 && Is_LeapYear(year))
{
mon = 0;
}
return arr[mon];
}
int GetYear_Mon_Day_Total(int year, int mon, int day)
{
int m = 1;
int total = day;
for (; m < mon; m++)
{
total = total + GetMon_Day(year, m);
}
return total;
}
int main()
{
int year, mon, day;
int total = 0;
cin>>year>>mon>>day;
total = GetYear_Mon_Day_Total(year, mon, day);
printf("%d年 %d月 %d日 => 总共%d", year, mon, day, total);
return 0;
}