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數據封裝說明

申請空間而且判斷是否合法code

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;
}

判斷是否閏年blog

Status IsLeap(DATE D)
{
    if ((D[0] % 4 == 0 && D[0] % 100 == 0) || D[0] % 400 == 0)
        return TRUE;
    else
        return FALSE;
}

判斷是哪一個月ci

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]];
}

判斷是哪一天input

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];
}

增長天數string

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++不大會,恰好舍友百度到了用法,就用了那個語法
  • 整個代碼大部分上都是借鑑大佬的,邊看他們代碼邊理解
相關文章
相關標籤/搜索