DS博客作业01--日期抽象数据类型设计与实验

1.思维导图及学习体会

1.1第一张绪论知识点思维导图

1.2学习体会

一开学就是大作业,我方了。学习数据结构呢其实跟之前学习C的时候感觉没多大差别,最主要的就是从C换成了C++,虽然并没有学过C++,但是还有百度(可还行)。不会就查,查多了,也就懂得多了,毕竟C++跟C差不多。最主要得是,C++真的比C要少写好多行代码。目前学的知识呢,还比较简单,不过接下来要更加努力,别再像上学期那样到课设的时候,还有知识点不会去查书。

2.大作业作业内容

2.1设计日期的ADT类型

ADT Date{
数据对象:D={year,month,day|year,month,day属于ElemType类型}
数据关系:R={<year,month>,<month,day>}
数据操作:
Status InitDate(Date &date, ElemType year, ElemType month,ElemType day);
    //初始化日期
    //操作结果:构造三元组date,元素year、month、day分别为年、月、日
string ExpDate(Date date);
    //初始条件:三元组date存在
    //操作结果:以字符串的形式输出日期
Status IsLeapyear(ElemType year);
    //初始条件:三元组date存在
    //操作结果:若是闰年则返回TRUE,否则返回FALSE
string Week(Date date);
    //初始条件:三元组date存在
    //操作结果:返回这个日期是星期几
string MonthInEnglish(Date date);
    //初始条件:三元组date存在
    //操作结果:返回月份英文名
void AddDate(Date &date, ElemType days);
    //初始条件:三元组date存在
    //操作结果:返回当前日期增加days天的日期
char CompareDate(Date date,Date OtherDate);
    //初始条件:三元组date存在
    //操作结果:比较当前日期与OtherDate的逻辑关系
}ADT Date

2.2数据抽象:头文件

Common.h

Date.h

2.3数据封装说明

(1)构造三元组函数

分配动态内存,构造三元组,并判断日期是否合法

(2)格式化日期函数

以字符串的形式输出日期

(3)判断闰年函数

按照公式判断该日期是年份否是闰年,是返回TRUE,反之返回FALSE

(4)返回星期函数

根据公式判断该日期所在的星期,并返回该星期

(5)返回月份英文函数

(6)增加日期函数

输入所加的天数,并返回增加改天数之后的日期

(7)比较函数

判断输入日期与原日期之间的逻辑关系

(8)主函数

int main()
{
	Date date;//定义一个date的三元组
	int year, month, day;
	int days;
	string newDate;         //增加days天后的日期
	Date otherDate;
	string strDate;
	string strWeek;
	string str;
	char op;
	ifstream infile;
	infile.open("input.txt", ios::in);
	ofstream outfile;
	outfile.open("output.txt", ios::out);
	while (!infile.eof())
	{
		infile >> year >> month >> day;
		if (!InitDate(date, year, month, day))
		{
			strDate = ExpDate(date);
			cout << strDate << "不是合法日期" << endl << endl;
			outfile << strDate << "不是合法日期" << endl << endl;
			continue;
		}
		strDate = ExpDate(date);
		cout << strDate << endl;
		outfile << strDate  << endl;
		if (IsLeapyear(year))
		{
			cout << year << "是闰年" << endl;
			outfile << year << "是闰年" << endl;
		}
		else
		{
			cout << year << "不是闰年" << endl;
			outfile << year << "不是闰年" << endl;
		}
		strWeek = Week(date);
		cout << strDate << strWeek << endl;
		outfile << strDate << strWeek << endl;
		str = MonthInEnglish(date);
		cout << strDate << "月份是" << str << endl;
		outfile << strDate << "月份是" << str << endl;
		cout << "请输入增加天数:";
		cin >> days;
		AddDate(date, days);
		newDate = ExpDate(date);
		cout << strDate << '+' << days << "是" << newDate << endl;
		outfile << strDate << '+' << days << "是" << newDate << endl;
		cout << "请输入与当前日期相比较的日期:";
		/*cin >> year >> month >> day;
		while (!InitDate(otherDate, year, month, day));
		{
			cout << "请输入正确的日期:" << endl;
			cin >> year >> month >> day;
		}*/
		while (1)
		{
			cin >> year >> month >> day;
			if (!InitDate(otherDate, year, month, day))
			{
				cout << "请输入正确的日期:" << endl;
				continue;
			}
			else
				break;
		}
		newDate = ExpDate(otherDate);
		op = CompareDate(date, otherDate);
		cout << strDate << op << newDate << endl<<endl;
		outfile << strDate << op << newDate << endl<<endl;
	}
	infile.close();
	outfile.close();
	system("pause");
}

3.结果展示

输入数据

输出结果

4.调试碰到问题

  • 一开始的时候,输出英文月份的那个函数无法运行,导致整个函数都崩了。去找了一下我函数中的逻辑关系,发现并没有问题,但是一到那个地方程序就崩了,之后又差不多找了半个多小时的代码问题(因为vs那会不太会调试,就没一步步调试了),后来突然get到vs的调试方法,然后从头调试,然后发现就算三元组传递到函数的时候没有&取地址符也会改变三元组中数据的值,就把每个函数都添加了一些临时变量,来存放三元组中的值。然后程序就正确运行了。
原文地址:https://www.cnblogs.com/Lay-549/p/10470705.html