为了账号安全,请及时绑定邮箱和手机立即绑定

日期相差天数有关公元前

日期相差天数有关公元前

C++
晴书 2017-04-17 23:16:22
下面附上我的代码 公元之后的我写了 公元前还没有#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;}求大神帮帮忙
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1008 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信