-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueModelBase.cpp
240 lines (205 loc) · 3.58 KB
/
ValueModelBase.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "ValueModelBase.h"
#include <fstream>
#include <windows.h>
int ValueModelBase::getRand(unsigned int min, unsigned int max)
{
LARGE_INTEGER t1;
QueryPerformanceCounter(&t1);
srand(t1.QuadPart+time(0));
if (max == 0 && min == 0)
{
//qdebug() << "rand % error";
return 0;
}
else if (max == 0)
{
max = min;
min = 0;
}
return rand() % max + min;
}
int ValueModelBase::getMax(QList<int> in)
{
int max;
max = in.at(0);
for (size_t i = 1; i < in.size(); i++)
{
if (in.at(i) > max)
{
max = in.at(i);
}
}
return max;
}
int ValueModelBase::getMin(QList<int> in)
{
int min;
min = in.at(0);
for (size_t i = 1; i < in.size(); i++)
{
if (in.at(i) < min)
{
min = in.at(i);
}
}
return min;
}
QList<int> ValueModelBase::getOddNumber(QList<int> in)
{
QList<int> odd;
for (auto& obj: in)
{
if (obj%2 == 1)
{
odd.push_back(obj);
}
}
return odd;
}
QList<int> ValueModelBase::getEvenNumber(QList<int> in)
{
QList<int> even;
for (auto& obj : in)
{
if (obj % 2 == 0)
{
even.push_back(obj);
}
}
return even;
}
QList<int> ValueModelBase::getPrimeNumber(QList<int> in)
{
QList<int> prime;
std::ifstream inStream("primeList.txt");
std::string in_string;
inStream >> in_string;
QString in_qstring = QString(in_string.c_str());
QStringList prime_1_10000 = in_qstring.split(',');
int max = getMax(in);
int endIndex = 0;
for (size_t i = 0; i < prime_1_10000.size(); i++)
{
//qdebug() << prime_1_10000.at(i).toInt();
if (prime_1_10000.at(i).toInt() >= max)
{
//qdebug() << i;
endIndex = i;
break;
}
}
for (size_t i = 0; i <= endIndex; i++)
{
for (auto& obj: in)
{
if (obj == prime_1_10000.at(i).toInt())
{
prime.push_back(obj);
}
}
}
return prime;
}
QList<int> ValueModelBase::getCompositeNumber(QList<int> in)
{
QList<int> prime = getPrimeNumber(in);
QList<int> composite = in;
for (auto& obj: composite)
{
if (prime.contains(obj))
{
composite.removeOne(obj);
}
}
return composite;
}
bool ValueModelBase::isPrimeNumber(int in)
{
std::ifstream inStream("primeList.txt");
std::string in_string;
inStream >> in_string;
QString in_qstring = QString(in_string.c_str());
QStringList prime_1_10000 = in_qstring.split(',');
QString tmp_string = QString::number(in);
return prime_1_10000.contains(tmp_string);
}
int ValueModelBase::hasContinue(int obj, QList<int> in)
{
if (in.contains(obj+1))
{
return obj+1;
}
return 0;
}
int ValueModelBase::hasContinue(QList<int> vec, QList<int> in)
{
int max = getMax(vec);
return hasContinue(max, in);
}
QList<QList<int>> ValueModelBase::getContinueNumber(QList<int> in)
{
QList<QList<int>> out;
for (auto& obj: in)
{
bool has_vec = false;
QList<int> tmp_vector;
for (auto& obj2: out)
{
if (obj2.contains(obj))
{
tmp_vector = obj2;
out.removeOne(obj2);
//qdebug() << out.removeOne(obj2);
has_vec = true;
break;
}
}
if (!has_vec)
{
tmp_vector.push_back(obj);
}
if (hasContinue(tmp_vector,in))
{
tmp_vector.push_back(obj + 1);
}
if (tmp_vector.size()>1)
{
out.push_back(tmp_vector);
}
}
return out;
}
int ValueModelBase::getTailNumber(int in)
{
if (in > 10)
{
return in % 10;
}
return in;
}
int ValueModelBase::getSum(QList<int> in)
{
int sum = 0;
for (auto& obj: in)
{
sum += obj;
}
return sum;
}
void ValueModelBase::Calculate(DataType type)
{
switch (type)
{
case Element::DataMin:
setValue(_min);
break;
case Element::DataNormal:
setValue(getValue(MainWindow::currentListIndex));
break;
case Element::DataMax:
setValue(_max);
break;
default:
break;
}
}