三天打鱼两天晒网

一、题目名称“三天打鱼两天晒网”

二、题目内容:  中国有句俗语叫“三天打鱼两天晒网”。某人从201011日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。用CC++语言实现程序解决问题。

三、算法设计:

1.输入指令选择1.手动输入/2.文件输入

2.如果选择1则直接输入当前日期的年、月、日。如果选择2请提前在文件中输入日期

3.检验当前日期是否合法。

3.从2010年开始遍历

3.1若当前年份小于输入年份

    3.1.1若当前年份为闰年,则总天数累加366

    3.1.2若当前年份不是闰年,则总天数累加365

5.计算输入的月份之前每个月的天数,并累加到总天数中

5.将输入的日期累加到总天数中

6.将总天数对5取模,得到余数

7.若余数为1、2、3时,则当天打鱼,否则为晒网。

流程图:

三天打鱼两天晒网

 

代码:

 1 #include<iostream>
 2 #include<fstream>
 3 using namespace std;
 4 class time
 5 { public:
 6     void input();
 7     int days();
 8     void output();
 9     void file();
10     int year,month,day;
11     int sum = 0;
12     int y = 2010;
13     int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
14 };
15 void time::input()//输入日期
16 {
17     cout<<"请输入年月日中间用空格隔开"<<endl;
18     cin>>year>>month>>day;
19     if ((year < 2010) || (month < 1 || month>12) || (month == 2 && (year % 4 != 0 || year % 400 != 0) && day >= 29) || (day > a[month]))
20         {    
21             cout << "输入日期错误,请重新输入" << endl;
22             cout << "请按照年月日的顺序重新输入日期,中间用空格隔开n" << endl;
23             cin >> year >> month >> day;
24         }
25 }
26 int time::days()//计算总天数
27 {
28     for (y;y< year;y++)//用此循环加上相差年份所隔的天数。 
29     {
30         if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))//判断此年份是否是闰年 
31         {
32             sum += 366;
33         }//是闰年,则总天数加上366. 
34         else
35         {
36             sum += 365;
37         }//是平年,总天数加上365 
38     }
39     for (int m = 1; m < month; m++)//用此循环加上所隔月份的总天数 
40     {
41         sum += a[m];
42         if (month == 2 && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
43         {
44             sum += 1;
45         }//判断月份是否为闰年2月,若是2月,则总天数加一。
46     }
47     sum += day;
48     return sum;
49 }
50 void time::output()//输出结果
51 {
52     if ((sum % 5 == 1) || (sum % 5 == 2) || (sum % 5 == 3))//用相隔总天数除以周期5,若余数为1,2,3,则此人打鱼。 
53         cout << "这个人在"<<year<<""<<month<<""<<day<<""<<"这天打鱼" << endl;
54         else//否则,这个人晒网
55         cout <<"这个人在"<<year <<""<< month<<""<<day<<""<<"这天晒网"<<endl;
56 }
57 void time::file()
58 {
59         ifstream infile;
60         ofstream outfile;
61         infile.open("in.txt");
62         outfile.open("out.txt");
63         while (!infile.eof())//当in.txt文件读取完时结束
64         { 
65         infile >>year>> month >> day;//读取文件中的数据
66         days();
67         if ((sum % 5 == 1) || (sum % 5 == 2) || (sum % 5 == 3))//用相隔总天数除以周期5,若余数为1,2,3,则此人打鱼。 
68             outfile << "这个人在" << year << "" << month << "" << day << "" << "这天打鱼" << endl;//向文件写入结果
69         else//否则,这个人晒网
70             outfile << "这个人在" << year << "" << month << "" << day << "" << "这天晒网" << endl;
71         // 关闭打开的文件
72         }
73         outfile.close();
74         infile.close();
75         cout << "请到out.txt中查看结果" << endl;
76 }
77 void main()
78 { while (1)
79     {    time t;
80         int a;
81         cout <<endl<<  "请选择:" << endl;
82         cout << "1、自行输入"<< endl;
83         cout << "2、文件输入"<< endl;
84         cin>>a;
85         switch(a)
86         {
87         case 1:
88             t.input();
89             t.days();
90             t.output();
91             cout << endl;
92             break;
93         case 2:
94             t.file();
95             break;
96         }
97         system("pause");
98     }
99 }

                                                                                                                                                                                                                                                                                                                                                                2020.3.5 任兵齐

 

 

 

 

原文链接: https://www.cnblogs.com/rbqnb/p/12417764.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    三天打鱼两天晒网

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/371431

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月3日 上午10:46
下一篇 2023年3月3日 上午10:46

相关推荐