求解“下一天”的日期
这是我写的 不知道语法有没有错误.. 也不知道能不能运行 (今天还没有装上C++)
不过肯定有更好的方法
请高手们不吝赐教
[解决办法]
#include <ctime>
#include <iostream>
using namespace std;
class YearMonDay
{
public: YearMonDay();
YearMonDay(int a,int b,int c){Y=a;M=b;D=c;}
YearMonDay& Year(int x){Y=x;return *this;}
YearMonDay& Month(int x){M=x;return *this;}
YearMonDay& Day(int x){D=x;return *this;}
int Year(){return Y;}
int Month(){return M;}
int Day(){return D;}
bool Valid() const;
static int MaxDay(int,int);
YearMonDay& operator ++();
YearMonDay operator ++(int);
YearMonDay& operator -- ();
YearMonDay operator -- (int);
bool operator <(const YearMonDay&) const;
bool operator==(const YearMonDay&) const;
bool operator <=(const YearMonDay& rhs) const
{
return *this <rhs || *this==rhs;
}
private:
int Y,M,D;
};
int YearMonDay::MaxDay(int year,int Mon)
{
int month[12]={31,0,31,30,31,30,31,31,30,31,30,31};
if(year%4==0 && year%100!=0 || year%400==0)
month[1]=29;
else month[1]=28;
if(Mon> =1 && Mon <=12) return month[Mon-1];
else return 0;
}
YearMonDay::YearMonDay()
{
using namespace std;
time_t timer;
tm *tblock;
timer = time(0);
tblock = localtime(&timer);
Y=tblock-> tm_year+1900;
M=tblock-> tm_mon+1;
D=tblock-> tm_mday;
}
bool YearMonDay::Valid() const
{
if(Y> 0 && M> =1 && M <=12 && D> =1) return D <=MaxDay(Y,M);
return false;
}
YearMonDay& YearMonDay::operator ++()
{
if(D> =YearMonDay::MaxDay(Y,M)){
++M;
D=1;
if(M==13){
++Y;
M=1;
}
}
else ++D;
return *this;
}
YearMonDay YearMonDay::operator ++(int)
{
YearMonDay T(*this);
++ *this;
return T;
}
YearMonDay& YearMonDay::operator --()
{
if(D==1){
--M;
if(M==0){
M=12;
--Y;
}
D=YearMonDay::MaxDay(Y,M);
}
else --D;
return *this;
}
YearMonDay YearMonDay::operator --(int)
{
YearMonDay T(*this);
-- *this;
return T;
}
bool YearMonDay::operator <(const YearMonDay& rhs) const
{
bool YN;
if(Y <rhs.Y) YN=true;
else if(rhs.Y <Y) YN=false;
else if(M <rhs.M) YN=true;
else if(rhs.M <M) YN=false;
else YN=D <rhs.D;
return YN;
}
bool YearMonDay::operator==(const YearMonDay& rhs) const
{
return Y==rhs.Y && M==rhs.M && D==rhs.D;
}
int main()
{
YearMonDay today;
cout < < "today: " < <today.Year() < < '- ' < <today.Month() < < '- ' < <today.Day() < < '\n ';
for(int i=0;i <30;++i){
++today;
cout < < "nextday: " < <today.Year() < < '- ' < <today.Month() < < '- ' < <today.Day() < < '\n ';
}
}
G:\test> ymd
today:2007-9-7
nextday:2007-9-8
nextday:2007-9-9
nextday:2007-9-10
nextday:2007-9-11
nextday:2007-9-12
nextday:2007-9-13
nextday:2007-9-14
nextday:2007-9-15
nextday:2007-9-16
nextday:2007-9-17
nextday:2007-9-18
nextday:2007-9-19
nextday:2007-9-20
nextday:2007-9-21
nextday:2007-9-22
nextday:2007-9-23
nextday:2007-9-24
nextday:2007-9-25
nextday:2007-9-26
nextday:2007-9-27
nextday:2007-9-28
nextday:2007-9-29
nextday:2007-9-30
nextday:2007-10-1
nextday:2007-10-2
nextday:2007-10-3
nextday:2007-10-4
nextday:2007-10-5
nextday:2007-10-6
nextday:2007-10-7
G:\test>
[解决办法]
#include "stdafx.h "
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char* argv[])
{
time_t ltime;
tm* ntime;
time(<ime);
ntime = localtime(<ime);
char buff[30] = {0};
strftime(buff,30, "%Y-%m-%d ",ntime);
cout < < "today is " < <buff < <endl;
for(int i = 1;i <30;i++)
{
ltime+=86400;
ntime=localtime(<ime);
strftime(buff,30, "%Y-%m-%d ",ntime);
cout < < "next day is " < <buff < <endl;
}
return 0;
}
today is 2007-09-08
next day is 2007-09-09
next day is 2007-09-10
next day is 2007-09-11
next day is 2007-09-12
next day is 2007-09-13
next day is 2007-09-14
next day is 2007-09-15
next day is 2007-09-16
next day is 2007-09-17
next day is 2007-09-18
next day is 2007-09-19
next day is 2007-09-20
next day is 2007-09-21
next day is 2007-09-22
next day is 2007-09-23
next day is 2007-09-24
next day is 2007-09-25
next day is 2007-09-26
next day is 2007-09-27
next day is 2007-09-28
next day is 2007-09-29
next day is 2007-09-30
next day is 2007-10-01
next day is 2007-10-02
next day is 2007-10-03
next day is 2007-10-04
next day is 2007-10-05
next day is 2007-10-06
next day is 2007-10-07