Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using std::cout; using std::cin; using std::endl; using std::istream; using std::ostream;
- class date
- {
- public:
- date() = default;
- bool organizeDates();
- bool organize();
- ostream &output(ostream &os) { os << year << " " << month << " " << day; return os; }
- istream &input(istream &is) { is >> year >> month >> day; return is; }
- private:
- bool checkValid();
- bool checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3);
- void switchItems(unsigned &item1, unsigned &item2) { unsigned tempItem1 = item1; item1 = item2; item2 = tempItem1; }
- unsigned year = 0;
- unsigned month = 0;
- unsigned day = 0;
- };
- bool date::checkValid()
- {
- if (month <= 12 && day <= 31)
- return 1;
- return 0;
- }
- bool date::checkSwitch(unsigned &item1, unsigned &item2, unsigned &item3)
- {
- switchItems(item1, item2);
- if (checkValid())
- {
- return true;
- }
- else
- {
- switchItems(item1, item2);
- switchItems(item2, item3);
- if (checkValid())
- return true;
- else
- {
- switchItems(item2, item3);
- switchItems(item1, item3);
- if (checkValid())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- bool date::organize()
- {
- if (checkValid())
- return 1;
- else
- {
- if (checkSwitch(day, month, year))
- {
- return true;
- }
- else if (checkSwitch(year, month, day))
- {
- return true;
- }
- else if (checkSwitch(day, year, month))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- bool date::organizeDates()
- {
- if (checkValid())
- {
- return true;
- }
- else
- {
- if (organize())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- return false;
- }
- int main()
- {
- cout << "Enter year, month, date and ill try and organize it" << endl;
- date dawg;
- dawg.input(cin);
- if (dawg.organizeDates())
- dawg.output(cout) << endl;
- else
- cout << "Your input was invalid" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment