12345678910111213141516171819202122232425262728293031 |
- /* You are to read each part of the date into its own integer
- ~ type variable. The year should be a 4 digit number.
- ~ You can assume the user enters a correct date (no error checking required).
- ~ Determine whether the entered date is a magic date.
- ~ Here are the rules for a magic date:
- mm * dd is a 1-digit number that matches the last digit of yyyy
- or
- mm * dd is a 2-digit number that matches the last 2 digits of yyyy
- or
- mm * dd is a 3-digit number that matches the last 3 digits of yyyy
- The program should then display true if the date is magic,
- or false if it is not.
- */
- bool magic(std::string str) {
- char space = ' ';
- std::size_t firstspace = str.find(space);
- // find position of the SPACE following firstspace
- std::size_t secondspace = str.find(space,firstspace+1);
- // the month is everything before firstspace
- int month = std::stoi(str.substr(0,firstspace));
- // the day is everything between firstspace and secondspace
- int day = std::stoi(str.substr(firstspace,(secondspace-firstspace)));
- // the year is everything after the secondspace
- std::string year = (str.substr(secondspace+1,4));
- int ears = year[3]-'0';
- int decade = std::stoi(year.substr(2,2));
- int century = std::stoi(year.substr(1,3));
- // md is a misdirection otherwise known as MONTH*DAY
- int md = month*day;
- return md==ears || md==decade || md==century;
- }
|