C语言,中国有句俗语:“三天打鱼两天晒网”,某人从1990年1月1日起开始“三天打鱼两天晒网”。问这个人在以后的某一天是在“打鱼”还是在“晒网”?
#include<stdio.h>
long y_tianshu(int y);
int n_tianshu(int y, int n);
int T(int y, int n,int d);
long y_tianshu(int y)//计算当年之前的天数
{
long a=0;
if (y == 1990) a = 0;
else if (y == 1991) a = 365;
else if (y == 1992) a = 365 * 2;
else if (y == 1993) a = 365 * 2 + 366;
else if (y > 1993 && y < 1997)
a = 365 * 2 + 366 + (y - 1993) * 365;
else if (y >= 1997)
a = 365 * 2 + 366 + (y - 1993) * 365 + (y - 1993) / 4;
return a;
}
int n_tianshu(int y, int n)//计算在本年,当月之前的天数
{
int s; int b=0;
for (int a = 1; a <= n - 1; a++)
{
switch (a)//根据月份判断天数
{
case 1:s = 31; break; case 2:s = 28; break;
case 3:s = 31; break; case 4:s = 30; break;
case 5:s = 31; break; case 6:s = 30; break;
case 7:s = 31; break; case 8:s = 31; break;
case 9:s = 30; break; case 10:s = 31; break;
case 11:s = 30; break; case 12:s = 31; break;
}
if (a == 2)//判断闰年2月差异
{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
s = 29;
else
s = 28;
}
b = b + s;
}
return b;
}
int T(int y, int n,int d)//判断输入的月份的天数是否正确
{
int s=0;
switch (n)//根据月份判断天数
{
case 1:s = 31; break; case 2:s = 28; break;
case 3:s = 31; break; case 4:s = 30; break;
case 5:s = 31; break; case 6:s = 30; break;
case 7:s = 31; break; case 8:s = 31; break;
case 9:s = 30; break; case 10:s = 31; break;
case 11:s = 30; break; case 12:s = 31; break;
}
if (n == 2)//判断闰年2月差异
{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
s = 29;
else
s = 28;
}
if (d <= s) return 0;
else return 1;
}
void main()
{
int y, n, d;
while (1)
{
scanf_s("%d-%d-%d", &y, &n, &d);//输入时间
while (y <= 0 || n <= 0 || d <= 0 || y < 1990||T(y, n, d)||n>12)
{ //控制输入的数据为正,并且在1990之后
//控制相应月份对应所具有的相应天数
printf("input error!please re-input:\n");
scanf_s("%d-%d-%d", &y, &n, &d);
}
long a = y_tianshu(y) + n_tianshu(y, n) + d;
a = a % 5;
if (a <= 3&&a>0) printf("他在打鱼\n");//利用总日子除以5的余数判断
else printf("他在晒网\n");
}
}