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)构造三元组函数

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

(2)格式化日期函数

以字符串的形式输出日期
数据结构

(3)判断闰年函数

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

(4)返回星期函数

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

(5)返回月份英文函数

(6)增长日期函数

输入所加的天数,并返回增长改天数以后的日期
设计

(7)比较函数

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

(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的调试方法,而后从头调试,而后发现就算三元组传递到函数的时候没有&取地址符也会改变三元组中数据的值,就把每一个函数都添加了一些临时变量,来存放三元组中的值。而后程序就正确运行了。
相关文章
相关标签/搜索