程序设计方法学 三天打鱼两天晒网问题

程序设计方法学 三天打鱼两天晒网问题


#include<iostream>
#include<fstream>
using namespace std;
class Date
{
private:
	int year, month, day;
public:
	Date(int x, int y, int z);
	int fun(Date &A);
};
Date::Date(int x, int y, int z)
{
	year = x;
	month = y;
	day = z;
}
int Date::fun(Date &A)
{
	int thenumber = 0;  //在所给年份到初始年份的天数
	int psnd = 0;
	int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (((this->year % 4 == 0 && this->year % 100 != 0) || this->year % 400 == 0))
	// 校正闰年2月份天数
	{
		days[2] ++;
	}
	if (this->year < 2010)    //判断年输入正确性
	{
		cerr << "年份不得小于2010!" << endl; return 0;
	}
	else if (this->month < 0 || this->month > 12)  //判断月输入正确性
	{
		cerr << "月份输出有误" << endl; return 0;
	}
	else if (this->day > days[this->month])  //判断日输入正确性
	{
		cerr << "日输入有误" << endl; return 0;
	}
	else  //计算从输入日期到初始日期的天数
	{
		for (int i = 2010; i < this->year; i++) //在所给年份到初始年份中闰年的个数
		{
			if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
			psnd++;
		}
		thenumber = (this->year - A.year) * 365 + psnd;
		for (int i = 0; i < this->month; i++)
		{
			thenumber += days[i];
		}
		thenumber += this->day + 1;
		return thenumber;
	}
}
int main()
{
	int judge, flag;
	int Innumber;  //输入数据
	int Fnumber;   //测试数据
	int cyear, cmonth, cday;
	Date First(2010, 1, 1);  // 初始年月日
	cout << "请选择输入方式:" << endl << "1、手动输入方式" << endl << "2、文件输入方式:";
	cin >> flag;
	if (flag == 1)
	{ 
		cout << "please input a date:";
		cin >> Innumber;
		cyear = Innumber / 10000;  //分离年
		cmonth = Innumber % 10000 / 100;  //分离月
		cday = Innumber % 100;   //分离日
		Date InputNum(cyear, cmonth, cday);
		if (InputNum.fun(First))
		{
			judge = InputNum.fun(First) % 5;
			if (judge > 3)  //判断该天进行的活动
			cout << " 今天晒网。" << endl;
			else
			cout << " 今天打鱼。" << endl;
		}
	}
	else if (flag == 2)
	{
		ifstream infile("in.txt", ios::in);
		infile >> Fnumber >> Innumber; //从文件中读取数据
		cout << "文件输入日期为:" << Innumber << endl << "开始活动日期(初始日期)为:" << Fnumber << endl;
		infile.close();
		cyear = Innumber / 10000;  //分离年
		cmonth = Innumber % 10000 / 100;  //分离月
		cday = Innumber % 100;  //分离日
		Date InputNum(cyear, cmonth, cday);
		if (InputNum.fun(First))
		{
			judge = InputNum.fun(First) % 5;
			ofstream outfile("out.txt", ios::out);
			if (judge > 3)  //判断该天进行的活动
				outfile << Innumber << " 该天晒网";
			else
				outfile << Innumber << " 该天打鱼";
			outfile.close();
		}
	}
	else
		cerr << "序号有误!" << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值