三天打鱼,两天晒网
问题描述
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
问题分析:
用in.txt文件进行数据的输入,并把结果输出至out.txt文件
源代码:
**//某人从2010年1月1日开始“三天打鱼,两天晒网”,问这个人在以后的某一天是“打鱼”还是“晒网”
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int year,month,day; //定义年月日
int totalday=0; //总天数
int i=0; //闰年多余的天数
int mon[13]={0,31,28,31,28,31,30,31,31,30,31,30,31};
ifstream fin;
fin.open("in.txt"); //打开in.txt文件,读取年月日
fin>>year>>month>>day;
if(!fin.is_open())
{
cerr<<"读取数据错误!"<<endl;
exit(0);
}
ofstream fout;
fout.open("out.txt"); //打开out.txt文件,向里边输入结果
if(!fout.is_open())
{
cerr<<"打开文件错误"<<endl;
exit(0);
}
if(year%400==0||(year%4==0&&year%100!=0)) //判断闰年
{
mon[2]=29;
}
if(year<2010) //判断输入数据是否正确
{cout<<"输入年份过小,请重新输入"<<endl;
fout<<"输入年份过小,请重新输入"<<endl;
exit(0);}
if(month<1||month>12)
{cout<<"输入月份错误,请重新输入"<<endl;
fout<<"输入月份错误,请重新输入"<<endl;
exit(0);}
if(day<1||day>mon[month])
{cout<<"日期输入错误,请重新输入"<<endl;
fout<<"日期输入错误,请重新输入"<<endl;
exit(0);}
for(int a=2010;a<year;a++) //计算总天数
{
if(a%400==0||(a%4==0&&a%100!=0))
i+=1;
}
totalday=(year-2010)*365+i;
for(int a=0;a<month;a++)
{
totalday+=mon[a];
}
totalday+=day;
if(totalday%5==1||totalday%5==2||totalday%5==3) //判断“打鱼”还是“晒网”
{cout<<"渔夫这天打鱼!"<<endl;
fout<<"渔夫这天打鱼!";}
else
{cout<<"渔夫这天晒网!"<<endl;
fout<<"渔夫这天晒网!";}
fout.close();
fin.close();
return 0;
}**