概述
算法设计与问题分析
计算从20100101日起开始"三天打鱼两天晒网",求出在以后的某一天是"打鱼"or"晒网"
即1.计算从20100101日起至指定日期的天数,分别求从2010至指定日期前一年的天数(day_year)+从该年一月到前一月的天数(day_month)+该月指定日期的天数(today.day)=相差日期(diff);
2,用相差日期(diff)%5计算余数判断该天是"打鱼"还是"晒网"
流程图
源程序:
#include #include struct Date
{
int year;
int month;
int day;
};
int DateDiff(FILE* in,FILE* out)
{
struct Date today,term;
term.month = 12;
term.day =31;
long date;
while( fscanf(in,"%ld",&date) != EOF)
{
/*针对读in.txt文件做出测试
printf("%ldn",date);
*/
int diff,day_year,day_month=0;//sum为由20100101至指定日期的总天数
int leap;
int n = 0; //由20100101至指定日期共经过多少个闰年
static int mon[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//每月的天数
fscanf(in,"%d",&date);
today.year = date/10000; //将输入日期年,月,日分开
today.month = date/100-today.year*100;
today.day = date%100;
/*对输入的日期年月日分开后的数据进行测试
printf("%d年%d月%d日n",today.year,today.month,today.day);*/
if(today.year < 2010 || today.month < 1 || today.month > 12) //判断输入的年份和月份是否合理
{
fprintf(out,"输入的日期不合理");
return 0;
}
leap = ( today.year % 4 == 0 && today.year % 100 != 0 )|| today.year % 400 ; //判断输入年份是否为闰年
if(today.day < 1 || (today.day > mon[today.month] && today.month != 2)||(today.month == 2 && today.day > mon[today.month] + leap)) //判断输入日期是否符合当月天数
{
fprintf(out,"输入的日期不合理");
return 0;
}
for(int i = 2010;i < today.year;i++)
{
if(( i % 4 == 0 && i % 100 != 0 )|| i % 400 ==0) //计算由2010年至指定日期经过多少个闰年
{
n++;
}
}
day_year = (today.year - 2010) * 365 + n;
if(leap == 1)
{
for(int j = 0;j < today.month ; j++)
{
day_month += mon[j];
}
if(today.month > 2) //若输入年份为闰年且输入月份大于二计算天数加一
{
day_month += 1;
}
}
else
{
for(int k = 0;k < today.month;k++)
{
day_month += mon[k];
}
}
diff = day_year + day_month + today.day;
switch(diff % 5) //总天数除以5,若余数为1,2,3则为打鱼,其余为晒网
{
case 0:fprintf(out,"晒网 "); break;
case 1:fprintf(out,"打鱼 "); break;
case 2:fprintf(out,"打鱼 "); break;
case 3:fprintf(out,"打鱼 "); break;
case 4:fprintf(out,"晒网 "); break;
}
}
return 0;
}
int main()
{
FILE* in = fopen("in.txt","r");
/*针对文件是否打开且打开是否为空做出测试
if (in != NULL)
{
printf("There are date!n");
}
else
{
printf("no date!n");
}*/
FILE* out = fopen("out.txt","w");
DateDiff(in,out);
fclose(in);
fclose(out);
return 0;
}
测试代码:
测试一:针对文件是否打开且打开是否为空做测试
测试二:针对读in.txt文件做出测试
测试三:对输入日期年月日分开后的数据测试
最后
以上就是笨笨山水为你收集整理的三天打鱼两天晒网的c语言程序,c语言“三天打鱼两天晒网”的全部内容,希望文章能够帮你解决三天打鱼两天晒网的c语言程序,c语言“三天打鱼两天晒网”所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复