2021年1月10日是2021年的第10天
2021年2月10日是2021年的第31+10=41天
2021年3月10日是2021年的第31+28+10=69天
闰年和平年2月份的天数不一样
每一月的天数都有区别
输入的年月日也得满足实际情况
不满足条件,可以使用return -1;结束整个程序
源程序:
#include <stdio.h>
int main()
{
int year, month, day;
int mark, i;
int m_count[12], d_count = 0;
m_count[0] = 31;
m_count[2] = 31;
m_count[3] = 30;
m_count[4] = 31;
m_count[5] = 30;
m_count[6] = 31;
m_count[7] = 31;
m_count[8] = 30;
m_count[9] = 31;
m_count[10] = 30;
m_count[11] = 31;
printf("Please input data(xxxx xx xx):\n");
scanf("%d %d %d", &year, &month, &day);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //判断是否为闰年
{
mark = 1;
m_count[1] = 29;
}
else
{
mark = 0;
m_count[1] = 28;
}
if (year < 0 || month < 1 || month > 12 || day < 1 || day > 31) //判断输入的数据是否符合实际
{
printf("ERROR!\n");
return -1;
}
if (mark == 1 && month == 2 && day >29)
{
printf("ERROR!\n");
return -1;
}
if (mark == 0 && month == 2 && day >28)
{
printf("ERROR!\n");
return -1;
}
for (i = 0; i < month - 1; i++) //先把前面的月份天数相加
{
d_count += m_count[i];
}
d_count += day; //最后加上当月的天数
printf("Total:%d\n", d_count);
return 0;
}
运行结果截图: