Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication3.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <iostream>
- #define MAX_MONTH 12
- int monthsMaxDays[] = {
- 31, // JAN
- 28, // FEB
- 31, // ...
- 30,
- 31,
- 30,
- 31,
- 31,
- 30,
- 31,
- 30,
- 31
- };
- void decDate(int &m, int &d);
- bool validateInput(const int m, const int d);
- int main()
- {
- int d, m;
- std::cout << "Month: ";
- std::cin >> m;
- std::cout << "Day: ";
- std::cin >> d;
- if (!validateInput(m, d)) {
- return -1;
- }
- decDate(m, d);
- std::cout << "Previous date: Month = " << m << ", Day = " << d;
- return 0;
- }
- void decDate(int &m, int &d)
- {
- d -= 1;
- if (d == 0) {
- d = monthsMaxDays[m - 2];
- m -= 1;
- if (m == 0) {
- m = MAX_MONTH;
- }
- }
- }
- bool validateInput(const int m, const int d) {
- if (m > MAX_MONTH || m <= 0) {
- std::cerr << "Month must be greater than 0 and lesser than 12";
- return false;
- }
- if (d > monthsMaxDays[m - 1] || m <= 0) {
- std::cerr << "Day must be greater than 0 and lesser its max value";
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement