This is homework from one MOOC(no requirement of forbidding paste codes online). The title gave me a framework:
class Date
{
int month;
int day;
int year;
};
And it asks me to implement operator<()
, operator>()
, operator==()
, as well as print()
and sort()
.
Below codes is my implementation:
/*=============================================================*/
//date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
class Date
{
int month;
int day;
int year;
static char colon;
public:
Date(int m, int d, int y) : month(m), day{d}, year{y} {}
bool operator>(Date const& another) const { return this->magic() > another.magic(); };
bool operator<(Date const& another) const { return this->magic() < another.magic(); }
bool operator==(Date const& another) const { return this->magic() == another.magic(); }
void print() const { std::cout << year << colon<< month << colon << day << std::endl; }
long int magic() const { return static_cast<long int>(month * 100 + day + year * 10000); }
};
#endif // !DATE_H
/*=============================================================*/
//date.cpp
#include "stdafx.h"
#include "date.h"
char Date::colon = '-';
/*=============================================================*/
//extras.hh
#ifndef EXTRAS_HH
#define EXTRAS_HH
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "date.h"
#include "extras.hxx"
inline void description();
inline void checkValid(int month, int day, int year);
inline auto generateDate();
inline void Sort(std::vector<Date>& v);
inline void print(std::vector<Date>& Dates);
#endif
/*=============================================================*/
//extras.hxx
//To use inline and seperate inline functions' declaration and definition
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "date.h"
struct InvalidDate {};
inline void description()
{
std::cout
<< "Request: " << std::endl
<< " (1): CreatePoints: generates Dates;" << std::endl
<< " (2): Sort: sort the dates" << std::endl
<< "I will check if dates generated by rand() is valid, such as LeapYear"
<< std::endl
<< std::endl;
}
inline void checkValid(const int month, const int day, const int year) noexcept(false)
{
if (day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0)
{
std::cout << month << "/" << day << "/" << year << ": " << "day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0 is not valid? " << std::endl;
throw InvalidDate();
return;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
{
std::cout << month << "/" << day << "/" << year << ": " << "it is a solar month of 30 days" << std::endl;
throw InvalidDate();
}
if (month == 2)
{
if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0)
{
if (day > 29)
{
std::cout << month << "/" << day << "/" << year << ": " << "is LeapYear!!" << std::endl;
throw InvalidDate();
return;
}
}
else
{
if (day > 28)
{
std::cout << month << "/" << day << "/" << year << ": " << "is LeapYear!!" << std::endl;
throw InvalidDate();
return;
}
}
}
}
inline auto generateDate() noexcept(false)
{
std::srand(std::time(0));
std::vector<Date> ans;
int month = 0, day =0, year = 0;
for (auto i = 0; i < 10; i++)
{
month = std::rand() % 15, day = std::rand() % 35, year = std::rand() % 10000;
while (true)
{
try
{
checkValid(month, day, year);
}
catch (InvalidDate)
{
month = std::rand() % 15, day = std::rand() % 35, year = std::rand() % 10000;
continue;
}
break;
}
ans.push_back(Date( month, day, year ));
}
return ans;
}
inline void Sort(std::vector<Date>& v)
{
std::sort(std::begin(v), std::end(v), [](Date const& a, Date const& b) { return a < b; });
}
inline void print(std::vector<Date>& dates)
{
for (auto date : dates) date.print();
}
/*=============================================================*/
//Main.cpp
#include "stdafx.h"
#include <iostream>
#include "extras.hh"
#include "date.h"
int main()
{
description();
std::vector<Date> dates = generateDate();
Sort(dates);
print(dates);
}
Here is a sample output in console
Request:
(1): CreatePoints: generates Dates;
(2): Sort: sort the dates
I will check if dates generated by rand() is valid, such as LeapYear
8/0/3339: day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0 is not valid?
14/33/2889: day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0 is not valid?
0/19/3260: day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0 is not valid?
0/12/8402: day > 31 || month > 12 || day <= 0 || month <= 0 || year < 0 is not valid?
778-8-6
1679-12-15
2029-8-28
2614-5-10
4645-6-2
4913-12-3
5253-3-7
8024-4-3
9589-5-26
9632-11-4
Press any key to continue . . .
How can I enhance these codes? Thanks in advance.