IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 根据艾宾浩斯遗忘曲线利用日期类给自己指定一份背六级单词的计划时间表 -> 正文阅读

[C++知识库]根据艾宾浩斯遗忘曲线利用日期类给自己指定一份背六级单词的计划时间表

背景:还有一个多月就要考英语六级了,昨晚躺在床上苦苦焦虑单词还没有背,这可咋整啊,于是就想到了给自己整理一个详细科学的记忆表来督促自己背单词。按照平常高中时候可能就自己拿笔傻傻地在纸上立着flag了,昨晚在梦里突然想起了可以利用学过的C++日期类给自己指定一份单词记忆表,我恍然大悟,赶紧爬起床敲起了代码,开始写上这份日期表。

有时候总是在刷题,刷了一些题目过后,可能题目思路掌握了,但是似乎很多都没有真正去理解其核心含义。为何不自己针对自己的实际生活学习需求出发,利用编程去便利我们的生活和学习。

设计要求:六级英语单词总共有30个单元的词汇,根据艾宾浩斯遗忘曲线我需要在第1,2,4,7,14,28天的时候去不断重复记忆一个单元的单词。

代码

#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print();
	int GetMonthDay(int year, int month);

	bool operator>(const Date& d);
	bool operator<(const Date& d);
	bool operator>=(const Date& d);
	bool operator<=(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	//这六个成员函数只需要实现两个其他的实现就可以复用
	//不仅仅是Date类可以这样实现,所有的类实现比较,都可以用这种复用的方式

	Date& operator+=(int day);
	Date& operator++();
	int operator-(const Date& d);
	//打印日期是星期几
	void PrintGetWeekDay();
private:
	int _year;
	int _month;
	int _day;
};
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!(_year >= 0) &&
		(_month > 0 && _month < 13)
		&& (_day > 0 && _day <= GetMonthDay(year, month)))
	{
		cout << "非法日期->";
		this->Print();
	}
}//自行设计一个构造函数来判断用户输入的日期是否合法
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day;
}
int Date::GetMonthDay(int year, int month)
{
	static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = monthDayArray[month];
	//一年大概365天+5小时
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))//先判断是否是二月,再判断闰年
	{
		day += 1;//如果是闰年并且是二月,那么day+1
	}
	return day;
}
bool Date::operator>(const Date& d)//bool operator>(Date* const this,const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year&&_month > d._month)
	{
		return true;
	}
	else if (_year == d._year&&_month == d._month&&_day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//d1==d2
bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
//d1>=d2
bool Date::operator>=(const Date& d)
{
	return *this > d || *this == d;
}
//d1<d2
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
//d1<=d2
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}
//d1+=100
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))//大于月份天数就需要再处理
	{
		_day -= GetMonthDay(_year, _month);
		_month++;//_month要先减了当月的天数再++
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;//这里返回的是d1
}
//++d1.自定义类型++推荐使用前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;//返回++之后的值
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;//默认假设为第一个大第二个小
	int flag = 1;//设定一个flag为正数1,第一个大于第二个时相减为正数
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;//如果第一个小第二个大时第一个和第二个相减时结果为负数,将flag置为1
	}
	int count = 0;//利用计数器去不断++,最终count就是相差的天数
	while (min != max)
	{
		++min;
		++count;
	}
	return count * flag;
}
void Date::PrintGetWeekDay()
{
	const char* arr[] = { "星期一","星期二","星期三", "星期四", "星期五", "星期六", "星期日" };
	Date start(1900, 1, 1);
	int count = *this - start;
	cout << arr[count % 7] << endl;
}
int main()
{
	Date start(2022, 5, 5);
	for (int i = 1; i <= 30; i++)
	{
		start.Print();
		printf("\t");
		start.PrintGetWeekDay();
		start += 1;
		printf("%d\t", i);
		if ((i - 1) > 0)
		{
			printf("\t%d\t", (i - 1));
		}
		if ((i - 3) > 0)
		{
			printf("\t%d\t", (i - 3));
		}
		if ((i - 6) > 0)
		{
			printf("\t%d\t", (i - 6));
		}
		if ((i - 13) > 0)
		{
			printf("\t%d\t", (i - 13));
		}
		if ((i - 27) > 0)
		{
			printf("\t%d\t", (i - 27));
		}
		printf("\n");
		printf("----------------------------------------------------------------------------------------------\n");
	}
	return 0;
}

运行结果:

2022-5-5        星期四
1
----------------------------------------------------------------------------------------------
2022-5-6        星期五
2               1
----------------------------------------------------------------------------------------------
2022-5-7        星期六
3               2
----------------------------------------------------------------------------------------------
2022-5-8        星期日
4               3               1
----------------------------------------------------------------------------------------------
2022-5-9        星期一
5               4               2
----------------------------------------------------------------------------------------------
2022-5-10       星期二
6               5               3
----------------------------------------------------------------------------------------------
2022-5-11       星期三
7               6               4               1
----------------------------------------------------------------------------------------------
2022-5-12       星期四
8               7               5               2
----------------------------------------------------------------------------------------------
2022-5-13       星期五
9               8               6               3
----------------------------------------------------------------------------------------------
2022-5-14       星期六
10              9               7               4
----------------------------------------------------------------------------------------------
2022-5-15       星期日
11              10              8               5
----------------------------------------------------------------------------------------------
2022-5-16       星期一
12              11              9               6
----------------------------------------------------------------------------------------------
2022-5-17       星期二
13              12              10              7
----------------------------------------------------------------------------------------------
2022-5-18       星期三
14              13              11              8               1
----------------------------------------------------------------------------------------------
2022-5-19       星期四
15              14              12              9               2
----------------------------------------------------------------------------------------------
2022-5-20       星期五
16              15              13              10              3
----------------------------------------------------------------------------------------------
2022-5-21       星期六
17              16              14              11              4
----------------------------------------------------------------------------------------------
2022-5-22       星期日
18              17              15              12              5
----------------------------------------------------------------------------------------------
2022-5-23       星期一
19              18              16              13              6
----------------------------------------------------------------------------------------------
2022-5-24       星期二
20              19              17              14              7
----------------------------------------------------------------------------------------------
2022-5-25       星期三
21              20              18              15              8
----------------------------------------------------------------------------------------------
2022-5-26       星期四
22              21              19              16              9
----------------------------------------------------------------------------------------------
2022-5-27       星期五
23              22              20              17              10
----------------------------------------------------------------------------------------------
2022-5-28       星期六
24              23              21              18              11
----------------------------------------------------------------------------------------------
2022-5-29       星期日
25              24              22              19              12
----------------------------------------------------------------------------------------------
2022-5-30       星期一
26              25              23              20              13
----------------------------------------------------------------------------------------------
2022-5-31       星期二
27              26              24              21              14
----------------------------------------------------------------------------------------------
2022-6-1        星期三
28              27              25              22              15              1
----------------------------------------------------------------------------------------------
2022-6-2        星期四
29              28              26              23              16              2
----------------------------------------------------------------------------------------------
2022-6-3        星期五
30              29              27              24              17              3
----------------------------------------------------------------------------------------------

D:\c++code\ppp\Debug\ppp.exe (进程 14564)已退出,返回代码为: 0。
按任意键关闭此窗口...

好了,写到这里顺便自己又复习了一遍日期类,背单词去咯哈哈哈哈哈哈哈

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-07 11:00:54  更:2022-05-07 11:01:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 4:14:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码