日期

  1 #include <iostream>
  2 bool is_leap(int year)//判断闰年
  3 {
  4     if (year % 100 == 0)
  5     {
  6         if (year % 400 == 0)
  7             return true;
  8         else
  9             return false;
 10     }
 11     else
 12     {
 13         if (year % 4 == 0)
 14         {
 15             return true;
 16         }
 17         else
 18         {
 19             return false;
 20         }
 21     }
 22 }
 23 using namespace std;
 24 class date
 25 {
 26 public:
 27     date(int y, int m, int d) :year(y), month(m), day(d) {};
 28     date() {};
 29     ~date() {};
 30     void show()
 31     {
 32         cout << year << "/" << month << "/" << day << " ";
 33     }
 34 public:
 35     int year;
 36     int month;
 37     int day;
 38 };
 39 int get_days(class date d)
 40 {
 41     int days = 0;
 42     for (int i = 1935; i < d.year; i++)
 43     {
 44         if (is_leap(i))
 45             days += 366;
 46         else
 47             days += 365;
 48     }
 49     for (int i = 1; i < d.month; i++)
 50     {
 51         if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
 52             days += 31;
 53         else if (i == 4 || i == 6 || i == 9 || i == 11)
 54             days += 30;
 55         else
 56         {
 57             if (is_leap(d.year))
 58                 days += 29;
 59             else
 60                 days += 28;
 61         }
 62     }
 63     days += d.day;
 64     return days;
 65 }
 66 void cmp_date(class date d1, class date d2)
 67 {
 68     int days1 = get_days(d1);
 69     int days2 = get_days(d2);
 70     int diff = days1 - days2;
 71     {
 72         if (diff == 0)
 73         {
 74             d1.show();
 75             cout << "";
 76             d2.show();
 77             cout << "是同一天" << endl;;
 78         }
 79         else if (diff > 0)
 80         {
 81             d1.show();
 82             cout << "";
 83             d2.show();
 84             cout << "" << diff << "" << endl;;
 85         }
 86         else
 87         {
 88             diff = -diff;
 89             d1.show();
 90             cout << "";
 91             d2.show();
 92             cout << "" << diff << "" << endl;;
 93         }
 94     }
 95 }
 96 int main()
 97 {
 98     while (1)
 99     {
100         int y, m, d;
101         cout << "输入第一个日期年/月/日:";
102         cin >> y >> m >> d;
103         class date d1(y, m, d);
104         cout << "输入第二个日期年/月/日:";
105         cin >> y >> m >> d;
106         class date d2(y, m, d);
107         cmp_date(d1, d2);
108         cout << endl;
109     }
110     return 0;
111 }

 

posted @ 2020-05-30 11:38  幻想Elapse  阅读(134)  评论(0编辑  收藏  举报