Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.3 kB
2
Indexable
#include <iostream>
#include <string>
#include <windows.h>
#include <vector>

struct Date
{
private:
    int day{};
    std::string month{};
    int year{};
    bool t{};
public:
    Date() {}

    Date(int day, std::string month, int year)
    {
        this->day = day;
        this->month = month;
        this->year = year;
        t = 1;
    }

    std::string get_date() 
    {
        std::string result;
        result += std::to_string(day) + ' ';
        result += month + ' ';
        result += std::to_string(year) + '\n';
        return result;
    }

    bool get_t()
    {
        return t;
    }

    int number_of_days(int month_number, int year)
    {
        int result = 1;
        if (month_number == 2)
        {
            if (year % 4 == 0)
                result = 4;
            else
                result = 3;
        }
        else if (month_number % 2 == 0)
            result = 2;
        return result;
    }// 1 - 31 день, 2 - 30 дней, 3 -28 дней в ферале, 4 - 29 дней в ферале

    void is_date( std::vector<std::string> months, int month_number)
    {

        bool temp = 0;
        for (size_t i = 0; i < months.size(); i++)
            if (month.find(months[i]) != std::string::npos)
                temp = 1;
                t = temp;

        int n = 0;
        n = number_of_days(month_number, year);

        if (day < 0)
            t = 0;
        else
        {
            switch (n)
            {
            case 1:
                if (day > 31)
                    t = 0;
                break;
            case 2:
                if (day > 30)
                    t = 0;
                break;
            case 3:
                if (day > 28)
                    t = 0;
                break;
            case 4:
                if (day > 29)
                    t = 0;
                break;
            default:
                std::cout << "'\nn < 0 or n > 4 \n";
                break;
            }
        }



        if (year > 2000 && year < 1900)
            t = 0;
        
        if (month.length() < 3)
            t = 0;
    }

};

void set_values(int& day, std::string& month, int& year)
{
    std::cout << "Введите день: ";
    std::cin >> day;
    std::cout << "Введите месяц: ";
    std::cin >> month;
    std::cout << "Введите год: ";
    std::cin >> year;
    std::cout << std::endl;
}

int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    int d, y ,month_number;
    std::string m;
    std::vector<std::string> mes {"янв","фев","мар" ,"апр" ,"май" ,"июн" ,"июл" ,"авг" ,"сен" ,"окт" ,"ноя" ,"дек" ,"мая"};

    set_values(d, m, y);

    Date input = { d,m,y};

    for (size_t i = 0; i < mes.size(); i++)
        if (m.find(mes[i]) != std::string::npos)
            month_number = i+1;
    if (month_number == 13)
        month_number = 5;

    input.is_date(mes,month_number);

    std::cout << input.get_date();

    if (input.get_t())
        std::cout << "Верная дата\n";
    else
        std::cout << "Неверная дата\n";

}