DS博客作业01--日期抽象数据类型设计与实现
1思维导图及学习体会
1.1思维导图

1.2学习体会
整篇大作业都用了C++来写,之前都是用C忽然换了个语言有点不习惯,然后遇到语法问题只能百度百度,或者看看大佬代码
其实整篇的代码,都是边看大佬的边理解边打的,好像都快是抄袭了
,不过总体上还是理解了
自己打的代码可能会比较低效率,有时候看了大佬的,都觉得自己愚蠢,所以看看大佬的代码学习一下他们的思路还是可取的
2大作业作业内容
2.1设计日期的ADT类型
ADT DATE
{
数据对象:D={year,month,day|year,month,day属于int类型}
数据关系:R={<year,month>,<month,day>}
数据操作:
Status InitDate(DATE &D,int year,int month,int day);
//操作结果:构造了三元组D,赋初值给year,month,day
string PutDate(DATE D);
//初始条件:日期不为空
//操作结果:输出日期的格式为:XX/XX/XX
Status IsLeap(DATE D);
//初始条件:日期不为空
//操作结果:是闰年返回TRUE,不是返回FLASE
string WhichDay(DATE D);
//初始条件:日期不为空
//操作结果: 返回星期几,用string的类型输出
string WhichMonth(DATE D);
//初始条件:日期不为空
//操作结果:返回月份的英文名,用string的类型输出
string AddDate(DATE D,int days);
//初始条件:日期不为空
//操作结果:返回当前日期增加days天的日期
Status CompDate(DATE D,DATE otherdate);
//初始条件:日期不为空
//操作结果:当前日期比较大返回1,一样返回0,比较小返回-1
}ADT DATE;
2.2数据抽象:头文件
common.h

date.h

2.3数据封装说明
申请空间并且判断是否合法
Status InitDate(DATE &D, int year, int month, int day)
{
int result = TRUE;
D = new int[3];/*动态申请空间*/
if (!D)
exit(OVERFLOW);/*分配存储空间失败*/
D[0] = year;
D[1] = month;
D[2] = day;
if (month < 1 || month>12)
{
result = FALSE;
}
else if(month==2)
{
if (IsLeap(D))
{
if (day > 29)
result = FALSE;
}
else
{
if (day > 28)
result = FALSE;
}
}
else
{
if (day > MonthDay[month])
result = FALSE;
}
return result;
}
输出日期
string PutDate(DATE D)
{
string str;
int i;
char ch[100];
for (i = 0; i < 3; i++)
{
_itoa_s(D[i], ch, 10); /*调用itoa函数将十进制转化为字符型数据储存*/
str += ch;
if (i < 2)
str += "/";
}
return str;
}
判断是否闰年
Status IsLeap(DATE D)
{
if ((D[0] % 4 == 0 && D[0] % 100 == 0) || D[0] % 400 == 0)
return TRUE;
else
return FALSE;
}
判断是哪个月
string WhichMonth(DATE D)
{
char ch[13][15] = { "0","January","February","March","April","May","June","July","August","September","October","November","December" };
return ch[D[1]];
}
判断是哪一天
string WhichDay(DATE D)
{
int year = D[0];
int month = D[1];
int day = D[2];
int sign;
char ch[7][20] = { "是星期一","是星期二" ,"是星期三","是星期四" ,"是星期五","是星期六" ,"是星期日" };
if (month < 3)
{
month += 12;
year -= 1;
}
/*用基姆拉尔森计算公式计算星期几*/
sign = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
return ch[sign];
}
增加天数
int MonthDay[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
DATE AddDate(DATE D, int days)
{
int i;
D[2] += days;
while (1)
{
if (D[1] == 2)
{
if (IsLeap(D))
{
if (D[2] <= 29)
break;
}
else
{
if (D[2] <= 28)
break;
else
D[2]--;
}
}
if (D[2] > MonthDay[D[1]])
{
D[2] -= MonthDay[D[1]];
D[1]++;
if (D[1] > 12)
{
D[0]++;
D[1] -= 12;
}
}
break;
}
return D;
}
判断逻辑关系
char CompDate(DATE D, DATE otherDate)
{
int day1, day2;
day1 = D[0] * 10000 + D[1] * 100 + D[2];
day2 = otherDate[0] * 10000 + D[1] * 100 + D[2];
if (day1 > day2)
return '>';
else if (day1 < day2)
return '<';
else
return '=';
}
主函数
int main()
{
int year, month, day;
int days;
DATE D;
DATE addDate; /*增加的天数后的日期*/
DATE otherDate; /*需要对比大小的日期*/
string tempDate,tempWeek,tempMonth,tempstr; /*用来做临时储存的变量*/
char op; /*比较大小的符号*/
ifstream infile("input.txt"); /*以读的方式打开文件input.txt*/
ofstream outfile("out.txt"); /*以写的方式打开文件out.txt*/
while (!infile.eof())
{
infile >> year >> month >> day;
/*判断是否合法*/
if (!InitDate(D, year, month, day))
{
tempDate = PutDate(D);
cout << tempDate << "不是合法日期" << endl<<endl;
outfile<<tempDate<< "不是合法日期" << endl<<endl;
continue; /*非法日期,不进入下面的函数,继续读取下一个日期*/
}
tempDate = PutDate(D); /*将日期转化格式*/
cout << tempDate << endl;
outfile << tempDate << endl;
/*判断是否闰年*/
if (IsLeap(D))
{
cout << year << "是闰年" << endl;
outfile << year << "是闰年" << endl;
}
else
{
cout << year << "不是闰年" << endl;
outfile << year << "不是闰年" << endl;
}
/*判断是星期几*/
tempWeek = WhichDay(D);
cout << tempDate << tempWeek<< endl;
outfile << tempDate<< tempWeek<<endl;
/*判断是几月份*/
tempMonth = WhichMonth(D);
cout << tempDate << "的月份是"<<tempMonth << endl;
outfile << tempDate << "的月份是"<<tempMonth << endl;
/*增加天数后的日期*/
cout << "请输入需要增加的天数:";
cin >> days;
addDate = AddDate(D, days);
tempstr = PutDate(addDate);
cout << tempDate << "+" << days << "是" << tempstr << endl;
outfile << tempDate << "+" << days << "是" << tempstr << endl;
/*进行日期比较*/
while (1)
{
cout << "请输入日期进行比较(日期中间用空格隔开):";
cin >> year >> month >> day;
if (!InitDate(otherDate, year, month, day))
{
tempstr = PutDate(otherDate);
cout << tempstr<< "不是合法日期" << endl << endl;
continue;
}
else
{
break;
}
}
op = CompDate(D, otherDate);
tempstr = PutDate(otherDate);
cout << tempDate << op<< tempstr << endl << endl;
outfile<< tempDate << op<< tempstr << endl << endl;
}
system("pause");
infile.close();
outfile.close();
}
3结果显示
运行结果

input文件

out文件

4调试碰到问题
- 我是先写主函数在写各个函数的,想着先把主体构建好再构建其他的
- 读取文件以及写入文件用C++不大会,刚好舍友百度到了用法,就用了那个语法
- 整个代码大部分上都是借鉴大佬的,边看他们代码边理解

浙公网安备 33010602011771号