1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#ifndef DATE_H
#define DATE_H
#include <string>
static const std::string month_name[]
{
"dummy",
"Jan","Feb","Mar", "Apr","May","Jun",
"Jul","Aug","Sep", "Oct","Nov","Dec"
};
static const int days_in_month[]
{ -99, 31,28,31, 30,31,30, 31,31,30, 31,30,31 };
class Date
{
private:
int day;
int year;
int month;
public:
Date(int aMonth = 1, int aDay = 1, int aYear = 1901)
{
month = aMonth;
day = aDay;
year = aYear;
}
~Date(){};
std::string monthWord(int);
void showDate();
bool isValid();
};
#endif
//#include "Date.h"
#include <string>
#include <iostream>
#include <iomanip>
std::string Date::monthWord(int aMonth)
{
return month_name[aMonth];
}
bool Date::isValid()
{
if(year < 1900 or year > 2021)
return false;
if(month < 1 or month > 12)
return false;
if( day < 1 or day > days_in_month[month])
return false;
return true;
}
void Date::showDate()
{
std::cout << month << "/" << day << "/" << year << std::endl;
std::cout << monthWord(month) << " " << day << ", " << year << std::endl;
std::cout << day << "." << monthWord(month) << "." << year << std::endl;
std::cout
<< year << "."
<< std::setw(2) << std::setfill('0') << month << "."
<< std::setw(2) << std::setfill('0') << day << std::endl;
}
int main()
{
Date date1(4, 29, 2021);
date1.showDate();
Date date2;
date2.showDate();
Date date3(7,14,1987);
date3.showDate();
Date date4(4,31,1987);
if(date4.isValid())
date4.showDate();
else
std::cout << "*** Invalid date\n";
Date date5(12,12,2022);
if(date5.isValid())
date5.showDate();
else
std::cout << "*** Invalid date\n";
}
|