【问题标题】:Total days between 2 dates C++2 个日期之间的总天数 C++
【发布时间】:2016-11-12 00:38:22
【问题描述】:

我正在尝试解决一个问题,该问题要求我给出两个日期之间的总天数。

我必须处理这两个日期之间的一些问题,例如闰年和用户输入年份的方式。 (例如,如果你输入 1 和 17,代码仍然会给你 16 年的差异(2017 - 2001 = 16)。我不应该更改 main() 函数内的任何内容。

这是我的代码。

 #include <iostream>
 #include <cmath>

 using namespace std;

 class date
 {
    private:
    int m;
    int d;
    int y;

    public:
    date(int, int, int);
    int countLeapYears(date&);
    int getDifference(date&);
    int operator-(date&);    
  };

   int main()
   {
    int day, month, year;
    char c;

    cout << "Enter a start date: " << endl;
    cin >> month >> c >> day >> c >> year;

    date start = date(month, day, year);

    cout << "Enter an end date: " << endl;
    cin >> month >> c >> day >> c >> year;

    date end = date(month, day, year);
    int duration = end-start;

     cout << "The number of days between those two dates are: " <<    
     duration << endl;

     return 0;
   }


    date::date(int a, int b, int c)
    {
    m = a;
    d = b;
    y = c;
    }

    const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,   
    30, 31};

   int date::countLeapYears(date& d)
   {
      int years = d.y;
      if (d.m <= 2)
      years--;
      return years / 4 - years / 100 + years / 400;
    }

   int date::getDifference(date& other)
   {

      int n1 = other.y*365 + other.d;

      for (int i=0; i<other.m - 1; i++)
     {
       n1 += monthDays[i];
       n1 += countLeapYears(other);
      }

      return n1;
      }

   int date::operator-(date& d) {
      int difference = getDifference(d);
      return difference;
    }

当我运行这段代码时,它显示invalid binary operation between "date" and "date"

现在,我假设当我初始化int duration = end - start 时,我应该得到一个数字。我想我在这里做错的是我未能将(结束 - 开始)日期类型转换为整数。我以为我的函数 getDifference 已经解决了这个问题,但显然它没有。

【问题讨论】:

  • 2007 - 20017 - 1 是一样的,我真的不明白这有什么危险。
  • 我不明白你在说什么。
  • 你的问题很难理解。你的意思是你应该像用户输入2007一样接受输入7?如果是这样,只需检查输入的年份是否小于2000 并在这种情况下添加2000
  • @DUman 谢谢!我的大脑今天不工作。之后,在 main() 函数中,当我编译时,它说 int duration = end - start 无效,因为我从类日期到 int 类型。有什么技巧可以解决这个问题?
  • -,减法运算符,没有为您的类定义。您的编译器不知道如何减去您的 date 对象,它们不是数字。 C++ 允许您为类定义运算符,例如 +-。要搜索的术语是“运算符重载”。

标签: c++


【解决方案1】:

接受挑战。

使用这个free, open-source, header-only date library

#include "date.h"
#include <iostream>

namespace me
{

class date
{
    ::date::sys_days tp_;
public:
    date(int month, int day, int year)
        : tp_{::date::year(year)/month/day}
        {}

    friend
    int
    operator-(const date& x, const date& y)
    {
        return (x.tp_ - y.tp_).count();
    }
};

}  // namespace me

using namespace std;
#define date me::date

int main()...

【讨论】:

  • 这看起来很棒,但我不应该使用 date.h 来解决这个难题。我的朋友通过使用运算符重载给了我提示,我不应该更改 main() 函数。我还在努力。
  • @Hugh:实际上最可靠的日期算​​法比作业级编码更先进。对于想要通过这些公共领域算法编写自己的日期代码的人:howardhinnant.github.io/date_algorithms.html
  • 霍华德先生,您好,我上面的代码有问题,需要您的帮助。当我运行它时,它说“日期”和“日期”之间的二进制操作无效。现在,我假设当我初始化 int duration = end - start 时,我应该得到一个数字。我想我在这里做错了什么是我未能将(结束 - 开始)日期类型转换为整数。我认为我的函数 getDifference 已经解决了这个问题。不知何故,我似乎没有处理这个问题。
  • @Hugh:搜索你的书,或者你拥有的任何关于这个主题的课程材料:“重载运算符”。
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2012-09-09
  • 2014-09-19
  • 1970-01-01
相关资源
最近更新 更多