Commit d199bc0bece39fb3c468658bf10b7e959fd98108
1 parent
38471bb3
Added DateType
Showing
4 changed files
with
146 additions
and
0 deletions
examples02/01-date/DateType.cpp
0 → 100644
1 | +#include "DateType.h" | |
2 | + | |
3 | +/** Constructors **/ | |
4 | +DateType::DateType() { | |
5 | + Day = 1; | |
6 | + Month = 1; | |
7 | + Year = 1; | |
8 | +} | |
9 | +DateType::DateType(int newMonth, int newDay, int newYear) { | |
10 | + Day = newDay; | |
11 | + Month = newMonth; | |
12 | + Year = newYear; | |
13 | +} | |
14 | +// returns Year | |
15 | +int DateType::GetYear() const { return Year; } | |
16 | +// returns Month | |
17 | +int DateType::GetMonth() const { return Month; } | |
18 | +// returns Day | |
19 | +int DateType::GetDay() const { return Day; } | |
20 | + | |
21 | +RelationType DateType::ComparedTo(DateType otherDate) { | |
22 | + if (Year < otherDate.Year) | |
23 | + return Precedes; | |
24 | + if (Year > otherDate.Year) | |
25 | + return Follows; | |
26 | + if (Month < otherDate.Month) | |
27 | + return Precedes; | |
28 | + if (Month > otherDate.Month) | |
29 | + return Follows; | |
30 | + if (Day < otherDate.Day) | |
31 | + return Precedes; | |
32 | + if (Day > otherDate.Day) | |
33 | + return Follows; | |
34 | + return Same; | |
35 | +} | ... | ... |
examples02/01-date/DateType.h
0 → 100644
1 | +enum RelationType { Precedes, Same, Follows }; | |
2 | + | |
3 | +class DateType { | |
4 | +public: | |
5 | + // constructor | |
6 | + DateType(); | |
7 | + DateType(int newMonth, int newDay, int newYear); | |
8 | + // accessor methods (get methods) | |
9 | + int GetYear() const; // returns Year | |
10 | + int GetMonth() const; // returns Month | |
11 | + int GetDay() const; // returns Day | |
12 | + RelationType ComparedTo(DateType dateA); | |
13 | + | |
14 | +private: | |
15 | + int Year; | |
16 | + int Month; | |
17 | + int Day; | |
18 | +}; | |
0 | 19 | \ No newline at end of file | ... | ... |
examples02/01-date/TestDateType.cpp
0 → 100644
1 | +#include "DateType.h" | |
2 | +#include <iomanip> | |
3 | +#include <iostream> | |
4 | +using namespace std; | |
5 | + | |
6 | +void PrintMonth(int Month, ostream &Out) { | |
7 | + switch (Month) { | |
8 | + case 1: | |
9 | + Out << "January"; | |
10 | + return; | |
11 | + case 2: | |
12 | + Out << "February"; | |
13 | + return; | |
14 | + case 3: | |
15 | + Out << "March"; | |
16 | + return; | |
17 | + case 4: | |
18 | + Out << "April"; | |
19 | + return; | |
20 | + case 5: | |
21 | + Out << "May"; | |
22 | + return; | |
23 | + case 6: | |
24 | + Out << "June"; | |
25 | + return; | |
26 | + case 7: | |
27 | + Out << "July"; | |
28 | + return; | |
29 | + case 8: | |
30 | + Out << "August"; | |
31 | + return; | |
32 | + case 9: | |
33 | + Out << "September"; | |
34 | + return; | |
35 | + case 10: | |
36 | + Out << "October"; | |
37 | + return; | |
38 | + case 11: | |
39 | + Out << "November"; | |
40 | + return; | |
41 | + case 12: | |
42 | + Out << "December"; | |
43 | + return; | |
44 | + default: | |
45 | + Out << "Juvember"; | |
46 | + } | |
47 | +} | |
48 | + | |
49 | +void PrintDate(DateType aDate, ostream &Out) { | |
50 | + PrintMonth(aDate.GetMonth(), Out); | |
51 | + Out << ' ' << aDate.GetDay() << ", " << setw(4) << aDate.GetYear() << endl; | |
52 | +} | |
53 | + | |
54 | +RelationType ComparedTo(DateType dateA, DateType dateB) { | |
55 | + if (dateA.GetYear() < dateB.GetYear()) | |
56 | + return Precedes; | |
57 | + if (dateA.GetYear() > dateB.GetYear()) | |
58 | + return Follows; | |
59 | + if (dateA.GetMonth() < dateB.GetMonth()) | |
60 | + return Precedes; | |
61 | + if (dateA.GetMonth() > dateB.GetMonth()) | |
62 | + return Follows; | |
63 | + if (dateA.GetDay() < dateB.GetDay()) | |
64 | + return Precedes; | |
65 | + if (dateA.GetDay() > dateB.GetDay()) | |
66 | + return Follows; | |
67 | + return Same; | |
68 | +} | |
69 | + | |
70 | +int main() { | |
71 | + DateType Tomorrow(1, 18, 2002), AnotherDay(10, 12, 1885); | |
72 | + if (ComparedTo(Tomorrow, AnotherDay) == Same) { | |
73 | + cout << "what do you think?" << endl; | |
74 | + } | |
75 | + | |
76 | + if (Tomorrow.ComparedTo(AnotherDay) == Same) | |
77 | + cout << "Think about it, Scarlett!" << endl; | |
78 | + | |
79 | + PrintDate(Tomorrow, cout); | |
80 | +} | ... | ... |
examples02/01-date/makefile
0 → 100644
1 | +TestDateType: TestDateType.o DateType.o | |
2 | + g++ -g $^ -o $@ | |
3 | + | |
4 | +DateType.o: DateType.cpp DateType.h | |
5 | + g++ -g -c -Wall -pedantic $< -o $@ | |
6 | + | |
7 | +TestDateType.o: TestDateType.cpp DateType.h | |
8 | + g++ -g -c -Wall -pedantic $< -o $@ | |
9 | + | |
10 | +.PHONY: clean | |
11 | + | |
12 | +clean: | |
13 | + -rm TestDateType DateType.o TestDateType.o | |
0 | 14 | \ No newline at end of file | ... | ... |