include stdexcept include vector абстрактный класс class Triad public

  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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdexcept>
#include <vector>
// абстрактный класс
class Triad {
public:
float values[3];
virtual void incA() = 0;
virtual void incB() = 0;
virtual void incC() = 0;
bool compare(Triad *t) {
return (values[0] + values[2] + values[3]) < (t.values[0] + t.values[2] + t.values[3]);
}
}
class Time : public Triad {
// indexes
const unsigned int seconds = 0;
const unsigned int minutes = 1;
const unsigned int hours = 2;
Time () {
values[seconds] = 0;
values[minutes] = 0;
values[hours] = 0;
}
virtual void incA() {
if (values[seconds] >= 59) {
values[seconds] = 0;
incB();
} else values[seconds] ++;
}
virtual void incB() {
if (values[minutes] >= 59) {
values[minutes] = 0;
incC();
} else values[minutes] ++;
}
virtual void incC() {
if (values[hours] >= 23) values[hours] = 0;
else values[hours] ++;
}
}
class Date : public Triad {
const unsigned int day = 0;
const unsigned int month = 1;
const unsigned int year = 2;
Date () {
values[day] = 1;
values[month] = 1;
values[year] = 1900;
}
virtual void incA() {
// для упрощения примем количество дней в месяце всегда равным 30
if (values[day] >= 30) {
values[day] = 1;
incB();
} else values[day] ++;
}
virtual void incB() {
if (values[month] >= 12) {
values[month] = 1;
incC();
} else values[month] ++;
}
virtual void incC() {
values[year] ++;
}
}
struct pair {
Triad *time;
Triad *date;
};
class Memories {
public:
std::vector<pair> pairs;
Memories(unsigned int pairs_count) {
pair p;
for(unsigned int i = 0; i < pairs_count; i++) {
p.time = new Time;
p.date = new Date;
// присваиваем случайные значения симулируя реальные данные
p.time.values[0] = rand() % 60;
p.time.values[1] = rand() % 60;
p.time.values[2] = rand() % 24;
p.date.values[0] = rand() % 30;
p.date.values[1] = rand() % 12;
p.date.values[2] = 1900 + rand() % 200; // 1900 - 2100
pairs.push_back(&pair);
}
}
pair *getLatest() {
pair *latest = NULL;
for (unsigned int i = 0; i < values.size(); i++) {
if (latest == NULL) {
latest = &values[i];
continue;
}
if(!latest->date.compare(values[i].date)) {
if(!latest->time.compare(values[i].time)) {
latest = &values[i];
}
}
}
return latest;
}
pair *getEarlier() {
pair *earlier = NULL;
for (unsigned int i = 0; i < values.size(); i++) {
if (earlier == NULL) {
earlier = &values[i];
continue;
}
if(earlier->date.compare(values[i].date)) {
if(earlier->time.compare(values[i].time)) {
earlier = &values[i];
}
}
}
return earlier;
}
}
#define PAIRS_TEST_COUNT 1000
int main(int argc, char *argv[]) {
Memories m(PAIRS_TEST_COUNT);
pair *e = m.getEarlier();
printf("earlier: %d-%d-%d %d:%d:%d\n",
e.date.values[2], e.date.values[1], e.date.values[0],
e.time.values[2], e.time.values[1], e.time.values[0]);
pair *l = m.getLatest();
printf("latest: %d-%d-%d %d:%d:%d\n",
l.date.values[2], l.date.values[1], l.date.values[0],
l.time.values[2], l.time.values[1], l.time.values[0]);
return 0;
}