题目:中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。用C或C++语言实现程序解决问题。
代码:
#include<stdio.h>
#include<stdlib.h>
int numleap=0; //闰年数
int i=0; //i为1是当前年份为闰年,为0是为非闰年
int j=0; //j为1时表示当前月份为闰年的2月
int jiday=0; //查询的月份为奇月
int ouday=0; //查询的月份为偶月
int date=0; //要查询的时间距2010年1月1日的天数
int year=0;
int month=0;
int day=0;
char * str[2]={"此人在该天晒网!","此人在该天打鱼!"}; //存储向文件out.txt文件输出的字符串
int flag1 = 0,flag2=0;
void main(){
void judgeyear(int a);
void judgemonth(int b);
void judgeday(int c);
printf("欢迎使用“三天打鱼两天晒网”系统!\n\n");
do{
//对输入年份的操作
printf("请输入要查询的年、月、日(格式:xxxx-xx-xx):\n");
scanf("%d-%d-%d",&year,&month,&day);
judgeyear(year);
judgemonth(month);
judgeday(day);
if(year||month||day==0){
flag2=1;
}else flag2=0;
}while(flag2==1);
//计算输入的日期距2010年1月1日的天数date
printf("date=%d\n",date);
int k = date%5;
if(k>3){
printf("此人在该天晒网!\n\n");
flag1 = 0;
}else{ printf("此人在该天打鱼!\n\n");
flag1 = 1;
}
//将输出的结果放在工程根目录的out.txt文件中
FILE * fp;
fp=fopen("out.txt","w");
if(flag1==1){
fputs(str[1],fp);
}else fputs(str[0],fp);
fclose(fp);
printf("已将结果写入out.txt文件!");
}
//对输入的年份进行操作
void judgeyear(int a){
if(a<2010){
printf("输入有误,请重新输入!\n");
}else
if(a<=2011){
numleap=0;
}else
{
if(a%4!=0){
numleap=(a-2012)/4+1;
i=0;
}else{
if(a%100!=0){
numleap=(a-2012)/4+1;
i=1;
}else{
if(a%400!=0){
numleap=(a-2012)/4+1;
i=0;
}else{
numleap=(a-2012)/4+1;
i=1;}
}
}
}
}
//对输入的月份进行操作
void judgemonth(int b){
bool z = (b>=0&&b<=12);
if(z){
if(b%2==0){
if(i==1){
if(b==2){
j=1;
}
}
ouday=1;
}else
jiday=1;
}else{
printf("输入有误,请重新输入!\n");
}
}
//对输入的日期进行操作
void judgeday(int c){
if(c>31||c<0){
printf("输入有误,请重新输入!\n");
}else{
int d = (month/2);
int f = !j;
if(jiday){
if(d>1){
int f = !j;
date = (year-2010-(numleap-i))*365+(numleap-i)*366+d*31+j*29+f*28+(d-1)*30;
}else
date = (year-2010-(numleap-i))*365+(numleap-i)*366+c;
}else
date = (year-2010-(numleap-i))*365+(numleap-i)*366+d*31+j*29+f*28+(d-1)*30;
}
}