bear七 2022-05-31 10:18 采纳率: 83.3%
浏览 74
已结题

c++运算符重载相关代码问题

直接给正确代码也可以的,不用看我写的代码了😢


#include <iostream>
using namespace std;

class Date
{
public:
 // 获取某年某月的天数
 int  GetMonthDay(int year, int month)
 {
  static const int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  if ((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  {
   return 29;
  }
  return monthday[month];
 }

 // 全缺省的构造函数
 Date(int year = 2020, int month = 1, int day = 1)
  {
   if (year >= 0
    && month > 0 && month < 13
    && day>0 && day <= GetMonthDay(year, month))
   {
    _year = year;
    _month = month;
    _day = day;
   }
   else
   {
    cout << "Date invalid" << endl;
   }
  }
 

 // 拷贝构造函数
 Date(const Date& d)
 {
  this->_year = d._year;
  _month = d._month;
  _day = d._day;

 }
 Date& operator=(const Date& d)
 {
  if (this != &d)
  {
   this->_year = d._year;
   this->_month = d._month;
   this->_day = d._day;
  }
  return *this;
 }
 // 析构函数
 ~Date()
 {}

 // 日期+=天数
 Date& operator+=(int day)
 {
  if (day < 0)
  {
   return *this -= -day;
  }
  _day += day;
  while (_day > GetMonthDay(_year, _month))
  {
   _day -= GetMonthDay(_year, _month);
   _month++;
   if (_month == 13)
   {
    _year++;
    _month = 1;
   }
  }
  return *this;
 }
 // 前置++
 Date& operator++()
 {
  *this += 1;
  return *this;
 }

 // 后置++
 Date operator++(int)
 {
  Date tmp(*this);
  *this += 1;
  return tmp;
 }

 // >运算符重载
 bool operator>(const Date& d)
 {
  if (_year > d._year)
  {
   return true;
  }
  else if (_year == d._year)
  {
   if (_month > d._month)
   {
    return true;
   }
   else if (_month == d._month)
   {
    if (_day > d._day)
    {
     return true;
    }
   }
  }
  return false;
 }
 
 // <运算符重载
 bool operator < (const Date& d)
 {
  return !(*this>=d);
 }

 // ==运算符重载
 bool operator==(const Date &d)
 {
  return  ((_year == d._year) && (_month == d._month) && (_day == d._day));
 }
 bool operator != (const Date &d)
 {
  return !(*this == d);
 }
 void Print()
 {
  cout << _year << "-" << _month << "-" << _day << endl;
 }
private:
 int _year;
 int _month;
 int _day;
};
int main()
{
 Date d(2022, 5, 16);
 Date d1(2020, 1, 1);
    if(d==d1) {cout<<"Equal"<<endl;}
    else if(d<d1) {cout<<"Less"<<endl;}
    else {cout<<"Greater"<<endl;}
 Date d2 = d + 1000;
 d2.Print();
 return 0;
}

运算符重载的问题,想问下面的代码怎么改呀,这个日期类Date,要求:
(1) 可以建立具有指定日期(年、月、日)的Date对象,默认日期是2020.1.1。
(2) 可以从输出流输出一个格式为“年-月-日”的日期,其中年是四位数据,月、日可以是一位也可以是两位数据。
(3)可以动态地设置年、月、日。
(4)可以用运算符= =、!=、< 和 > 对两个日期进行比较。
(5)可以用运算符 ++、+= 等完成天数的加一天或若干天的操作
(6)Date类必须能够正确表达日期,不会出现类似于13月,32日一类的情况。Date类还必须处理闰年的问题,闰年包括:所有能被400整除的年份,以及能被4整除同时又不能被100整除的年份。
输入格式:
第一行包含两个日期,第二行包含一个日期和一个整数。输入日期的格式为“年 月 日”。
输出格式:
输出两行:
第一行是比较输入数据第一行的两个日期,若相等,则输出“Equal”,若第一个日期小于第二个日期,则输出“Less”,若第一个日期大于第二个日期,则输出“Greater”。
第二行是输入数据第二行的日期+=输入的整数后的日期,输出格式和题目描述中的(2)一致,也就是“年-月-日”。
请问哪里出错了,出错了应该怎么写代码

  • 写回答

2条回答 默认 最新

  • 关注

    重载++ 、--、+这些运算符的时候,需要对Date类的元素进行修改,而不是直接对类对象操作。
    运行结果:

    img

    代码修改如下:

    /*运算符重载的问题,想问下面的代码怎么改呀,这个日期类Date,要求:
    (1) 可以建立具有指定日期(年、月、日)的Date对象,默认日期是2020.1.1。
    (2) 可以从输出流输出一个格式为“年 - 月 - 日”的日期,其中年是四位数据,月、日可以是一位也可以是两位数据。
    (3)可以动态地设置年、月、日。
    (4)可以用运算符 == 、 != 、< 和 > 对两个日期进行比较。
    (5)可以用运算符 ++、 += 等完成天数的加一天或若干天的操作
    (6)Date类必须能够正确表达日期,不会出现类似于13月,32日一类的情况。Date类还必须处理闰年的问题,闰年包括:所有能被400整除的年份,以及能被4整除同时又不能被100整除的年份。
    输入格式 :
    第一行包含两个日期,第二行包含一个日期和一个整数。输入日期的格式为“年 月 日”。
    输出格式 :
    输出两行:
    第一行是比较输入数据第一行的两个日期,若相等,则输出“Equal”,若第一个日期小于第二个日期,则输出“Less”,若第一个日期大于第二个日期,则输出“Greater”。
    第二行是输入数据第二行的日期 += 输入的整数后的日期,输出格式和题目描述中的(2)一致,也就是“年 - 月 - 日”。
    */
    #include <iostream>
    using namespace std;
    
    class Date
    {
    public:
        // 获取某年某月的天数
        int  GetMonthDay(int year, int month)
        {
            static const int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
            if ((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            {
                return 29;
            }
            return monthday[month];
        }
    
        // 全缺省的构造函数
        Date(int year = 2020, int month = 1, int day = 1)
        {
            if (year >= 0
                && month > 0 && month < 13
                && day>0 && day <= GetMonthDay(year, month))
            {
                _year = year;
                _month = month;
                _day = day;
            }
            else
            {
                cout << "Date invalid" << endl;
            }
        }
    
    
        // 拷贝构造函数
        Date(const Date& d)
        {
            this->_year = d._year;
            _month = d._month;
            _day = d._day;
    
        }
        Date& operator=(const Date& d)
        {
            if (this != &d)
            {
                this->_year = d._year;
                this->_month = d._month;
                this->_day = d._day;
            }
            return *this;
        }
        // 析构函数
        ~Date()
        {}
    
        // 日期+=天数
        Date& operator+=(int day)
        {
            if (day >= 0)
            {
                _day += day;
                while (_day > GetMonthDay(_year, _month))
                {
                    _day -= GetMonthDay(_year, _month);
                    _month += 1;
                    if (_month > 12)
                    {
                        _month = 1;
                        _year += 1;
                    }
                }
            }
            else
            {
                _day -= day;
                while (_day <= 0)
                {
                    _month -= 1;
                    if (_month <= 0)
                    {
                        _month = 12;
                        _year -= 1;
                    }
                    _day += GetMonthDay(_year, _month);
                }
            }
            
            return *this;
        }
        // 前置++
        Date& operator++()
        {
            _day += 1;
            if (_day > GetMonthDay(_year, _month))
            {
                _day = 1;
                _month += 1;
                if (_month > 12)
                {
                    _month = 1;
                    _year += 1;
                }
            }
            return *this;
        }
    
        // 后置++
        Date operator++(int)
        {
            Date tmp(*this); //用临时变量记录原实例的值 
            
            //调整原实例的值
            _day += 1;
            if (_day > GetMonthDay(_year, _month))
            {
                _day = 1;
                _month += 1;
                if (_month > 12)
                {
                    _month = 1;
                    _year += 1;
                }
            }
    
            return tmp; //返回临时变量对象
        }
    
        // >运算符重载
        bool operator>(const Date& d)
        {
            if (_year > d._year)
            {
                return true;
            }
            else if (_year == d._year)
            {
                if (_month > d._month)
                {
                    return true;
                }
                else if (_month == d._month)
                {
                    if (_day > d._day)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    
        
    
        // ==运算符重载
        bool operator==(const Date& d)
        {
            return  ((_year == d._year) && (_month == d._month) && (_day == d._day));
        }
        bool operator != (const Date& d)
        {
            return !(*this == d);
        }
    
        // <运算符重载
        bool operator < (const Date& d)
        {
            if (*this > d)
                return false;
            else if (*this == d)
                return false;
            else
                return true;
        }
    
        void Print()
        {
            cout << _year << "-" << _month << "-" << _day << endl;
        }
    private:
        int _year;
        int _month;
        int _day;
    };
    int main()
    {
        int y1, m1, dt1, y2, m2, dt2;
        int y3, m3, dt3;
        int day;
        cin >> y1 >> m1 >> dt1 >> y2 >> m2 >> dt2; //第一行输入2个日期
        cin >> y3 >> m3 >> dt3 >> day; //第二行输入一个日期和1个整数
        Date d(y1,m1,dt1);
        Date d1(y2,m2,dt2);
        Date d3(y3, m3, dt3);
        if (d == d1) { cout << "Equal" << endl; }
        else if (d < d1) { cout << "Less" << endl; }
        else { cout << "Greater" << endl; }
        d3  += day;
        d3.Print();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月8日
  • 已采纳回答 5月31日
  • 赞助了问题酬金5元 5月31日
  • 修改了问题 5月31日
  • 展开全部

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效