64,635
社区成员
发帖
与我相关
我的任务
分享
#ifndef _DATE_H
#define _DATE_H
#include <string>
class Date
{
public:
enum Month{jan=1,feb,apr,may,jun,jul,aug,sep,oct,nov,dec};
class Bad_date{};
Date(int dd=0,Month mm=Month(0),int yy=0);
void Date::string_rep()const;//输出
static void set_default(int,Month,int);
Date& add_year(int n);
Date& add_month(int n);
Date& add_day(int n);
bool leapyear(int y);
private:
int d,m,y;
static Date default_date;
};
#endif
//date-func.cpp:
#include "date.h"
#include <ctime>
Date::Date(int dd=1,Month mm=Month(1);int yy=1970)
{
int max;
switch (mm)
{
case feb:
max=28+leapyear(yy);
break;
case apr:
case jun:
case sep:
case nov:
max=30;
break;
case jan:
case mar:
case may:
case jul:
case aug:
case oct:
case dec:
max=31;
default:
throw Bad_date();
}
if (dd<1||max<dd) throw Bad_date();
y=yy;
d=dd;
m=mm;
}
Date& Date::add_year(int n)
{
if (d==29&&m==2&&!=leapyear(y+n))
{
d=1;
m=3;
}
y+=n;
return *this;
}
Date& Date::add_month(int n)
{
if (n==0)return *this;
if (n>0)
{
int delta_y=n/12;
int mm=m+n%12;
if (mm>12)
{
delta_y++;
mm-=12;
}
//处理month(m)没有天数的情况
y+=delta_y;
m=Month(mm);
return *this;
}
return *this;
}
Date& Date::add_day(int n)
{
if (n==0)return *this;
if (n>0)
{
std::tm tm;
tm.tm_sec=0;
tm.tm_min=0;
tm.tm_hour=12;
tm.tm_mday=1+days;
tm.tm_mon=1;
tm.tm_year=70;
std::mktime(&tm);
d=tm.tm_mday;
m=tm.tm_mon;
y=tm.tm_year;
return *this;
}
}
static void Date::set_default(int,Month,int)
{
Date::default_date=Date(d,m,y);
}
bool Date::leapyear(int y)
{
if (y%4==0&&y%100!=0||y%400==0)
return true;
else
return false;
}
void Date::string_rep()const
{
std::cout<<d<<'\t'<<m<<'\t'<<year<<std::endl;
}
//main.cpp:
#include <iostream>
#include "date.h"
using namespace std;
int main()
{
Date ddate(1,Date::Month(1),1970);
cout<<"starting:\n";
cout<<"a.加天数 b.加月数\n";
cout<<"c.加年数 d.退出\n";
char s;
int ld,lm,ly;
while (cin>>s)
{
switch (s)
{
case 'a':
cout<<"输入要加的天数:";
cin>>ld;
ddate.add_day(ld);
ddate.string_rep();
break;
case 'b':
cout<<"输入要加的月数:";
cin>>lm;
ddate.add_month(lm);
ddate.string_rep();
break;
case 'c':
cout<<"要输入的年数:";
cin>>ly;
ddate.add_year(ly);
ddate.string_rep();
break;
case 'd':
cout<<"bye";
exit(0);
default:
break;
}
}
system("pause");
return 0;
}
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
COleDateTime t;
COleDateTimeSpan ts;
CString s,fmt;
int nYear;
int nMonth;
int nDay;
int nHour;
int nMin;
int nSec;
int lDays;
int nHours;
int nMins;
int nSecs;
int i,N;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf("Fatal Error: MFC initialization failed\n");
return 1;
}
if (argc<13) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 2;
}
if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
else {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 3;
}
nYear =atoi(argv[ 1]);
nMonth=atoi(argv[ 2]);
nDay =atoi(argv[ 3]);
nHour =atoi(argv[ 4]);
nMin =atoi(argv[ 5]);
nSec =atoi(argv[ 6]);
lDays =atoi(argv[ 7]);
nHours=atoi(argv[ 8]);
nMins =atoi(argv[ 9]);
nSecs =atoi(argv[10]);
N =atoi(argv[11]);
if (N<=0) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 4;
}
t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
for (i=1;i<=N;i++) {
s=t.Format(fmt);
printf("%08d %s\n",i,s);
t=t+ts;
}
return 0;
}
//date.h
#ifndef _DATE_H
#define _DATE_H
#include <string>
class Date
{
public:
enum Month{jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};//漏了mar。。
class Bad_date{};
Date(int dd=0,Month mm=Month(0),int yy=0);
void Date::string_rep()const;//输出
static void set_default(int,Month,int);
Date& add_year(int n);
Date& add_month(int n);
Date& add_day(int n);
bool leapyear(int y);
private:
int d,m,y;
static Date default_date;
};
#endif
//date-func.cpp:
#include "date.h"
#include <iostream>
#include <ctime>
Date Date::default_date;//要在类定义外定义static变量。。
Date::Date(int dd,Month mm,int yy)//这里是,不是; 还有默认参数在声明或定义的其中一处出现即可。。
{
int max;
switch (mm)
{
case feb:
max=28+leapyear(yy);
break;
case apr:
case jun:
case sep:
case nov:
max=30;
break;
case jan:
case mar:
case may:
case jul:
case aug:
case oct:
case dec:
max=31;
default:
throw Bad_date();
}
if (dd<1||max<dd) throw Bad_date();
y=yy;
d=dd;
m=mm;
}
Date& Date::add_year(int n)
{
if (d==29&&m==2&&!leapyear(y+n))//多了个=
{
d=1;
m=3;
}
y+=n;
return *this;
}
Date& Date::add_month(int n)
{
if (n==0)return *this;
if (n>0)
{
int delta_y=n/12;
int mm=m+n%12;
if (mm>12)
{
delta_y++;
mm-=12;
}
//处理month(m)没有天数的情况
y+=delta_y;
m=Month(mm);
return *this;
}
return *this;
}
Date& Date::add_day(int n)
{
if (n==0)return *this;
if (n>0)
{
std::tm tm;
tm.tm_sec=0;
tm.tm_min=0;
tm.tm_hour=12;
tm.tm_mday=1;//+days;
tm.tm_mon=1;
tm.tm_year=70;
std::mktime(&tm);
d=tm.tm_mday;
m=tm.tm_mon;
y=tm.tm_year;
return *this;
}
}
void Date::set_default(int d,Month m,int y)//static只在声明中出现即可。。还有缺少形参名字。。
{
Date::default_date=Date(d,m,y);
}
bool Date::leapyear(int y)
{
if (y%4==0&&y%100!=0||y%400==0)
return true;
else
return false;
}
void Date::string_rep()const
{
std::cout<<d<<'\t'<<m<<'\t'<<y<<std::endl;//这里是y不是year。。
}
//main.cpp:
#include <iostream>
#include "data.h"
using namespace std;
int main()
{
Date ddate(1,Date::Month(1),1970);
cout<<"starting:\n";
cout<<"a.加天数 b.加月数\n";
cout<<"c.加年数 d.退出\n";
char s;
int ld,lm,ly;
while (cin>>s)
{
switch (s)
{
case 'a':
cout<<"输入要加的天数:";
cin>>ld;
ddate.add_day(ld);
ddate.string_rep();
break;
case 'b':
cout<<"输入要加的月数:";
cin>>lm;
ddate.add_month(lm);
ddate.string_rep();
break;
case 'c':
cout<<"要输入的年数:";
cin>>ly;
ddate.add_year(ly);
ddate.string_rep();
break;
case 'd':
cout<<"bye";
exit(0);
default:
break;
}
}
system("pause");
return 0;
}