Создать класс Date для работы с датами в формате "год.месяц.день". Дата представляется структурой с тремя полями типа unsigned int: для года, месяца и дня. Класс должен включать не менее трех функций инициализации: числами, строкой вида "год.месяц.день" (например, "2004.08.31") и датой. Обязательными операциями являются: вычисление даты через заданное количество дней, вычитание заданного количества дней из даты, определение високосности года, присвоение и получение отдельных частей (год, месяц, день), сравнение дат (равно, до, после), вычисление количества дней между датами.
Есть код, но задача в том, чтобы при вводе целого числа (например 12102002) это число воспринималось как дата и всё вычислялось.
main.cpp:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| #include <iostream>
#include "Date.h"
using namespace std;
int main()
{
setlocale( LC_ALL,"Russian" );
Date n1(20,12, 2018),n2;
cout << "Введите день, месяц и год, которые хотите назначить" << endl;
cin >> n2;
cout << "Дата строкой: ";
string S;
cin >> S;
if(n2.check()){
cout << "error";
}
else{
cout << n1<< endl;
cout << n2<< endl;
cout << "Дата по прошествии указанного количества дней:" << n1.after_a_specified_number_of_days(10) << endl;
cout << "Дата после вычитания:" << n1.subtraction_of_a_specified_number_of_days(11) << endl;
n2.leap_year();
cout << "Дни между датами:" << n1.calculating_the_number_of_days_between_dates(n2);
n1.date_comparison(n2);
}
} |
|
Date.h:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| #ifndef DATE_H
#define DATE_H
#include <iostream>
class Date
{
friend std::ostream& operator<<(std::ostream&,const Date&);
friend std::istream& operator>>(std::istream&, Date&);
public:
static std::string to_string_int(int );
std::string to_string() const;
Date();
Date(int ,int ,int );
Date(Date const & ) ;
int after_a_specified_number_of_days(int);
int subtraction_of_a_specified_number_of_days(int);
void leap_year();
Date assignment_of_individual_parts(Date d2);
void date_comparison(Date d2);
int calculating_the_number_of_days_between_dates(Date d2);
bool check();
private:
int Year;
int Day;
int Month;
};
#endif //DATE_H |
|
Date.cpp:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| #include "Date.h"
using namespace std;
bool Date::check(){
return Day > 31 || Day < 0 || Month > 12 || Month < 0 || Year < 0;}
Date::Date():Year(2018),Month(1),Day(1){}
Date::Date(int d,int m,int y) :Year(y), Month(m), Day(d) {}
Date::Date(Date const & other) :Year(other.Year), Month(other.Month), Day(other.Day) {}
std::string Date::to_string_int(int x) {
char str_rub [11];
return std::string(itoa(x,str_rub,10 ));
}
std::string Date::to_string() const {
return to_string_int(Day) + '/' + to_string_int(Month) + '/' + to_string_int(Year);
}
std::ostream& operator<<(std::ostream& out,const Date& date){
out << date.to_string();
return out;
};
std::istream& operator>>(std::istream& in, Date& date){
char t='.';
in >> date.Day >> t >> date.Month >> t >> date.Year;
return in;
};
int Date::after_a_specified_number_of_days(int d2)
{
return(Day + d2);
}
int Date::subtraction_of_a_specified_number_of_days(int d2)
{
return(Day - d2);
}
void Date::leap_year()
{
if(Year / 4 == 0)
{
cout << "Год високосный"<< endl;
}else
{
cout<< "Год не високосный"<< endl;
}
}
Date Date::assignment_of_individual_parts(Date d2)
{
return Date(d2.Day, d2.Month, d2.Year);
}
void Date::date_comparison(Date d2)
{
int D = Day;
int D2 = d2.Day;
if(D < D2 && Month<=d2.Month && Year<=d2.Year)
{
cout << "Первая Дата больше чем вторая" << endl;
}else
if (D > D2 && Month >= d2.Month && Year >= d2.Year)
{
cout << "Первая дата меньше второй" << endl;
}
else
{
cout << "Первая дата равна второй" << endl;
}
}
int Date::calculating_the_number_of_days_between_dates(Date d2)
{
return Day - d2.Day+(Month - d2.Month)*30+((Year - d2.Year)*365);
} |
|