原题目:某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
以下是程序源代码,初学者可能在很多地方不是很完善。
#include<stdio.h>
struct fish
{
};
int year;
int month;
int day;
int i;//闰年
int j = 0;//闰年个数
int k = 0;//平年个数
int y_s, m_s;//年月差
int t = 0;//总日差
int r = 0;//判断打渔晒网
//判断年份
int judge()
{
if (year < 2010)//判断年分输入是否正确
{
printf_s("year error!\n");
return 0;
}
else
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判断闰年
{
i = 1;
}
else
{
i = 0;
}
}
return 1;
}
//计算年份
void count()
{
j = (year - 2010) / 4;//计算闰年个数
k = (year - 2010) - j;//计算平年个数
y_s = 366 * j + 365 * k;//计算年份差
}
//判断月份差
int j_m()
{
judge();
if (i = 1)
{
switch (month)//计算月份差
{
case 1:
m_s = 0;
break;
case 2:
m_s = 31;
break;
case 3:
m_s = 59;
break;
case 4:
m_s = 90;
break;
case 5:
m_s = 120;
break;
case 6:
m_s = 151;
break;
case 7:
m_s = 181;
break;
case 8:
m_s = 212;
break;
case 9:
m_s = 243;
break;
case 10:
m_s = 273;
break;
case 11:
m_s = 304;
break;
case 12:
m_s = 334;
break;
default:
printf_s("month error!\n");
return 0;
break;
}
}
if (i = 0)
{
switch (month)//计算月份差
{
case 1:
m_s = 0;
break;
case 2:
m_s = 31;
break;
case 3:
m_s = 58;
break;
case 4:
m_s = 89;
break;
case 5:
m_s = 119;
break;
case 6:
m_s = 150;
break;
case 7:
m_s = 180;
break;
case 8:
m_s = 211;
break;
case 9:
m_s = 242;
break;
case 10:
m_s = 272;
break;
case 11:
m_s = 303;
break;
case 12:
m_s = 333;
break;
default:
printf_s("month error!\n");
return 0;
break;
}
}
return 1;
}
//判断打渔晒网
void reckon()
{
t = y_s + m_s + day;
r = t % 5;
switch (r)//判断打渔晒网
{
case 0:
printf_s("今天晒网\n");
break;
case 1:
case 2:
case 3:
printf_s("今天打渔\n");
break;
case 4:
printf_s("今天晒网\n");
break;
}
}
//判断日子
void j_d()
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
{
if (day<1||day>31)
{
printf_s("day error!\n");
}
else
{
reckon();
}
}
else
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day<1 || day>30)
{
printf_s("day error!\n");
}
else
{
reckon();
}
}
else
{
if (i=1)
{
if (day<1 || day>29)
{
printf_s("day error!\n");
}
else
{
reckon();
}
}
else
{
if (day<1 || day>28)
{
printf_s("day error!\n");
}
else
{
reckon();
}
}
}
}
}
//主函数
int main()
{
int choose=1;
while (choose)
{
printf_s("请输入一个日期(在2010.1.1以后的日期)并以英文逗号隔开:\n");
scanf_s("%d,%d,%d", &year, &month, &day);
if (judge())
{
if (j_m())
{
j_d();
}
}
printf_s("继续请输入1,结束请输入0:\n");
scanf_s("%d",&choose);
}
return 0;
}
最终效果图: