编写了个程序,返回两日期相差的天数不知为什么 始终为零(编程软件VS2015)
头文件:
class Date
{
private:
int year;
int mouth;
int day;
int leap(int);//判断指定的年份是否为闰年
intdton(Date &);//将指定日期转换成从0年O月O日起的天数
Datentod(int);//将指定的0年O月O日起的天数转换成对应的日期
public:
Date operator+(int days);//返回某日期加上天数得到的日期
Date operator-(int days);//返回某日期减去天数得到的日期
int operator-(Date&m);//返回两日期相差的天数
int day_tab[2][12] = { {31,28,31,30,3l,30,3l,3l,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31} };
//day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年
int show();//输入函数
int display();// 显示函数
};
源文件:
#include<iostream>
#include"tou.h"
using namespace std;
int Date::leap(int b)
{
if ((b % 400 == 0) || ((b % 100 != 0) && (b % 4 == 0)))//满足条件的是闰年
return 366;
else
return 365;
}
int Date::dton(Date &)
{
int a;
int i, j, m;
for (a = 0, i = 0;i < Date::year;i++)
{
m = leap(i);
a = a + m;
}
if (leap(Date::year) == 366)
{
for (j = 0;j < Date::mouth - 1;j++)
a = a + day_tab[1][j];
}
else
{
for (j = 0;j < Date::mouth - 1;j++)
a = a + day_tab[0][j];
}
a = a + Date::day;
return a;
}
Date Date::ntod(int a)
{
Date l;
int i, n, m;
int x = a;
for (i = 0, n = 0, m = 0;;i++)
{
m = m + leap(i);
if (m < a)
{
n++;
x = a - m;
}
else
break;
}
l.year = n;
int y, z, q, k;
k = x;
if (leap(Date::year) == 366)
for (y = 0, z = 1, q = 0;;q++)
{
y = y + Date::day_tab[1][q];
if (y < x)
{
z++;
k = x - y;
}
else
break;
}
else
for (y = 0, z = 1, q = 0;;q++)
{
y = y + Date::day_tab[0][q];
if (y < x)
{
z++;
k = x - y;
}
else
break;
}
l.mouth = z;
l.day = k;
return l;
}
Date Date::operator+(int days)
{
Date c;
int m;
m = dton(c) + days;
c=ntod(m);
return c;
}
Date Date::operator-(int days)
{
Date c;
int m;
m = dton(c)-days;
c=ntod(m);
return c;
}
int Date::operator-(Date&m)
{
Date a;
long p;
p =dton(a)-dton(m) ;
return p;
}
int Date::show()
{
int a, b, c;
cout << "please input the year:" << endl;
cin >> a;
cout << "please input the mouth:" << endl;
cin >> b;
cout << "please input the day:" << endl;
cin >> c;
Date::year = a;
Date::mouth = b;
Date::day = c;
return 0;
}
int Date::display()
{
cout << Date::year << "年" << Date::mouth << "月" << Date::day << "日" << endl;
return 0;
}
int main()
{
Date a, m;
int b, c, n;
cout << "welcome to the time machine:" << endl;
a.show();
a.display();
cout << "日期加天数选1 " << "日期减天数选2 " << "两日期相减选3 " << "退出选4" << endl;
am:cout << "please select" << endl;
cin >> c;
switch (c)
{
case 1:cout << "please input days" << endl;
cin >> b;
a = a + b;a.display();break;
case 2:
cout << "please input days" << endl;
cin >> b;
a = a - b;a.display();break;
case 3:m.show();n = a - m;cout << n << "天" << endl;break;
case 4:break;
}
cout << "继续 y/退出 n " << endl;
getchar();
if (getchar() == 'y')
goto am;
return 0;
}
头文件:
class Date
{
private:
int year;
int mouth;
int day;
int leap(int);//判断指定的年份是否为闰年
intdton(Date &);//将指定日期转换成从0年O月O日起的天数
Datentod(int);//将指定的0年O月O日起的天数转换成对应的日期
public:
Date operator+(int days);//返回某日期加上天数得到的日期
Date operator-(int days);//返回某日期减去天数得到的日期
int operator-(Date&m);//返回两日期相差的天数
int day_tab[2][12] = { {31,28,31,30,3l,30,3l,3l,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31} };
//day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年
int show();//输入函数
int display();// 显示函数
};
源文件:
#include<iostream>
#include"tou.h"
using namespace std;
int Date::leap(int b)
{
if ((b % 400 == 0) || ((b % 100 != 0) && (b % 4 == 0)))//满足条件的是闰年
return 366;
else
return 365;
}
int Date::dton(Date &)
{
int a;
int i, j, m;
for (a = 0, i = 0;i < Date::year;i++)
{
m = leap(i);
a = a + m;
}
if (leap(Date::year) == 366)
{
for (j = 0;j < Date::mouth - 1;j++)
a = a + day_tab[1][j];
}
else
{
for (j = 0;j < Date::mouth - 1;j++)
a = a + day_tab[0][j];
}
a = a + Date::day;
return a;
}
Date Date::ntod(int a)
{
Date l;
int i, n, m;
int x = a;
for (i = 0, n = 0, m = 0;;i++)
{
m = m + leap(i);
if (m < a)
{
n++;
x = a - m;
}
else
break;
}
l.year = n;
int y, z, q, k;
k = x;
if (leap(Date::year) == 366)
for (y = 0, z = 1, q = 0;;q++)
{
y = y + Date::day_tab[1][q];
if (y < x)
{
z++;
k = x - y;
}
else
break;
}
else
for (y = 0, z = 1, q = 0;;q++)
{
y = y + Date::day_tab[0][q];
if (y < x)
{
z++;
k = x - y;
}
else
break;
}
l.mouth = z;
l.day = k;
return l;
}
Date Date::operator+(int days)
{
Date c;
int m;
m = dton(c) + days;
c=ntod(m);
return c;
}
Date Date::operator-(int days)
{
Date c;
int m;
m = dton(c)-days;
c=ntod(m);
return c;
}
int Date::operator-(Date&m)
{
Date a;
long p;
p =dton(a)-dton(m) ;
return p;
}
int Date::show()
{
int a, b, c;
cout << "please input the year:" << endl;
cin >> a;
cout << "please input the mouth:" << endl;
cin >> b;
cout << "please input the day:" << endl;
cin >> c;
Date::year = a;
Date::mouth = b;
Date::day = c;
return 0;
}
int Date::display()
{
cout << Date::year << "年" << Date::mouth << "月" << Date::day << "日" << endl;
return 0;
}
int main()
{
Date a, m;
int b, c, n;
cout << "welcome to the time machine:" << endl;
a.show();
a.display();
cout << "日期加天数选1 " << "日期减天数选2 " << "两日期相减选3 " << "退出选4" << endl;
am:cout << "please select" << endl;
cin >> c;
switch (c)
{
case 1:cout << "please input days" << endl;
cin >> b;
a = a + b;a.display();break;
case 2:
cout << "please input days" << endl;
cin >> b;
a = a - b;a.display();break;
case 3:m.show();n = a - m;cout << n << "天" << endl;break;
case 4:break;
}
cout << "继续 y/退出 n " << endl;
getchar();
if (getchar() == 'y')
goto am;
return 0;
}
