三天打鱼两天晒网C语言编程
题目:中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
#include <stdio.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
//判断是否为闰年
int leapyear (int year)
{
if(year%400==0 || (year %4 == 0&&year%100!=0))
return 1;
else
return 0;
}
//计算总天数
int countDay (DATE currentDay) {
int perMonth[12]={0,31,28,31,30,31,30,31,31,30,31,30}; //每月天数
int totalDay=0,year,i;
//求出指定日期之前的每一年的天数累加和
for(year=2010; year<currentDay.year; year++) {
if(leapyear (year)) //判断是否为闰年
totalDay=totalDay+366;
else
totalDay=totalDay+365;
}
if(leapyear(currentDay.year))
perMonth[2]+=1;//如果为闰年,二月份为29天
for(i=0; i<currentDay.month; i++)
totalDay+=perMonth[i];//当年的天数
totalDay+=currentDay.day;//加上本月日期
return totalDay;
}
int main(void)
{
DATE today; //指定日期
int totalDay; //指定日期距离2010年1月1日的天数
int result; //totalDay对5取余的结果
int error;
do { //输入指定日期
printf(“请输入年/月/日!:\n”);
scanf("%d/%d/%d",
&today.year, &today.month, &today.day);
printf("\n");
error=0;
if(today.year<2010)
//判断日期格式是否正确
{printf(“年份应大于2010!\n”);error=1;}
if(today.month<1||today.month>12)
{printf(“月份输入错误!\n”);error=1;}
if(today.day<0||today.day>31)
{printf(“日期输入错误!\n”);error=1;}
printf("***********************************************************************\n");
}
while(error);
totalDay=countDay(today); //求出指定日期距离2010年1月1日的天数
result=totalDay%5; //天数%5,判断输出打鱼还是晒网
if(result>0 && result<4)
printf(“打鱼!\n”);
else
printf(“晒网!\n”);
printf("***********************************************************************\n");
return 0;
}