用C/C++解决三天打鱼两天晒网问题

三天打鱼两天晒网问题

1.问题描述:
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒 网”。用C或C++语言/java/python实现程序解决问题。
基本要求:
1.程序风格良好(使用自定义注释模板),提供友好的输入输出。
提高要求:
1.输入数据的正确性验证。
2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行判定,并将结果 输出至out.txt文件。
2.使用软件
visual studio 2017
3.程序流程图:

在这里插入图片描述

4.问题分析及算法设计
计算指定日期与2010年1月1日相距的天数,记为r,再用r%5来得到此人正在打鱼还是筛网。
(1),r的计算:总的思路:r=年+月+日,即指定日期距2010年1月1日经过的年,月,日,具体算法见详细代码;
(2),判断日期是否合法;
(3),判断正在打鱼还是筛网:w=r%5;若w=1,2,3时,正在打鱼;
(4),测试日期写在in.txt中,将结果保存在out.txt中。

5.详细代码:

#include <iostream>
#include <fstream>//文件操作
using namespace std;
struct Date  //定义一个结构体来存储日期 
{
 	int year;
 	int month;
	 int day;
}D;
int Judge(int year)  //判断是否是闰年,是闰年返回1,否则返回0
{
	 int i, j;
	 i = year;
 	if (i % 4 == 0 && i % 100 != 0)
 		 j = 1;
	 else 	if (i % 400 == 0)
 			 j = 1;
 		else
			 j = 0;
 	return j;
}
int remainder(int year, int month, int day)//计算据2010年1月1日共多少天,赋值给r 
{
 	int r = 0;   //日期变量
 	int leapyear[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };   //闰年 
 	int commonyear[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };  //平年 
 	for (int i = year; i != 2010; i--)
 		 if (Judge(i) == 1)
  			 r += 366;
 		 else r += 365;
	 if (Judge(year) == 1)
 		 for (int j = 0; j < (month - 1); j++)
		   r = r + leapyear[j];
	 else for (int k = 0; k < (month - 1); k++)
		  r = r + commonyear[k];
	 r = r + day;
	 return(r);
}
int Islegal(int year,int month,int day)    //判断日期是否合法
{
	 if (year < 2010 || month>12) //若输入年份小于2010,则返回0 
		  return 0;
	if (month == 2)
 	{
 		 if (Judge(year) == 1 && day > 29)
			return 0;
		else
  			 if (Judge(year) == 0 && day > 28)
  			return 0;
  	}
	 return 1;
}
int main()
{
 	 int w;
 	 bool k;
 	 cout << "是否重新录入测试数据? 1-是,0-否"<<endl;
 	 cin >> k;
 	 if (k == 1)
 	 {
   		cout << "请输入5组测试数据" << endl;
 		  ofstream file("in.txt", ios::out);
 		  if (!file)
	   	{
   	 		cerr << "open error!" << endl;
  	 		 exit(1);
 		  }
  		 for (int i = 0; i < 5; i++)  
 		  {
   	 		cin >> D.year >> D.month >> D.day;
 	 	 	 file << D.year << "  " << D.month << "  " << D.day << "\n";
 		  }
  		 file.close();
 	 }
	 ifstream infile("in.txt", ios::out);//从文件中读取数据
	 ofstream ofile("out.txt", ios::app);//打开out.txt,将结果保存在该文件中
	 if (!infile|| !ofile)
	 {
 		 cerr << "open error!" << endl;
 		 exit(1);
	 }
	 for (int j = 0; j < 5; j++)
	 {
 		 infile >> D.year >> D.month >> D.day;
 		 if(Islegal(D.year, D.month, D.day)==0)
  		 ofile << D.year << "  " << D.month << "  " << D.day << "该日期非法!" << "\n";
 		 else
  			{
 			  w = remainder(D.year, D.month, D.day) % 5;
  			 if (w == 1 || w == 2 || w == 3)
  				  ofile << D.year << "  " << D.month << "  " << D.day << "  ta这一天在打渔!" << "\n";
			   else
  				  ofile << D.year << "  " << D.month << "  " << D.day << "  ta这一天在晒网!" << "\n";
 			 }
	 }
	 infile.close();
	 ofile.close();
	 system("pause");
	 return 0;
}

6.测试屏幕截图:
运行时界面将数据输入到in.txt文件中!在这里插入图片描述

7,总结:
在编程的过程中,练习了C++对日期的处理,同时复习了文件操作。

猜你喜欢

转载自blog.csdn.net/qq_44002167/article/details/88070912