0
我试图做一个函数,读取两个日期,并选择较大的一个,然后获得天差异。到目前为止,我尝试了很多,但有时候答案是不正确的。
我找不到问题所在。任何人都可以帮助我找到问题,并且/或者就如何调试这类问题提供一些建议?两个日期之间的c + +日期差异
#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;
}
这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
编辑您的文章以获得正确的缩进 - 或者甚至没有人会在你的网站上认真对待大声笑。如果你这样做,我可以看看发生了什么。 – 2016-04-27 16:43:50
添加其失败并为其传递的输入。 –