三天打鱼两天晒网

题目背景

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

基本要求及提高要求

基本要求:
1.程序风格良好(使用自定义注释模板),提供友好的输入输出。
提高要求:
1.输入数据的正确性验证。
2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在 in.txt文件中,程序读入in.dat文件进行判定,并将结果输出至 out.txt文件。

代码编写语言

C++

代码设计思想

1.建立主类,初始化数据元素
2.通过Deal_key函数实现日期的输入与正确性判断
3.函数sumdays()实现输入日期到2010-1-1的天数计算
4.display()函数判断变量days_5的取值来确定工作状态
5.file()函数通过文件流操作实现文件内数值的读入及处理

具体功能实现(代码片段)

1.数据输入及判断正误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int Date_fish::Deal_key()
{
cout<<"请输入年"<<endl;
cin>>year;
//判断输入的年,月,日是否在规定的范围
if (year<2010||year>2019) //判断年
{
cout<<"年份输入错误,请输入2010-2019范围的年份(包含2010和2019)"<<endl;

cin>>year;
}
cout<<"请输入月"<<endl;
cin >>month;
if(month<=0||month>12) //判断月
{
cout<<"月份输入错误,请输入1-12范围的月份(包含1和12)"<<endl;
cin>>month;
}


cout<<"请输入日"<<endl;
cin>>day;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//判断日
{
if(day>31||day<=0)//判断大月
{cout<<"天数输入错误,请输入改后的正常天数"<<endl;
cin>>day;
}
}
else if(month!=2)//判断小月
if(day>30||day<=0)
{{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;}
}
else if(((year%4==0&&year%100!=0)||year%400==0))//判断闰年2月
{
if(day>29||day<=0)
{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;
}

}
else
{
if(day>28||day<=0)
{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;}
return 0;
}
}
2.距离指定日期的天数计算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void Date_fish::sumdays() //判断平闰年函数并计算天数
{
int YEAR_0=2010; //定义初始变量YEAR_0为2010
for (YEAR_0;YEAR_0<year;YEAR_0++) //距当前年份几年,循环几次
{
if((year%400==0)||(year%4==0&&year%100!=0)) //当前年份为闰年,||前为世纪年判断,||后为普通年
{
sum+=366;//闰年,总天数加366天
}
else//为平年
{
sum+=365; //平年,总天数加365天
}
}
for(int i=0;i<month-1;i++)
{
if((year%400==0)||(year%4==0&&year%100!=0))
sum+=special_month_day[i]; //加当前月份的天数
else
sum+=normal_month_day[i];
}
sum+=day; //总天数
cout<<"到2010-01-01的总天数为:";
cout<<sum<<endl;

days_5=sum%5;//除5取余
sum=0;//初始化
//return days_5;
}
3.输入日期工作状态判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Date_fish::display()
{

if (days_5>=1&&days_5<4)//余数1,2,3
{
//余数1,2,3打鱼
cout<<"这天在打鱼"<<endl;
}
else
{
//晒网
cout<<"这天在晒网"<<endl;
}
//return days_5;
}
4.文件操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int Date_fish::file()

{int a[3];//数组实现txt文档中三个值的传回
ifstream infile("in.txt",ios::in);
if(!infile)
{cerr<<"open in.txt error!"<<endl;
exit(1);
}
infile>>a[0]>>a[1]>>a[2];
year=a[0]; month=a[1]; day=a[2];

sumdays();//接收计算后的days_5
//
ofstream outfile("out.txt",ios::out);

{if (days_5>0&&days_5<4)
{
outfile<<year<<"."<<month<<"."<<day<<"该天打鱼"<<endl; //余数为1或2或3,将打鱼的信息写入out文件中
}
else
{
outfile<<year<<"."<<month<<"."<<day<<"该天晒网"<<endl;//余数为0或4,将晒网的信息写入out文件中

}
}
//关闭文件
infile.close();
outfile.close();
return 0;
}

**文件操作解释说明

为方便使用.txt文档中保存的输入数据,分别将“year”,“month”,“day”放在一个数组中,以空格隔开,达到为程序写入数值的功能。

详细代码(可编译执行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*~
#@**xi'an university of science & technology**
#@Software engineering 1703
#@17408070828
~*/

#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Date_fish{
public :
int year; //年
int month; //月
int day; //日
Date_fish();//构造函数
int file(); //对文件中的信息进行处理
int Deal_key();//对从键盘输入的信息进行处理
int Choose;////键盘读入或文件读入的选择标志
void sumdays();//判断闰年平年
void display();//工作类型
int days_5; //除5的余数
int sum; //距2010年1月1日的总天数
int normal_month_day[12]; //平年每月的天数
int special_month_day[12];
};
Date_fish::Date_fish () //构造函数初始化
{
Choose=0; //键盘读入或文件读入的选择标志
sum=0;
days_5=0;
year=0;
month=0;
day=0;
int normal_month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int special_month_day[12]={31,29,31,30,31,30,31,31,30,31,30,31};//初始平闰年各月份天数

}
int Date_fish::Deal_key()
{
cout<<"请输入年"<<endl;
cin>>year;
//判断输入的年,月,日是否在规定的范围
if (year<2010||year>2019) //判断年
{
cout<<"年份输入错误,请输入2010-2019范围的年份(包含2010和2019)"<<endl;

cin>>year;
}
cout<<"请输入月"<<endl;
cin >>month;
if(month<=0||month>12) //判断月
{
cout<<"月份输入错误,请输入1-12范围的月份(包含1和12)"<<endl;
cin>>month;
}


cout<<"请输入日"<<endl;
cin>>day;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//判断日

{
if(day>31||day<=0)//判断大月
{cout<<"天数输入错误,请输入改后的正常天数"<<endl;
cin>>day;
}

}
else if(month!=2)//判断小月
if(day>30||day<=0)
{{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;}


}
else if(((year%4==0&&year%100!=0)||year%400==0))//判断闰年2月
{
if(day>29||day<=0)
{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;
}

}
else
{
if(day>28||day<=0)
{cout<<"您输入的日期有误,请重新输入"<<endl;
cin>>day;}
return 0;
}
}
void Date_fish::sumdays() //判断平闰年函数并计算天数
{
int YEAR_0=2010; //定义初始变量YEAR_0为2010
for (YEAR_0;YEAR_0<year;YEAR_0++) //距当前年份几年,循环几次
{
if((year%400==0)||(year%4==0&&year%100!=0)) //当前年份为闰年,||前为世纪年判断,||后为普通年
{
sum+=366;//闰年,总天数加366天
}
else//为平年
{
sum+=365; //平年,总天数加365天
}
}
for(int i=0;i<month-1;i++)
{
if((year%400==0)||(year%4==0&&year%100!=0))
sum+=special_month_day[i]; //加当前月份的天数
else
sum+=normal_month_day[i];
}
sum+=day; //总天数
cout<<"到2010-01-01的总天数为:";
cout<<sum<<endl;

days_5=sum%5;//除5取余
sum=0;//初始化
//return days_5;
}


void Date_fish::display()
{

if (days_5>=1&&days_5<4)//余数1,2,3
{
//余数1,2,3打鱼
cout<<"这天在打鱼"<<endl;
}
else
{
//晒网
cout<<"这天在晒网"<<endl;
}
//return days_5;
}

int Date_fish::file()

{int a[3];//数组实现txt文档中三个值的传回
ifstream infile("in.txt",ios::in);
if(!infile)
{cerr<<"open in.txt error!"<<endl;
exit(1);
}
infile>>a[0]>>a[1]>>a[2];
year=a[0]; month=a[1]; day=a[2];

sumdays();//接收计算后的days_5
//
ofstream outfile("out.txt",ios::out);

{if (days_5>0&&days_5<4)
{
outfile<<year<<"."<<month<<"."<<day<<"该天打鱼"<<endl; //余数为1或2或3,将打鱼的信息写入out文件中
}
else
{
outfile<<year<<"."<<month<<"."<<day<<"该天晒网"<<endl;//余数为0或4,将晒网的信息写入out文件中

}
}
//关闭文件
infile.close();
outfile.close();
return 0;
}
int main()
{
Date_fish m;
cout<<"请选择日期读取方式:"<<endl;
cout<<"*******0为从键盘读取*******"<<endl;
cout<<"*******1为从文件读取*******"<<endl;
cin>>m.Choose;

//
switch(m.Choose)
{
case 0:
{m.Deal_key();
m.sumdays();
m.display();}
break;
case 1:
m.file();
break;
}
return 0;
}
/*-----------
auther @却水*/

个人总结

在完成本次题目时个人存在的问题
1.编程基础较差,程序遇到bug解决能力较弱 ,解决时间较长
2.代码风格较乱,还需改进
3.设计算法较为繁琐,判断日期时天的判断if—else语句繁琐,刚开始的算法较简洁但是出现计算时间错误,无奈之下只能改为if嵌套语句
4.c++语法不够熟练,尤其文件操作生疏,导致文件操作方面花费了大量的时间
5.因理解题目要求不到位,在文件的操作方面走了很多弯路

在本次完成题目时的收获
1.重温C++语法
2.学习了以前忽略的C语言文件操作
3.通过不断调试错误,对错误的出现有了一些新的认识,如,顺序结构影响程序执行的逻辑,嵌套语句的使用不当导致程序异常
4.做出成果增强了学习其他语言的信心及决心

流程图

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2020 卻水
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信