缘起
感觉公选、马原什么的快要交读书报告了,但是又不知道现在第几周,然而自己去数,好麻烦哦……于是就想做个工具来做这件事。
恰好上个学期打过一个Date类,拿来用正好。
演示
编译运行:
可以看到当前是第十周。
源代码
可以到我的github https://github.com/LoHiaufung/Interesting.git下载,weeks文件夹。
共有三个文件HowManyWeeks.cpp、Date.h、 Date.cpp,分别贴如下。
// HowManyWeeks.cpp
#include <time.h>
#include <iostream>
#include "Date.h"
using namespace std;
int main( void )
{
time_t t = time(0);
tm* curTime = localtime(&t);
int curYear = curTime -> tm_year + 1900;
int curMon = curTime -> tm_mon + 1;
int curDay = curTime -> tm_mday;
// current date
Date curDate(curYear, curMon, curDay);
// the date when the current term begin
Date beginDate(2016, 8, 29);
cout << "The term begin on:" << beginDate.toString() << endl;
int diff = 0;
while (curDate != beginDate) {beginDate++; diff++;}
cout << "Today is in the " << (diff / 7)+1 << "th week";
return 0;
}
// date.h
#include<iostream>
#include<string>
using namespace std;
class Date {
public:
Date(int = 0, int = 0 , int = 0);
Date(const Date&);
~Date();
std::string toString() const;
Date after(int day);
int& operator[] (int index);
friend ostream& operator<<(ostream& out, const Date&);
friend istream& operator>>(istream& in , Date&);
bool operator< (const Date&);
bool operator>(const Date& in);
bool operator==(const Date& in);
bool operator!=(const Date& in);
Date& operator= (const Date&);
Date& operator++(); // ++x
Date operator++(int); // x++
void setDay(const int&);
bool isValid();
private:
int max_date(int y, int m);
int year;
int month;
int day;
};
// date.cpp
#include<iostream>
#include<sstream>
#include"Date.h"
using namespace std;
/*
private:
int year;
int month;
int day;
*/
Date::Date(int y, int m, int d) {year = y; month = m; day = d;}
Date::Date(const Date& in) {
year = in.year;
month = in.month;
day = in.day;
}
Date::~Date() {
// cout << "Destructor: " << toString() <<endl;
}
std::string Date::toString() const {
stringstream out;
out << "[" << year << "-" << month << "-" << day << "]";
return out.str();
}
Date Date::after(int day) {
Date ans = *this;
while (day--) ++ans;
return ans;
}
int& Date::operator[] (int index) {
if (index == 0) return year;
else if (index == 1) return month;
else return day;
}
bool Date::operator< (const Date& other) {
if (year < other.year) {
return true;
} else if (year > other.year) {
return false;
} else {
if (month < other.month) {
return true;
} else if (month > other.month) {
return false;
} else {
if (day < other.day)
return true;
return false;
}
}
}
bool Date::operator>(const Date& other) {
if (year == other.year && month == other.month
&& day == other.day) return false;
return !((*this) < other);
}
bool Date::operator==(const Date& other) {
if (year == other.year && month == other.month
&& day == other.day) return true;
return false;
}
bool Date::operator!=(const Date& in) {
return !((*this) == in);
}
Date& Date::operator= (const Date& in) {
year = in.year;
month = in.month;
day = in.day;
return *this;
}
Date& Date::operator++() {
day++;
if (isValid()) {
return *this;
} else {
int temp = max_date(year, month);
month += day / temp;
day %= temp;
while (month > 12) { year++; month -= 12;}
}
return *this;
} // ++x
Date Date::operator++(int n) {
Date temp = *this;
++(*this);
return temp;
} // x++
void Date::setDay(const int& in) {
day = in;
}
bool Date::isValid() {
if (day <= max_date(year, month) && day >= 1
&& month >= 1 && month <= 12 && year > 0)
return true;
return false;
}
ostream& operator<<(ostream& out, const Date& obj) {
out << obj.toString()<< endl;
return out;
}
istream& operator>>(istream& in , Date& obj) {
int temp = 0;
in >> temp; obj.year = temp;
in >> temp; obj.month = temp;
in >> temp; obj.day = temp;
return in;
}
int Date::max_date(int y, int m) {
if (m != 2) {
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
return 31;
} else {
if ((y % 100 != 0 && y % 4 == 0) ||
(y % 100 == 0 && y % 400 == 0)) // 闰年
return 29;
return 28;
}
}