Date.h:::
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year, month, day;
private:
enum {YearBase = 1970};
enum {MonthBase = 1};
enum {DayBase = 1};
static int nDaysofMonth[13];
void LeapYearEnabled() const;
public:
Date(int y = YearBase, int m = MonthBase, int d = DayBase);
Date(const Date& d);
Date& operator = (const Date& rhs);
void SetDate(int y, int m, int d);
void Print() const;
static bool IsLeapYear(int y);
static int DaysOfThisYear(int y);
int Date2Days() const;
static Date Days2Date(int n);
string toString(string format_flag) const;
static Date Today();
int nthDay() const;
int nthWeek() const;
Date NextDate() const;
Date PrevDate() const;
int DiffDate(const Date& d) const;
Date AfterNDays(int n) const;
bool Equal(const Date& d) const;
bool Earlier(const Date& d) const;
};
#endif
Date.cpp:::
#include <iostream>
#include <stdio.h>
#include <cassert>
#include <time.h>
#include “Date.h”
using namespace std;
int Date::nDaysofMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int y, int m, int d) {
SetDate(y, m, d);
}
Date::Date(const Date& d) {
if(this != &d) {
year = d.year;
month = d.month;
day = d.day;
}
}
Date& Date::operator = (const Date& rhs) {
if(this != &rhs) {
year = this->year;
month = this->month;
day = this->day;
}
return *this;
}
void Date::SetDate(int y, int m, int d) {
year = y >= YearBase ? y : YearBase;
month = m >= 1 && m <= 12 ? m : MonthBase;
this->LeapYearEnabled();
day = d >= 1 && d <= nDaysofMonth[month] ? d : DayBase;
}
void Date::LeapYearEnabled() const {
if(IsLeapYear(year))
nDaysofMonth[2] = 29;
else
nDaysofMonth[2] = 28;
}
void Date::Print() const {
cout << this->year << “-” << this->month
<< “-” << this->day << endl;
}
bool Date::IsLeapYear(int y) {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int Date::DaysOfThisYear(int y) {
if(IsLeapYear(y)) return 366;
else return 365;
}
int Date::Date2Days() const {
this->LeapYearEnabled();
int s = 0;
s += day;
for(int m = 1; m < month; ++m)
s += nDaysofMonth[m];
for(int y = YearBase; y < year; ++y)
s += DaysOfThisYear(y);
return s;
}
Date Date::Days2Date(int n) {
Date d;
int y = YearBase;
while(n >= DaysOfThisYear(y)) {
n -= DaysOfThisYear(y);
++y;
if(n == DaysOfThisYear(y))
break;
}
d.year = y;
d.LeapYearEnabled();
int m = 1;
while(n >= nDaysofMonth[m]) {
n -= nDaysofMonth[m];
++m;
if(n == nDaysofMonth[m])
break;
}
d.month = m;
d.day = n;
return d;
}
string Date::toString(string format_flag) const {
int length = format_flag.size();
char *a = new char;
if(length == 6) {sprintf(a, “%s/%s/%s”, month, day, year);}
if(length == 10) {sprintf(a, “%02s/%02s/%04s”, month, day, year);}
if(length == 12) {
switch(month) {
case 1 : sprintf(a, “January %02s,%04s”, day, year); break;
case 2 : sprintf(a, “Febrary %02s,%04s”, day, year); break;
case 3 : sprintf(a, “March %02s,%04s”, day, year); break;
case 4 : sprintf(a, “April %02s,%04s”, day, year); break;
case 5 : sprintf(a, “May %02s,%04s”, day, year); break;
case 6 : sprintf(a, “June %02s,%04s”, day, year); break;
case 7 : sprintf(a, “July %02s,%04s”, day, year); break;
case 8 : sprintf(a, “Augest %02s,%04s”, day, year); break;
case 9 : sprintf(a, “September %02s,%04s”, day, year); break;
case 10 : sprintf(a, “October %02s,%04s”, day, year); break;
case 11 : sprintf(a, “November %02s,%04s”, day, year); break;
case 12 : sprintf(a, “December %02s,%04s”, day, year); break;
}
}
return string((string)a);
}
Date Date::Today() {
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
return Date(t_tm->tm_year, t_tm->tm_mon, t_tm->tm_mday);
}
int Date::nthDay() const {
Date now;
now = now.Today();
LeapYearEnabled();
int sum = 0;
sum += day;
for(int m = 1; m <= month; m++)
sum += nDaysofMonth[m];
return sum;
}
int Date::nthWeek() const {
int sum(nthDay());
return (sum / 7 + 1);
}
Date Date::NextDate() const {
int sum(Date2Days() + 1);
return Days2Date(sum);
}
Date Date::PrevDate() const {
int sum(Date2Days() – 1);
return Days2Date(sum);
}
int Date::DiffDate(const Date& d) const {
int sum1(Date2Days());
int sum2(d.Date2Days());
return (sum1 – sum2) > 0 ? (sum1 – sum2) : (sum2 – sum1);
}
Date Date::AfterNDays(int n) const {
int sum(Date2Days() + n);
return Days2Date(sum);
}
bool Date::Equal(const Date& d) const {
if(year == d.day && month == d.month && day == d.day) return true;
else return false;
}
bool Date::Earlier(const Date& d) const {
if(year < d.year || (year == d.year && month < d.month) ||
(year == d.year && month == d.month && day < d.day)) return true;
else return false;
}
main.cpp:::
#include <iostream>
#include <string>
#include “Date.h”
using namespace std;
int main() {
Date d(2008, 8, 8);
d.Print();
cout << d.Date2Days() << endl;
d = Date::Days2Date(d.Date2Days());
d.Print();
d = Date::Days2Date(2000);
d.Print();
cout << d.toString(“MM/dd/yyyy”) <<endl;
cout << d.nthDay() << endl;
cout << d.nthWeek() <<endl;
d.NextDate().Print();
d.PrevDate().Print();
cout << d.DiffDate(Date::Today()) << endl;
d.AfterNDays(100).Print();
cout << d.Equal(Date::Today()) << endl;
cout << d.Earlier(Date::Today()) << endl;
}
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year, month, day;
private:
enum {YearBase = 1970};
enum {MonthBase = 1};
enum {DayBase = 1};
static int nDaysofMonth[13];
void LeapYearEnabled() const;
public:
Date(int y = YearBase, int m = MonthBase, int d = DayBase);
Date(const Date& d);
Date& operator = (const Date& rhs);
void SetDate(int y, int m, int d);
void Print() const;
static bool IsLeapYear(int y);
static int DaysOfThisYear(int y);
int Date2Days() const;
static Date Days2Date(int n);
string toString(string format_flag) const;
static Date Today();
int nthDay() const;
int nthWeek() const;
Date NextDate() const;
Date PrevDate() const;
int DiffDate(const Date& d) const;
Date AfterNDays(int n) const;
bool Equal(const Date& d) const;
bool Earlier(const Date& d) const;
};
#endif
Date.cpp:::
#include <iostream>
#include <stdio.h>
#include <cassert>
#include <time.h>
#include “Date.h”
using namespace std;
int Date::nDaysofMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int y, int m, int d) {
SetDate(y, m, d);
}
Date::Date(const Date& d) {
if(this != &d) {
year = d.year;
month = d.month;
day = d.day;
}
}
Date& Date::operator = (const Date& rhs) {
if(this != &rhs) {
year = this->year;
month = this->month;
day = this->day;
}
return *this;
}
void Date::SetDate(int y, int m, int d) {
year = y >= YearBase ? y : YearBase;
month = m >= 1 && m <= 12 ? m : MonthBase;
this->LeapYearEnabled();
day = d >= 1 && d <= nDaysofMonth[month] ? d : DayBase;
}
void Date::LeapYearEnabled() const {
if(IsLeapYear(year))
nDaysofMonth[2] = 29;
else
nDaysofMonth[2] = 28;
}
void Date::Print() const {
cout << this->year << “-” << this->month
<< “-” << this->day << endl;
}
bool Date::IsLeapYear(int y) {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int Date::DaysOfThisYear(int y) {
if(IsLeapYear(y)) return 366;
else return 365;
}
int Date::Date2Days() const {
this->LeapYearEnabled();
int s = 0;
s += day;
for(int m = 1; m < month; ++m)
s += nDaysofMonth[m];
for(int y = YearBase; y < year; ++y)
s += DaysOfThisYear(y);
return s;
}
Date Date::Days2Date(int n) {
Date d;
int y = YearBase;
while(n >= DaysOfThisYear(y)) {
n -= DaysOfThisYear(y);
++y;
if(n == DaysOfThisYear(y))
break;
}
d.year = y;
d.LeapYearEnabled();
int m = 1;
while(n >= nDaysofMonth[m]) {
n -= nDaysofMonth[m];
++m;
if(n == nDaysofMonth[m])
break;
}
d.month = m;
d.day = n;
return d;
}
string Date::toString(string format_flag) const {
int length = format_flag.size();
char *a = new char;
if(length == 6) {sprintf(a, “%s/%s/%s”, month, day, year);}
if(length == 10) {sprintf(a, “%02s/%02s/%04s”, month, day, year);}
if(length == 12) {
switch(month) {
case 1 : sprintf(a, “January %02s,%04s”, day, year); break;
case 2 : sprintf(a, “Febrary %02s,%04s”, day, year); break;
case 3 : sprintf(a, “March %02s,%04s”, day, year); break;
case 4 : sprintf(a, “April %02s,%04s”, day, year); break;
case 5 : sprintf(a, “May %02s,%04s”, day, year); break;
case 6 : sprintf(a, “June %02s,%04s”, day, year); break;
case 7 : sprintf(a, “July %02s,%04s”, day, year); break;
case 8 : sprintf(a, “Augest %02s,%04s”, day, year); break;
case 9 : sprintf(a, “September %02s,%04s”, day, year); break;
case 10 : sprintf(a, “October %02s,%04s”, day, year); break;
case 11 : sprintf(a, “November %02s,%04s”, day, year); break;
case 12 : sprintf(a, “December %02s,%04s”, day, year); break;
}
}
return string((string)a);
}
Date Date::Today() {
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
return Date(t_tm->tm_year, t_tm->tm_mon, t_tm->tm_mday);
}
int Date::nthDay() const {
Date now;
now = now.Today();
LeapYearEnabled();
int sum = 0;
sum += day;
for(int m = 1; m <= month; m++)
sum += nDaysofMonth[m];
return sum;
}
int Date::nthWeek() const {
int sum(nthDay());
return (sum / 7 + 1);
}
Date Date::NextDate() const {
int sum(Date2Days() + 1);
return Days2Date(sum);
}
Date Date::PrevDate() const {
int sum(Date2Days() – 1);
return Days2Date(sum);
}
int Date::DiffDate(const Date& d) const {
int sum1(Date2Days());
int sum2(d.Date2Days());
return (sum1 – sum2) > 0 ? (sum1 – sum2) : (sum2 – sum1);
}
Date Date::AfterNDays(int n) const {
int sum(Date2Days() + n);
return Days2Date(sum);
}
bool Date::Equal(const Date& d) const {
if(year == d.day && month == d.month && day == d.day) return true;
else return false;
}
bool Date::Earlier(const Date& d) const {
if(year < d.year || (year == d.year && month < d.month) ||
(year == d.year && month == d.month && day < d.day)) return true;
else return false;
}
main.cpp:::
#include <iostream>
#include <string>
#include “Date.h”
using namespace std;
int main() {
Date d(2008, 8, 8);
d.Print();
cout << d.Date2Days() << endl;
d = Date::Days2Date(d.Date2Days());
d.Print();
d = Date::Days2Date(2000);
d.Print();
cout << d.toString(“MM/dd/yyyy”) <<endl;
cout << d.nthDay() << endl;
cout << d.nthWeek() <<endl;
d.NextDate().Print();
d.PrevDate().Print();
cout << d.DiffDate(Date::Today()) << endl;
d.AfterNDays(100).Print();
cout << d.Equal(Date::Today()) << endl;
cout << d.Earlier(Date::Today()) << endl;
}
解决方案:2分
toString中这句char *a = new char;
应该是要分配一个数组吧
应该是要分配一个数组吧
解决方案:3分
代码功能归根结底不是别人帮本人看或讲解或注释出来的;而是被本人静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生本人领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
提醒:再牛×的老师也无法代替学生本人领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
解决方案:10分
第一个问题本人上面已经说了
可以这样写 char *a = new char[100];
第二个问题sprintf中格式为%s,后面根的参数确确是int型
把全部sprintf 中的有%s都用%d替换,如if(length == 10) {sprintf(a, “%02d/%02d/%04d”, month, day, year);}
可以这样写 char *a = new char[100];
第二个问题sprintf中格式为%s,后面根的参数确确是int型
把全部sprintf 中的有%s都用%d替换,如if(length == 10) {sprintf(a, “%02d/%02d/%04d”, month, day, year);}
解决方案:7分
不出错不代表是正确的,你假如只是一个char,存储的字符串就溢出了,要是后面根了比较重要的数据就可能引起程序崩溃或更严重后果
同时你程序没有delete动态分配的内存,假如你加delete,就会发现在释放时程序会崩溃,原因是你的字符串写到了其他数据了
同时你程序没有delete动态分配的内存,假如你加delete,就会发现在释放时程序会崩溃,原因是你的字符串写到了其他数据了
解决方案:3分
出错的时候点中断,不要点终止,通过调用堆栈及各变量值 看是哪里出了问题
解决方案:5分
time_t 这种类型就是用来存储从1970年到现在经过了多少秒,要获得年月日看此博文:
http://www.cnblogs.com/iluzhiyong/archive/2014/01/24/3531996.html
http://www.cnblogs.com/iluzhiyong/archive/2014/01/24/3531996.html