Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std::literals;
  6.  
  7. template <typename T>
  8. T read(const std::string& request)
  9. {
  10.     auto except = std::cin.exceptions();
  11.     try {
  12.         std::cin.exceptions(std::ios::eofbit | std::ios::badbit);
  13.         std::cout << request;
  14.         T value;
  15.         bool repeat = true;
  16.         while (repeat) {
  17.             repeat = false;
  18.             std::cin >> value;
  19.             if(!std::cin) {
  20.                 std::cout << "Invalid input\n";
  21.                 std::cin.clear();
  22.                 repeat = true;
  23.                 while(!isspace(std::cin.get()) && std::cin)
  24.                     ;
  25.             }
  26.         }
  27.         std::cin.exceptions(except);
  28.         return value;
  29.     } catch(...) {
  30.         std::cin.exceptions(except);
  31.         throw;
  32.     }
  33. }
  34.  
  35. bool ask(const std::string& question)
  36. {
  37.     std::cout << question << '\n';
  38.     char c;
  39.     do {
  40.         c = static_cast<char>(tolower(read<char>("")));
  41.         if(c != 'y' && c != 'n')
  42.             std::cout << "Invalid answer. Only Y, y, N and n are allowed\n";
  43.     } while(c != 'y' && c != 'n');
  44.     return c == 'y';
  45. }
  46.  
  47. namespace date
  48. {
  49. //                            ↓ January                           December ↓
  50. constexpr int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  51. constexpr int year_days = 365;
  52.  
  53. struct date_diff
  54. {
  55.     int year;
  56.     int mon;
  57.     int day;
  58. };
  59.  
  60. date_diff operator-(const std::tm& lhs, const std::tm& rhs)
  61. {
  62.     date_diff result {lhs.tm_year - rhs.tm_year,
  63.                       lhs.tm_mon  - rhs.tm_mon ,
  64.                       lhs.tm_mday - rhs.tm_mday };
  65.     if(result.day < 0) {
  66.         result.day += month_days[rhs.tm_mon];
  67.         --result.mon;
  68.     }
  69.     if(result.mon < 0) {
  70.         result.mon += 12;
  71.         --result.year;
  72.     }
  73.     return result;
  74. }
  75.  
  76. std::tm get_current_date()
  77. {
  78.     std::time_t now = std::time(nullptr);
  79.     return *std::localtime(&now);
  80. }
  81.  
  82. std::tm get_date()
  83. {
  84.     std::tm result {0, 0, 0 ,0, 0, 0, 0, 0, 0};
  85.     result.tm_year = read<int>("Enter year: " ) - 1900; //Epoch start
  86.     result.tm_mon  = read<int>("Enter month: ") - 1; //Internal representation fix
  87.     result.tm_mday = read<int>("Enter day: "  );
  88.     std::mktime(&result);
  89.     return result;
  90. }
  91.  
  92. std::string date_to_string(const std::tm& date)
  93. {
  94.     constexpr std::size_t buff_size = 128;
  95.     char buff[buff_size];
  96.     if(strftime(buff, buff_size - 1, "%d/%m/%Y", &date) == 0)
  97.         return "";
  98.     return buff;
  99. }
  100.  
  101. void agecalculator()
  102. {
  103.     std::tm now = get_current_date();
  104.     std::cout << "Your current year is: " << date_to_string(now) << "\n\n";
  105.  
  106.     std::cout << "What date were you born?\n";
  107.     std::tm birthday = get_date();
  108.     std::cout << "The date you were born is: " << date_to_string(birthday) << '\n';
  109.  
  110.     auto diff = now - birthday;
  111.     birthday.tm_year = now.tm_year + 1;
  112.     auto bdiff = birthday - now;
  113.  
  114.     std::cout << "You are " <<
  115.         (diff.year != 0 ? std::to_string(diff.year) + " years, " : ""s) <<
  116.         (diff.mon  != 0 ? std::to_string(diff.mon ) + " month "  : ""s) << ((diff.year || diff.mon) && diff.day ? "and " : "") <<
  117.         (diff.day  != 0 ? std::to_string(diff.day ) + " days "   : ""s) << "old\n\n";
  118.     std::cout << "There is " <<
  119.         (bdiff.mon != 0 ? std::to_string(bdiff.mon) + " month " : ""s) <<  (bdiff.mon && bdiff.day ? "and " : ""s) <<
  120.         (bdiff.day != 0 ? std::to_string(bdiff.day) + " days "  : ""s) <<
  121.         "before your next birthday where you will be " << diff.year + 1 << " years old\n";
  122. }
  123.  
  124. void currentyearcalculator()
  125. {
  126.     int birthyear  = read<int>(  "What year were you born?\n");
  127.     int currentage = read<int>("\nWhat is your current age?\n");
  128.     bool birthday  = ask("\nHave you had your birthday this year? Y/N\n");
  129.     int year = currentage + birthyear + (birthday ? 0 : 1);
  130.     std::cout << "\n\nThe current year is " << year << '\n';
  131. }
  132.  
  133. void birthdatecalculator()
  134. {
  135.     std::tm now = get_current_date();
  136.     int myageyear  = read<int>("What is your current age in years?\n");
  137.     int myagemonth = read<int>("\nHow many months into your age?\n");
  138.     int myageday   = read<int>("\nHow many days into your age, don't write months?\n");
  139.  
  140.     int resultday   = now.tm_mday - myageday;
  141.     int resultmonth = now.tm_mon + 1 - myagemonth;
  142.     int resultyear  = now.tm_year + 1900 - myageyear;
  143.     if (resultday < 0) {
  144.         resultmonth = resultmonth + 1;
  145.         resultday = resultday + 30;
  146.     }
  147.     if (resultmonth < 0) {
  148.         resultyear = resultyear + 1;
  149.         resultmonth = resultmonth + 12;
  150.     }
  151.     std::cout << "This is the date you were born aprox: " << resultyear << '/' << resultmonth << '/' << resultday << '\n';
  152. }
  153.  
  154. }
  155.  
  156. void menue()
  157. {
  158.     int menu = read<int>("1 for agecalculator. \n2 for current year calculator. \n3 for birthdat calculator.\nEnter another number to exit.\n\nEnter your choice here: ");
  159.     if (menu == 1)
  160.         date::agecalculator();
  161.     if (menu == 2)
  162.         date::currentyearcalculator();
  163.     if (menu == 3)
  164.         date::birthdatecalculator();
  165. }
  166.  
  167. int main()
  168. {
  169.     menue();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement