2301_79921278 2023-12-12 13:25 采纳率: 100%
浏览 18
已结题

C++实验课运算符重载



实验5 运算符重载
一、实验目的
1.理解运算符重载概念;
2.学会使用运算符重载;
3.练习类和对象的使用。

二、实验内容
设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。

三、实验要求
在Date类中设计如下重载运算符函数:
    Date operator+(int days):返回某日期加上天数得到的日期
    Date operator-(int days):返回某日期减去天数得到的日期
int operator-(Date&b):返回两日期相差的天数
在实现这些重载运算符函数时调用以下私有成员函数:
leap(int):判断指定的年份是否为闰年
dton(Date &):将指定日期转换成从0年O月O日起的天数
ntod(int):将指定的0年O月O日起的天数转换成对应的日期

提示:可定义一个二维数组,存放闰年和非闰年每个月的天数,用于后面的计算,
int day_tab[2][12]={{312831303l,303l,3l,30313031),
    {312931303130313130313031}};
    //    day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年
  • 写回答

7条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-12-12 13:26
    关注
    #include <iostream>
    using namespace std;
    
    class Date {
    private:
        int year, month, day;
    
    public:
        Date(int y, int m, int d) {
            year = y;
            month = m;
            day = d;
        }
    
        bool leap(int y) {
            return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
        }
    
        int dton(Date& date) {
            int days = 0;
            for (int i = 0; i < date.year; i++) {
                if (leap(i)) {
                    days += 366;
                } else {
                    days += 365;
                }
            }
            for (int i = 1; i < date.month; i++) {
                if (leap(date.year)) {
                    days += day_tab[1][i - 1];
                } else {
                    days += day_tab[0][i - 1];
                }
            }
            days += date.day - 1;
            return days;
        }
    
        Date ntod(int n) {
            Date date(0, 1, 1);
            while (n >= 365) {
                if (leap(date.year)) {
                    n -= 366;
                } else {
                    n -= 365;
                }
                date.year++;
            }
            for (int i = 0; i < 12; i++) {
                if (leap(date.year) && n < day_tab[1][i]) {
                    date.month = i + 1;
                    date.day = n + 1;
                    break;
                }
                if (!leap(date.year) && n < day_tab[0][i]) {
                    date.month = i + 1;
                    date.day = n + 1;
                    break;
                }
                n -= leap(date.year) ? day_tab[1][i] : day_tab[0][i];
            }
            return date;
        }
    
        Date operator+(int days) {
            int d = dton(*this) + days;
            return ntod(d);
        }
    
        Date operator-(int days) {
            int d = dton(*this) - days;
            return ntod(d);
        }
    
        int operator-(Date& b) {
            int days1 = dton(*this);
            int days2 = dton(b);
            return abs(days1 - days2);
        }
    
        static int day_tab[2][12];
    
        int getYear() {
            return year;
        }
    
        int getMonth() {
            return month;
        }
    
        int getDay() {
            return day;
        }
    };
    
    int Date::day_tab[2][12] ={
            {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    
    int main() {
        int choice;
    
        while (true) {
            cout << "请选择操作:" << endl;
            cout << "1. 计算日期+天数" << endl;
            cout << "2. 计算日期-天数" << endl;
            cout << "3. 计算日期相差多少天" << endl;
            cout << "0. 退出程序" << endl;
            cin >> choice;
    
            if (choice == 0) {
                break;
            }
    
            if (choice == 1) {
                int year, month, day, days;
                cout << "请输入日期(年 月 日):" << endl;
                cin >> year >> month >> day;
                cout << "请输入天数:" << endl;
                cin >> days;
    
                Date date(year, month, day);
                Date result = date + days;
    
                cout << "加" << days << "天后的日期为:" << result.getYear() << "年" << result.getMonth() << "月" << result.getDay() << "日" << endl;
            } else if (choice == 2) {
                int year, month, day, days;
                cout << "请输入日期(年 月 日):" << endl;
                cin >> year >> month >> day;
                cout << "请输入天数:" << endl;
                cin >> days;
    
                Date date(year, month, day);
                Date result = date - days;
    
                cout << "减" << days << "天后的日期为:" << result.getYear() << "年" << result.getMonth() << "月" << result.getDay() << "日" << endl;
            } else if (choice == 3) {
                int year1, month1, day1, year2, month2, day2;
                cout << "请输入第一个日期(年 月 日):" << endl;
                cin >> year1 >> month1 >> day1;
                cout << "请输入第二个日期(年 月 日):" << endl;
                cin >> year2 >> month2 >> day2;
    
                Date date1(year1, month1, day1);
                Date date2(year2, month2, day2);
    
                int diff = date1 - date2;
    
                cout << "两个日期相差" << diff << "天" << endl;
            }
        }
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 赞助了问题酬金15元 12月12日
  • 创建了问题 12月12日