Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct dateTime
- {
- int day;
- int month;
- int year;
- string date;
- private:
- int mLength[13]{ 1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- string week[7]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
- string monthNames[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- string parsedInt;
- string parsedString;
- public:
- void setData(int d, int m, int y) {
- day = d;
- month = m;
- year = y;
- date = to_string(day) + "/" + to_string(month) + "/" + to_string(year);
- }
- string getData() {
- return date;
- }
- bool leapy(int d, int m, int y) {
- if (y % 4 == 0) {
- if (y % 100 != 0) {
- if (m > 2) {
- return true;
- }
- else if (m == 2 && d == 29) {
- return true;
- }
- else
- {
- return false;
- }
- }
- else {
- if (y % 400 == 0) {
- if (m > 2) {
- return true;
- }
- else if (m == 2 && d == 29) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- }
- int dofy(int d, int m, int y) {
- int th = 0;
- for (int i = 1; i < m; i++) {
- th += mLength[i];
- }
- th += d;
- if (leapy(d, m, y)) {
- th += mLength[0];
- }
- return th;
- }
- int dayOfYear() {
- int dayCount = dofy(day, month, year);
- return dayCount;
- }
- bool compareTo(dateTime dt) {
- if (year > dt.year) {
- return true;
- }
- else if (year == dt.year) {
- if (month > dt.month) {
- return true;
- }
- else if (month == dt.month) {
- if (day >= dt.day) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- int getTimeSpam(dateTime dt) {
- int dayCount = 0;
- if (compareTo(dt)) {
- if (year > dt.year) {
- dayCount += dt.dofy(31, 12, dt.year) - dt.dayOfYear();
- dayCount += dayOfYear();
- }
- else {
- dayCount += dayOfYear() - 1;
- }
- int yy = dt.year + 1;
- while (yy < year) {
- dayCount += dt.dofy(31, 12, yy);
- yy++;
- }
- return dayCount;
- }
- else {
- dayCount += dt.dayOfYear();
- int yy = dt.year - 1;
- while (yy > year) {
- dayCount += dt.dofy(31, 12, yy);
- yy--;
- }
- dayCount += dofy(31, 12, year) - dayOfYear();
- return dayCount;
- }
- }
- string dayOfWeek() {
- //2018 1 jan Monday
- dateTime dt;
- dt.setData(1, 1, 2018);
- int dayCount = getTimeSpam(dt);
- while (dayCount > 6) {
- dayCount -= 7;
- }
- if (compareTo(dt)) {
- return week[dayCount];
- }
- else {
- if (6 - dayCount + 1 < 7) {
- return week[6 - dayCount + 1];
- }
- else {
- return week[0];
- }
- }
- }
- string getMonth() {
- return monthNames[month - 1];
- }
- string getFullDateString() {
- return dayOfWeek() + "/" + getMonth() + "/" + to_string(year);
- }
- int checkDL(int d, int m, int y) {
- if (m == 2) {
- if (leapy(d, m, y)) {
- return mLength[m] + mLength[0];
- }
- else {
- return mLength[m];
- }
- }
- else
- {
- return mLength[m];
- }
- }
- int dateTester(int d, int m, int y) {
- if (0 < m && m < 13) {
- if (0 < d && d <= checkDL(d, m, y)) {
- return 0;
- }
- else {
- return 1;
- }
- }
- else {
- return 2;
- }
- }
- bool stringTester(string month) {
- int stringCount = 0;
- int intCount = 0;
- for (int i = 0; i < month.length(); i++) {
- if (48 <= month[i] && month[i] <= 57) {
- intCount++;
- parsedInt += month[i];
- }
- else
- {
- stringCount++;
- parsedString += month[i];
- }
- }
- if (intCount >= stringCount) {
- return true;
- }
- else {
- return false;
- }
- }
- int stringToInt(string toInt) {
- int m = 0;
- int tenth = 1;
- for (int i = toInt.length() - 1; i >= 0; i--) {
- m += (toInt[i] - '0') * tenth;
- tenth *= 10;
- }
- return m;
- }
- int getMonthString() {
- int hit[12]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 };
- int miss[12]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 };
- int maxIndex = 0;
- //parsedString
- for (int i = 0; i < parsedString.length(); i++) {
- for (int j = 0; j < 12; j++) {
- for (int k = 0; k < monthNames[j].length(); k++) {
- if (parsedString[i] == monthNames[j][k]) {
- hit[j]++;
- }
- }
- }
- }
- for(int i = 0; i < 12; i++) {
- miss[i] = hit[i] * 100 / (parsedString.length() * monthNames[i].length());
- if (miss[i] > miss[maxIndex]) {
- maxIndex = i;
- }
- }
- return maxIndex;
- }
- int chooseFormat(int d, string mm, int y) {
- int monthCalculated;
- if (stringTester(mm)) {
- monthCalculated = stringToInt(parsedInt);
- }
- else {
- monthCalculated = getMonthString() + 1;
- }
- return monthCalculated;
- }
- };
- int main() {
- int d, m, y;
- string month;
- bool run = true;
- cout << "Input day: ";
- cin >> d;
- cin.clear();
- cin.ignore(100, '\n');
- cout << "Input month: ";
- cin >> month;
- cout << "Input year: ";
- cin >> y;
- cin.clear();
- cin.ignore(100, '\n');
- cout << endl;
- while (run) {
- dateTime dt;
- m = dt.chooseFormat(d, month, y);
- if (dt.dateTester(d, m, y) == 0) {
- dt.setData(d, m, y);
- cout << dt.getData() << endl;
- cout << "Day of year: " << dt.dayOfYear() << endl;
- cout << "Day of week: " << dt.dayOfWeek() << endl;
- cout << "Month: " << dt.getMonth() << endl;
- cout << dt.getFullDateString() << endl;
- run = false;
- }
- else if (dt.dateTester(d, m, y) == 1) {
- cout << "Incorrect day format... Please enter integer(1-28/29, 1-30/31): ";
- cin >> d;
- cout << endl;
- }
- else if (dt.dateTester(d, m, y) == 2) {
- cout << "Incorrect month format... Please enter integer(1-12) or month name: ";
- cin >> month;
- cout << endl;
- }
- else if (dt.dateTester(d, m, y) == 3){
- cout << "Incorrect year format... Please enter integer: ";
- cin >> y;
- cout << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement