Untitled

 avatar
user_9026165
plain_text
a year ago
2.1 kB
5
Indexable
#include <iostream>
using namespace std;

bool isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

long long secondsInYear(int year) {
    return isLeapYear(year) ? 366 * 24 * 60 * 60 : 365 * 24 * 60 * 60;
}

int main() {
    int currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond;
    cout << "Enter current year: ";
    cin >> currentYear;
    cout << "Enter current month: ";
    cin >> currentMonth;
    cout << "Enter current day: ";
    cin >> currentDay;
    cout << "Enter current hour: ";
    cin >> currentHour;
    cout << "Enter current minute: ";
    cin >> currentMinute;
    cout << "Enter current second: ";
    cin >> currentSecond;

    int birthYear, birthMonth, birthDay;
    cout << "Enter your birth year: ";
    cin >> birthYear;
    cout << "Enter your birth month: ";
    cin >> birthMonth;
    cout << "Enter your birth day: ";
    cin >> birthDay;

    long long totalSeconds = 0;
    for (int year = birthYear; year < currentYear; ++year) {
        totalSeconds += secondsInYear(year);
    }

    int daysPassed = 0;
    for (int month = birthMonth; month <= 12; ++month) {
        int daysInMonth;
        switch (month) {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                daysInMonth = 31;
                break;
            case 4: case 6: case 9: case 11:
                daysInMonth = 30;
                break;
            case 2:
                daysInMonth = isLeapYear(birthYear) ? 29 : 28;
                break;
        }
        if (month == birthMonth) {
            daysInMonth -= birthDay - 1;
        }
        if (month == currentMonth) {
            daysInMonth = currentDay;
        }
        daysPassed += daysInMonth;
    }

    totalSeconds += daysPassed * 24 * 60 * 60 + currentHour * 60 * 60 + currentMinute * 60 + currentSecond;

    cout << "Your age in seconds including leap years is: " << totalSeconds << " seconds." << endl;

    return 0;
}

Editor is loading...
Leave a Comment