answersLogoWhite

0

#include<iostream>

#include<sstream>

#include<chrono>

#include<ctime>

std::string months[12] { "January", "February", "March", "April", "May", "June", "July",

"August", "September", "October", "November", "December"};

class date

{

public:

date (std::string = "");

date (size_t dd, size_t mm, size_t yyyy);

date (const date& dt): d{dt.d}, m{dt.m}, y{dt.y} {}

date (date&& dt): d{dt.d}, m{dt.m}, y{dt.y} {}

date& operator= (const date& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }

date& operator= (date&& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }

static bool is_leap_year (size_t year);

static date today ();

static bool validate (size_t dd, size_t mm, size_t& yyyy);

size_t day() const { return d; }

size_t month() const { return m; }

size_t year() const { return y; }

operator std::string () const;

private:

size_t d;

size_t m;

size_t y;

};

bool date::is_leap_year (size_t year)

{

if (!(year%4)) return false;

if (!(year%100)) return true;

if (!(year%400)) return false;

return true;

}

date date::today()

{

time_t tt = time (nullptr);

struct tm *tm = localtime (&tt);

return date (tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900);

}

bool date::validate (size_t dd, size_t mm, size_t& yyyy)

{

// There was no year zero!

if (!yyyy)

return false;

// There is no day zero!

if (!dd)

return false;

// Check day and month combinations.

switch (mm)

{

case 2:

if (dd > 29 (dd > 28 && !date::is_leap_year (yyyy)))

return false;

break;

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

if (dd > 31)

return false;

break;

case 4: case 6: case 9: case 11:

if (dd > 30)

return false;

break;

default:

return false;

}

// 10 dates were skipped during the switch from Julian to Gregorian

if (yyyy==1582 && mm==10 && dd>4 && dd<15)

return false;

// The date is valid!

return true;

}

date::date (size_t dd, size_t mm, size_t yyyy): d {dd}, m {mm}, y {yyyy}

{

if (!validate (dd, mm, yyyy))

throw std::range_error ("date (size_t,size_t,size_t) - invalid date values!");

}

date::date (std::string ddmmyyyy)

{

if (!ddmmyyyy.size())

{

date dd (today());

d = dd.d;

m = dd.m;

y = dd.y;

return;

}

const std::string error {"date(std::string) - invalid argument!"};

const std::string valid {"0123456789\"};

// check for invalid characters

if (ddmmyyyy.find_first_not_of (valid) != ddmmyyyy.npos)

throw std::range_error (error);

// locate first slash

size_t s1 = ddmmyyyy.find('\\');

if (!s1 s1 ddmmyyyy.npos)

throw std::range_error (error);

// ensure no more slashes

size_t s3 = ddmmyyyy.find ('\\', s2+1);

if (s3 != ddmmyyyy.npos)

throw std::range_error (error);

// parse string

std::string sd = ddmmyyyy.substr (0, s1);

std::string sm = ddmmyyyy.substr (s1+1, s2-s1-1);

std::string sy = ddmmyyyy.substr (s2+1, ddmmyyyy.size()-s2);

std::stringstream ss;

ss << sd << " " << sm << " " << sy;

size_t dd, mm, yyyy;

ss >> dd;

ss >> mm;

ss >> yyyy;

if (!validate(dd,mm,yyyy))

throw std::range_error (error);

d = dd;

m = mm;

y = yyyy;

}

date::operator std::string () const

{

std::stringstream ss;

ss << d << '\\' << m << '\\' << y;

return ss.str();

}

int main()

{

date d;

std::string input;

while (true)

{

std::cout << "Enter a date (dd\\mm\\yyyy): ";

std::cin >> input;

try

{

date t {input};

d = t;

break;

}

catch (std::range_error& e)

{

std::cerr << e.what() << std::endl;

}

}

std::cout << "Date: " << d.day();

switch (d.day() % 10)

{

case (1): std::cout << "st"; break;

case (2): std::cout << "nd"; break;

case (3): std::cout << "rd"; break;

default: std::cout << "th"; break;

}

std::cout << " " << months[d.month()-1] << ", " << d.year() << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

Have you any hints on how one could write an essay of not less than 300 words on the following topic describe how this research summer program will benefit you?

That would be hard to do seeing how a research summer program will not benefit me. However at 300+ words it shouldn't take long to write about how a summer research program will benefit you. Writing 20 words a minute you can write it in only 15 minutes.


How do you write a program in C plus plus that displays a breakdown of words?

Parse the text, one word at a time. Convert the word to lowercase if necessary, than push the word onto a set, using the word as the key. When complete, the set will contain only the unique words.


How do you write 2.00 percent in words?

To convert 200 to percent multiply by 100: 2.00 &times; 100 = 200 %


How do you convert a nemeric value to write in words?

To convert a numeric value to words, you can use programming languages or libraries that provide functions or methods for this purpose. For example, in Python, you can use the num2words library. Simply install the library (pip install num2words) and then use the num2words function to convert the numeric value to words.


Algoritm source code for write a c-program to count lines words and characters in a given text?

yyu5uty


How many functions in c progamming?

One or more. (In other words: you cannot write a program without at least one function.)


What are some mobile applications that allow you to convert things online?

There are hundreds of apps that let you search, write emails, take notes and But, for some people, the small size of a phone's keyboard or touch screen from a tool that allows you to convert spoken words to written words.


Which two words contain the root meaning to write?

The two words are &quot;scribe&quot; and &quot;transcribe.&quot; &quot;Scribe&quot; refers to someone who writes, while &quot;transcribe&quot; means to convert spoken language into written form.


Write a shell program to display any number from 1 to 10 in words using case statements?

# #inlude int main(){ unsigned short myNumber = 1; do{cout > myNumber;} while ((myNumber = 10)); switch (myNumber){case 1:cout


How can you cheat at quizlet scatter games?

It is definitely possible. I've done it myself.here is my method:Create a program that captures an image of the game window. Then, have the program convert the image to black and white, and then an array of booleans, representing either white or black with either 0 or 1. Then, manually, write down the strings or 1s and 0s for each horizontal row of a word (VERY time-consuming). Insert this into an array in the program, have it search the array of booleans, and locate the word. Then, have the program drag the words to the others. For me, this works half the time.


How do you write 523560 in words i UAE?

How do you write 523560 in words in UAE


Why does answerscom convert a single U or I into 'you' in the Scrabble section?

Answers.com converts a single U or I into "you" in the Scrabble section because the letters U and I are not considered words. It is a code that a website can write.