帮忙看下下面代码的问题 课程设计
主要是功能2有问题
图片如下:
#include<iostream>
#include<time.h>
#include<string>
using namespace std;
class DateFormatter //日期格式
{
private:
int day;
int mon;
int year;
public:
DateFormatter();
DateFormatter(int day,int mon,int year);
DateFormatter(char *date);
inline int GetDay(){return day;} //返回日期
inline int GetMonth(){return mon;} //返回月份
inline int GetYear(){return year;} //返回年份
inline void SetDay(int d){day=d;} //设置日期
inline void SetMonth(int m){mon=m;} //设置月份
inline void SetYear(int y){year=y;} //设置年份
inline bool IsLeapYear() //查看是否是闰年
{
float rem=year%4;
if(rem==0.0)
{
return true;
}
else
{
return false;
}
}
void AddDays (int days); //在日期上加上天数
void IncrementMonth(); //在日期上加上月份
char*GetDate();
int FindDifferenceInMonths(DateFormatter); //返回两个日期间月份的差值
int FindDifferenceInDays (DateFormatter); //返回两个日期间天数的差值
static char* Today() //返回当前系统时间的字符串
{
const time_t curr=time(0);
tm *dt=localtime(&curr);
int d=dt->tm_mday;
int y=(dt->tm_year+1900);
int m=dt->tm_mon;
char mn[4];
strcpy(mn,FindMonth(m));
char ret[25];
strcpy(ret,FormatDate(d,mn,y));
return ret;
}
char*GetFormattedDate();
DateFormatter operator + (const int&); //在形参前面加上const
DateFormatter operator - (const int&); //在形参前面加上const
DateFormatter operator ++ (int);
DateFormatter operator -- (int);
int operator<(DateFormatter df);
int operator>(DateFormatter df);
int operator==(DateFormatter df);
int operator - (DateFormatter& df); //在形参前加上&运算符
private:
static char*FindMonth(int m) //根据月份返回表示月份的字符串
{
if(m==0)
{
return "JAN";
}
else if(m==1)
{
return "FEB";
}
else if(m==2)
{
return "MAR";
}
else if(m==3)
{
return "APR";
}
else if(m==4)
{
return "MAY";
}
else if(m==5)
{
return "JUN";
}
else if(m==6)
{
return "JUL";
}
else if(m==7)
{
return "AUG";
}
else if(m==8)
{
return "SEP";
}
else if(m==9)
{
return "OCT";
}
else if(m==10)
{
return "NOV";
}
else if(m==11)
{
return "DEC";
}
else
{
return "NUL";
}
}
int FindMonthInNumber(string m); //将用字符串表示的月份形式转换为数字形式
static char*FormatDate(int d,char *m,int y) //将时间用字符串形式表示
{
char* dy=new char[25];
char* yr=new char[25];
dy=itoa(d,dy,10);
yr=itoa(y,yr,10);
strcat(dy,"-");
strcat(dy,m);
strcat(dy,"-");
strcat(dy,yr);
return dy;
}
string to_lower(string s); //将字符串中所有字母转换为小写
int FindDaysInMonth(int m) //返回月份中的最大天数
{
if(m==0)
{
return 31;
}
else if(m==1)
{
if(IsLeapYear())
{
return 29;
}
else
{
return 28;
}
}
else if(m==2)
{
return 31;
}
else if(m==3)
{
return 30;
}
else if(m==4)
{
return 31;
}
else if(m==5)
{
return 30;
}
else if(m==6)
{
return 31;
}
else if(m==7)
{
return 31;
}
else if(m==8)
{
return 30;
}
else if(m==9)
{
return 31;
}
else if(m==10)
{
return 30;
}
else if(m==11)
{
return 31;
}
return -1;
}
};
DateFormatter::DateFormatter() //构造函数,将成员数据赋值为当前系统时间
{
const time_t curr=time(0);
tm *dt=localtime(&curr);
this->day=dt->tm_mday;
this->mon=dt->tm_mon;
this->year=(dt->tm_year+1900);
}
DateFormatter::DateFormatter(int day,int mon,int year) //构造函数,用指定时间设置成员函数
{
this->day=day;
this->mon=mon;
this->year=year;
}
DateFormatter::DateFormatter(char *date) /*构造函数,将用字符串表示的日期形式还原成整型,可以识别诸如1/1/03 1-12-2003等形式的字符串*/
{
if(strlen(date)>8)
{
string s1=date;
int p=s1.find_first_of('-'); //找到第一个‘-’,如12-2-2003
string day;
string month;
string year;
if(p==2)
{
day=s1.substr(0,2); //取出日期
month=s1.substr(3,3); //取出月份
year=s1.substr(7,4); //取出年份
}
else if(p==1)
{
day=s1.substr(0,1);
month=s1.substr(2,3);
year=s1.substr(6,4);
}
char cday[5];
strcpy(cday,day.c_str()); //日期转换为字符串
char cyear[5];
strcpy(cyear,year.c_str());
this->day=atoi(cday); //再将字符串转换为整型数字
this->mon=FindMonthInNumber(month);
this->year=atoi(cyear);
}
else if(strlen(date)==8||strlen(date)==7||strlen(date)==6)
{
int len=strlen(date);
string str=date;
string day;
string month;
string year;
int p1=str.find_first_of('-');
if(p1<=0) p1=str.find_first_of('/'); //如果格式中无‘-’,寻找‘/’
int p2=str.find_last_of('-');
if(p2<=0) p2=str.find_last_of('/');
if(len==8)
{
day=str.substr(0,2); //同样将年、月、日转换成整型数字
month=str.substr(3,2);
year=str.substr(6,2);
}
else if(len==7)
{
if(p1==1)
{
day=str.substr(0,1);
if(p2==3)
{
month=str.substr(2,1);
year=str.substr(4,2);
}
else
{
month=str.substr(2,2);
year=str.substr(5,2);
}
}
else
{
day=str.substr(0,2);
if(p2==3)
{
month=str.substr(2,1);
year=str.substr(4,2);
}
else
{
month=str.substr(3,2);
year=str.substr(5,2);
}
}
}
else if(len==6)
{
day=str.substr(0,1);
month=str.substr(2,1);
year=str.substr(4,2);
}
int m=atoi((month.c_str()));
m-=1;
int yr=atoi((year.c_str()));
char y[5];
if(yr>=0&&yr<=70) //再将年份前面补上世纪
{
strcpy(y,"20");
strcat(y,year.c_str());
}
else
{
strcpy(y,"19");
strcat(y,year.c_str());
}
this->day=atoi((day.c_str()));
this->mon=m;
this->year=atoi(y);
}
}
int DateFormatter::FindMonthInNumber(string m) //识别月份的英文形式,还原成整数
{
if(to_lower(m)=="jan")
{
return 0;
}
else if(to_lower(m)=="feb")
{
return 1;
}
else if(to_lower(m)=="mar")
{
return 2;
}
else if(to_lower(m)=="apr")
{
return 3;
}
else if(to_lower(m)=="may")
{
return 4;
}
else if(to_lower(m)=="jun")
{
return 5;
}
else if(to_lower(m)=="jul")
{
return 6;
}
else if(to_lower(m)=="aug")
{
return 7;
}
else if(to_lower(m)=="sep")
{
return 8;
}
else if(to_lower(m)=="oct")
{
return 9;
}
else if(to_lower(m)=="nov")
{
return 10;
}
else if(to_lower(m)=="dec")
{
return 11;
}
else
{
return -1;
}
}
string DateFormatter::to_lower(string s) //将字符串中的大写转换成小写
{
int len=s.length();
for(int i=0;i<len;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
s[i]=tolower(s[i]);
}
}
return s;
}
char *DateFormatter::GetDate() //将以整型表示的日期转换为字符串的形式
{
char *d=new char[25];
char *m=new char[5];
char *y=new char[5];
d=itoa(day,d,10);
m=itoa((mon+1),m,10);
y=itoa(year,y,10);
strcat(d,"/");
strcat(d,m);
strcat(d,"/");
strcat(d,y);
char ret[25];
strcpy(ret,d);
delete[] d;
delete[] m;
delete[] y;
return ret;
}
char *DateFormatter::GetFormattedDate() //将用整数表示的时间改用字符串表示
{
char mn[5];
strcpy(mn,FindMonth(mon));
char ret[25];
strcpy(ret,FormatDate(day,mn,year));
return ret;
}
void DateFormatter::AddDays(int days) //日期加上天数成为一个新的日期,递归计算
{
if(day<=0) return;
int curr_day=this->day; //取对象当前日期
int curr_mon=this->mon; //取对象当前月份
int days_in_mon=FindDaysInMonth(curr_mon); //当前月份共有多少天数
if(this->mon==11&&days_in_mon==curr_day) //当月份为12月且当前日期为31天时
{
this->year++; //对象年份加1
}
if(days_in_mon==curr_day) //当前日期计满时
{
this->mon++; //当前月份加1
this->day=0; //日期清零
curr_day=0; //当前日期清零(可省略)
if(this->mon>11) //当前月份为12月,月份变为1月
{ this->mon=0; }
days_in_mon=FindDaysInMonth(this->mon); //新月份的天数
}
if(days<=days_in_mon) //相加的日期数小于新月份的天数
{
this->day+=days; //当前日期加上新天数
if(this->day>days_in_mon) //若大于当前月份应有的天数
{
this->mon++; //当前月份加1
if(this->mon>11) //当前月份为12月,月份变为1月
{ this->mon=0; }
this->day-=days_in_mon; //当前天数标准化
}
return;
}
else //当相加的天数大于当前月份的天数时
{
int days_to_add=days_in_mon-curr_day; //计算补满月份还需要的天数
this->day+=days_to_add; //补满当前月份的天数
if(this->day>days_in_mon) //天数多余当前月份
{
this->day=1; //为第一号
}
int rem_days=days-days_to_add; //求出剩余的天数
AddDays(rem_days); //递归计算
return;
}
}
DateFormatter DateFormatter::operator + (const int &days)
{
if(days <= 0) {cout<<"天数不为正,错误\n"; return *this;}
int curr_mon=this->mon;
int d=this->day+days; //把当前日期加上输入的天数赋给另一个变量保存起来
int days_in_mon = FindDaysInMonth(curr_mon);//当前月份共有多少天数
if(d>days_in_mon)
{
while(d/days_in_mon>=1)
{
d=d-days_in_mon;
if(d==0){d=days_in_mon;break;}
this->mon=this->mon+1;
days_in_mon = FindDaysInMonth(this->mon);
if(this->mon>11) {this->year=this->year+1;this->mon=this->mon%12;days_in_mon = FindDaysInMonth(this->mon);}
}
this->day=d;
}
else this->day=d;
if(this->mon>11)
{
this->year=this->year+this->mon/12;
this->mon=this->mon%12;
if(this->mon==0) this->mon=12;
}
return *this;
}
DateFormatter DateFormatter::operator - (const int &days)
{
if(days<=0){cout<<"天数不为正,错误\n";return *this;}
int curr_mon=this->mon;
int d=this->day-days;
int days_in_mon=FindDaysInMonth(curr_mon);
if(d<=0)
{
d=d+days_in_mon;
if(d==0)
{
this->mon=mon-1;
if(this->mon>11){
this->year=this->year-1;this->mon=this->mon%12;
}
d=days_in_mon;
}
else {
days_in_mon=FindDaysInMonth(this->mon);
if(this->mon>11){this->year=this->year-1;this->mon=this->mon%12;}
}
this->day=d;
}
else d=day-days;
this->day=d;
}
void DateFormatter::IncrementMonth() //月份加1
{
mon++;
if(mon>11)
{
mon=0;
year++;
}
}