中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。用C或C++语言/java/python实现程序解决问题。

基本要求:1.程序风格良好(使用自定义注释模板),提供友好的输入输出。

提高要求:1.输入数据的正确性验证。
2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行判定,并将结果输出至out.txt文件。
解决这个问题考虑以下三点:
1)计算从2010年1月1日开始至指定日期共有多少天;
2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;
3)根据余数判断他是在“打鱼”还是在“晒网”;若余数为1,2,3,则他是在“打鱼”否则 是在“晒在这里插入代码片网”
其中注意一下算天数时考虑闰年闰月,即年份能被4整除但不能被100整除或能被400整除。代码如下
#include
#include
using namespace std;
struct time { //定义结构体,包含年月日
int year;
int month;
int day;
}date;
void judge(struct time);//判断函数
int days(struct time);//天数计算函数
void file_test(); //文件数据处理
//void judge_date(struct time);
void main()
{
cout << “****三天打渔两天晒网\n”;
cout << " *明天干啥呢?\n";
printf(“输入2010年以后的任意日期,格式如下:\nxx(年) xx(月) xx(日)\n”);
cin >> date.year >> date.month >> date.day;
while (date.year < 2010 || date.month>12 || date.day > 31)//大致判断年月日是否合格
{
cout << “输入错误,重新输入:\n”;
cin >> date.year >> date.month >> date.day;
}
if (((date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0) && date.day > 28) && date.month == 2)//当为闰年闰月是判断
{ //天数是否合格
cout << “该月为闰月!重输\n”;
cin >> date.year >> date.month >> date.day;
}
days(date);
judge(date);
cout << “以下为文件测试结果:\n”;
file_test();
system(“pause”);
}
//void judge_date(struct time date)
//{

// if (date.year < 2010)
// cout << “年份小于2010!重输:\n”;
// cin >> date.year;
// if (date.month > 12)
// cout << “月份错误!重输:\n”;
// cin >> date.month;
// if (((date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0) && date.day > 28)&&date.month==2)
// cout << “该月为闰月!重输”;
// cin >> date.day;

//}
int days(struct time date)//天数计算函数
{
int i;
int m_d1[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int m_d2[] = {31,29,31,30,31,30,31,31,30,31,30,31 };
int Days = 0;
int days = 0;
int flag=(date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0);//根据是否是闰年选择相应年份天数及具体月份的天数
for (i = 2010;i < date.year;i++)
{
if(i%40&&i%100!=0||(i%4000))
Days += 365;
else Days += 366;
}
for ( i= 1;i < date.month;i++)
{

	if(flag)
	days += m_d1[i-1];
	else days += m_d2[i-1];
}
Days += (days+=date.day);
return Days;

}
void judge(struct time date)//结果判断函数并输出
{
if (days(date) % 5 < 4&& days(date) %5>0)
cout << date.year << “年” << date.month << “月” << date.day << “日:” << endl << “打渔”<<endl;
else cout << date.year << “年” << date.month << “月” << date.day << “日:” << endl << “晒网”<<endl;
}
void file_test()//文件数据检测函数
{
ifstream f1;
ofstream f2;
f1.open(“in.txt”);
f2.open(“out.txt”);
f1 >> date.year;//读取文件数据,存入结构体
f1 >> date.month;
f1 >> date.day;
while (date.year != 1)//读到year等于1结束
{
days(date);
if (days(date) % 5 < 4 && days(date) % 5 > 0)//判断闰年
{
f2 << date.year << “年” << date.month << “月” << date.day <<“日:”<< “打渔” << endl;
cout<< date.year << “年” << date.month << “月” << date.day << “日:” << “打渔” << endl;
}
else
{
f2 << date.year << “年” << date.month << “月” << date.day << “日:” << “晒网” << endl;
cout << date.year << “年” << date.month << “月” << date.day << “日:” << “晒网” << endl;
}
f1 >> date.year;
f1 >> date.month;
f1 >> date.day;
}
}
流程图如下:
“三天打渔,两天晒网”

相关文章: