我试图创建一个读取两个日期并选择较大日期的函数,然后在几天内获得差异。到目前为止,我已经尝试了很多但有时答案是不正确的 我无法找到问题所在。谁能帮我找到问题,和/或就如何调试这类问题提出一些建议?
#include < iostream >
#include < string.h >
#include < string>
#include < cstdlib >
#include < sstream >
using namespace std;
int period(string days1, string months1, string years1, string days2,string months2, string years2)
{
int periodday, periodmonth, periodyear, periodindays,
year1 = atof(years1.c_str()), month1 = atof(months1.c_str()),
day1 = atof(days1.c_str()), year2 = atof(years2.c_str()),
month2 = atof(months2.c_str()), day2 = atof(days2.c_str());
int biggeryear, smalleryear, biggermonth, smallermonth, biggerday, smallerday;
if (year1 > year2) {
biggeryear = year1;
biggermonth = month1;
biggerday = day1;
smalleryear = year2;
smallermonth = month2;
smallerday = day2;
}
else {
biggeryear = year2;
biggermonth = month2;
biggerday = day2;
smalleryear = year1;
smallermonth = month1;
smallerday = day1;
}
if (year1 == year2) {
if (month1 > month2) {
biggeryear = year1;
biggermonth = month1;
biggerday = day1;
smalleryear = year2;
smallermonth = month2;
smallerday = day2;
}
else {
biggeryear = year2;
biggermonth = month2;
biggerday = day2;
smalleryear = year1;
smallermonth = month1;
smallerday = day1;
}
if (month1 == month2) {
if (day1 > day2) {
biggeryear = year1;
biggermonth = month1;
biggerday = day1;
smalleryear = year2;
smallermonth = month2;
smallerday = day2;
}
else {
biggeryear = year2;
biggermonth = month2;
biggerday = day2;
smalleryear = year1;
smallermonth = month1;
smallerday = day1;
}
}
}
periodday = biggerday - smallerday;
periodmonth = biggermonth - smallermonth;
periodyear = biggeryear - smalleryear;
if (periodday < 0) {
periodmonth = periodmonth - 1;
if (biggermonth == 1 || biggermonth == 3 || biggermonth == 5 ||
biggermonth == 7 || biggermonth == 8 | biggermonth == 12 ||
biggermonth == 10) {
periodday += 31;
}
if (biggermonth == 4 || biggermonth == 6 || biggermonth == 9 ||
biggermonth == 11) {
periodday += 30;
}
if (biggermonth == 2 && biggeryear == 0) {
periodday += 28;
}
if (biggermonth == 1 && biggeryear % 4 != 0) {
periodday += 29;
}
}
if (periodmonth < 0) {
periodyear--;
if (biggermonth == 1 || biggermonth == 3 || biggermonth == 5 ||
smallermonth == 7 || biggermonth == 8 || biggermonth == 12 ||
biggermonth == 10)
{
periodmonth = periodmonth + 12;
periodmonth = abs(periodmonth) * 31;
}
if (biggermonth == 4 || biggermonth == 6 || biggermonth == 9 ||
biggermonth == 11) {
periodmonth = periodmonth + 12;
periodmonth = abs(periodmonth) * 30;
}
if (biggermonth == 2 && biggeryear % 4 == 0) {
periodmonth = periodmonth + 12;
periodmonth = abs(periodmonth) * 28;
}
if (biggermonth == 2 && biggeryear % 4 != 0) {
periodmonth = periodmonth + 12;
periodmonth = abs(periodmonth) * 29;
}
}
else if (periodmonth >= 0) {
if (biggermonth == 1 || biggermonth == 3 || biggermonth == 5 ||
biggermonth == 7 || biggermonth == 8 || biggermonth == 12 ||
biggermonth == 10) {
periodmonth = periodmonth * 31;
}
if (biggermonth == 4 || biggermonth == 6 || biggermonth == 9 ||
biggermonth == 11) {
periodmonth = periodmonth * 30;
}
if (biggermonth == 2 && biggeryear % 4 == 0) {
periodmonth = periodmonth * 28;
}
if (biggermonth == 2 && biggeryear % 4 != 0) {
periodmonth = periodmonth * 29;
}
}
if (periodyear < 0) {
if (year1 % 4 != 0 || year2 % 4 != 0) {
periodyear = year2 - year1;
periodyear *= 366;
}
else {
periodyear = year2 - year1;
periodyear *= 365;
}
}
else if (periodyear >= 0) {
if (year1 % 4 != 0 || year2 % 4 != 0) {
periodyear *= 366;
}
else {
periodyear *= 365;
}
}
periodindays = periodday + periodmonth + periodyear;
return abs(periodindays);
}
int main()
{
string daya = ("1"), montha = ("1"), yeara = ("2016"), yearb = ("2016"),
monthb = ("4"), dayb = ("27");
int difference;
difference = period(daya, montha, yeara, dayb, monthb, yearb);
cout << difference << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
我认为你做的事情有点复杂。我的建议是通过在struct tm中设置适当的字段并使用mktime()将日期转换为time_t值。然后,您可以使用difftime()来计算两个日期之间的秒数差异。