日期相差天数有关公元前


http://img.mukewang.com/58f4dbf500013bb511810616.jpg

http://img.mukewang.com/58f4dbf600012ed010330710.jpg

下面附上我的代码 公元之后的我写了 公元前还没有

#include <iostream>
using namespace std;


class Date
{
 friend int Delta(Date d1, Date d2)
 {
  int days = 0;
  if (d1.year > 0&&d2.year > 0)
  {
   for (int i = d1.year; i < d2.year; i++)
   {
    if ((i % 4 == 0 && i % 100 == 0) || i % 400 == 0)
     days += 366;
    else
     days += 365;
   }
  }
  else
  {
   for (int i = d1.year; i < d2.year; i++)
   {
    if (((i+1) % 4 == 0 && (i+1) % 100 == 0) || (i+1) % 400 == 0)
     days += 366;
    else
     days += 365;
   }
  }
  if (d1.month > 0)
  {
   for (int j = 1; j < d1.month; j++)
   {
    if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 || j == 12)
     days -= 31;
    else if (j == 4 || j == 6 || j == 9 || j == 11)
     days -= 30;
    else if (j == 2)
    {
     if ((d1.year % 4 == 0 && d1.year % 10 == 0) || d1.year % 400 == 0)
      days -= 29;
     else
      days -= 28;
    }
   }
  }
  else
  {


  }
  days -= d1.day;
  if (d2.month > 0)
  {
   for (int k = 1; k < d2.month; k++)
   {
    if (k == 1 || k == 3 || k == 5 || k == 7 || k == 8 || k == 10 || k == 12)
     days += 31;
    else if (k == 4 || k == 6 || k == 9 || k == 11)
     days += 30;
    else if (k == 2)
    {
     if ((d2.year % 4 == 0 && d2.year % 10 == 0) || d2.year % 400 == 0)
      days += 29;
     else
      days += 28;
    }
   }
  }
  else
  {


  }
  days += d2.day;
  return days;
 }
 friend std::istream & operator>>(std::istream & is, const Date &d)
 {
  is >> d.year;
  cout << "-";
  is >> d.month;
  cout << "-";
  is >> d.day;
  cout << endl;
  return is;
 }
public:
 Date()
 {
  year = 0;
  month = 0;
  day = 0;
 }
 ~Date(){}
 Date(int y, int m, int d)
 {
  year = y;
  month = m;
  day = d;
 }
private:
 int year;
 int month;
 int day;
};
int main()
{
 Date date1, date2;
 cin >> date1 >> date2;
 int delt = Delta(date2, date1);
 cout << delt << endl;
 return 0;


}

求大神帮帮忙

晴书
浏览 1011回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP