Guest User

Untitled

a guest
Dec 19th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using std::cout; using std::cin; using std::endl; using std::istream; using std::ostream;
  6.  
  7. class date
  8. {
  9. public:
  10.     date() = default;
  11.  
  12.     bool organizeDates();
  13.     bool organize();
  14.     ostream &output(ostream &os) { os << year << " " << month << " " << day; return os; }
  15.     istream &input(istream &is) { is >> year >> month >> day; return is; }
  16.  
  17. private:
  18.  
  19.     bool checkValid();
  20.     bool checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3);
  21.     void switchItems(unsigned &item1, unsigned &item2) { unsigned tempItem1 = item1; item1 = item2; item2 = tempItem1; }
  22.  
  23.     unsigned year = 0;
  24.     unsigned month = 0;
  25.     unsigned day = 0;
  26. };
  27.  
  28. bool date::checkValid()
  29. {
  30.     if (month <= 12 && day <= 31)
  31.         return 1;
  32.     return 0;
  33. }
  34.  
  35. bool date::checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3)
  36. {
  37.     switchItems(item1, item2);
  38.     if (checkValid())
  39.     {
  40.         return true;
  41.     }
  42.     else
  43.     {
  44.         switchItems(item1, item2);
  45.         switchItems(item2, item3);
  46.         if (checkValid())
  47.             return true;
  48.         else
  49.         {
  50.             switchItems(item2, item3);
  51.             switchItems(item1, item3);
  52.             if (checkValid())
  53.             {
  54.                 return true;
  55.             }
  56.             else
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. bool date::organize()
  65. {
  66.     if (checkValid())
  67.         return 1;
  68.     else
  69.     {
  70.         if (checkSwitch(day, month, year))
  71.         {
  72.             return true;
  73.         }
  74.         else if (checkSwitch(year, month, day))
  75.         {
  76.             return true;
  77.         }
  78.         else if (checkSwitch(day, year, month))
  79.         {
  80.             return true;
  81.         }
  82.         else
  83.         {
  84.             return false;
  85.         }
  86.     }
  87. }
  88.  
  89. bool date::organizeDates()
  90. {
  91.     if (checkValid())
  92.     {
  93.         return true;
  94.     }
  95.     else
  96.     {
  97.         if (organize())
  98.         {
  99.             return true;
  100.         }
  101.         else
  102.         {
  103.             return false;
  104.         }
  105.     }
  106.     return false;
  107. }
  108.  
  109. int main()
  110. {
  111.     cout << "Enter year, month, date and ill try and organize it" << endl;
  112.     date dawg;
  113.     dawg.input(cin);
  114.     if (dawg.organizeDates())
  115.         dawg.output(cout) << endl;
  116.     else
  117.         cout << "Your input was invalid" << endl;
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment