Created
February 1, 2018 21:25
-
-
Save async-python/2b7127b553a9f4a4bfb4efc9420000f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
//------------------------------------------------------------------------------------- | |
inline void error(const string& s) | |
{ | |
throw runtime_error(s); | |
} | |
//------------------------------------------------------------------------------------- | |
int max_value = 99; | |
int min_value = 1; | |
int year = 2; | |
int day = 1; | |
int month = 0; | |
//------------------------------------------------------------------------------------- | |
struct user_date { | |
int year; | |
int day; | |
int month; | |
void print_ambiguous() { | |
cout << "ambiguous" << endl; | |
} | |
void print_date() { | |
cout << month << "/" << day << "/" << year << endl; | |
} | |
user_date(int x, int y, int z) : year(x), day(y), month(z) {} | |
}; | |
//------------------------------------------------------------------------------------- | |
int number_input() { | |
int x; | |
while (!(cin >> x)) { | |
cin.clear(); | |
cin.ignore(); | |
error("Wrong input. Please, try again"); | |
} | |
if (x > max_value || x < min_value) { | |
error("value out range"); | |
} | |
return x; | |
} | |
//------------------------------------------------------------------------------------- | |
int main() { | |
while (true) { | |
try { | |
vector<int> month_table = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // table of valid days in months | |
vector<int> user_numbers = { 0, 0, 0 }; //user input template | |
for (int i = 0; i < user_numbers.size(); ++i) { | |
user_numbers[i] = number_input(); | |
} | |
sort(user_numbers.begin(), user_numbers.end()); | |
user_date u(user_numbers[year], user_numbers[day], user_numbers[month]); | |
int current_day_max = month_table[u.month - 1]; | |
if (u.month > month_table.size()) error("month out range"); | |
else if (u.day > current_day_max) error("day out range "); | |
if (u.year > current_day_max) { | |
if (u.day == u.month || u.day > month_table.size()) u.print_date(); | |
else u.print_ambiguous(); | |
} | |
else if (u.year == u.day && u.day > month_table.size()) { | |
u.print_date(); | |
} | |
else u.print_ambiguous(); | |
} | |
catch (exception& e) { | |
cin.clear(); | |
cin.ignore(); | |
cerr << "exception: " << e.what() << endl; | |
} | |
catch (...) { | |
cin.clear(); | |
cin.ignore(); | |
cerr << "unknown error" << endl; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment