-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaberProgramming.cpp
More file actions
308 lines (251 loc) · 6.67 KB
/
SaberProgramming.cpp
File metadata and controls
308 lines (251 loc) · 6.67 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Saber_Programming.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
#include <iostream>
#include <cmath>
#include <random>
#include <sstream>
using namespace std;
struct ListNode {
ListNode* prev;
ListNode* next;
ListNode* rand; // указатель на произвольный элемент или null;
string data;
};
class List {
private:
ListNode* head;
ListNode* tail;
int count =0;
public:
ListNode* Add(string data) {
ListNode *node = new ListNode;
if (count== 0) {
head = node;
(*node).prev = nullptr;
}
else {
tail->next = node;
(*node).prev = tail;
}
tail = node;
count++;
(*node).data = data;
(*node).next = nullptr;
(*node).rand = nullptr;
return node;
}
void Display(); // Displays the list
void GetRandomNodeForAll(); // Gets a random or nullptr for all nodes in the list
void Serialize(FILE* file); // Сохранение в файл (файл открыт с помощью fopen(path,"wb"));
void Deserialize(FILE* file);// Загрузка из файла (файл открыт с помощью fopen(path,"rb"));
template<typename T>
bool Write(FILE* file, T value); //Writes the value in a file;
template<typename T>
bool Read(FILE* file, T* value); // Reads the value from a file;
};
void List::Display() {
for (ListNode* i = head; i != NULL; i = i->next) {
cout << i->data;
if (i->rand != nullptr)
cout << " rand->" << i->rand->data << endl;
else
cout << endl;
}
}
template<typename T>
bool List::Write(FILE* file, T value) {
if (!file)
return false;
fwrite(&value, sizeof(value), 1, file);
size_t t = sizeof(value);
return true;
}
template<>
bool List::Write(FILE* file, string value)
{
if (!file)
return false;
uint32_t size = static_cast<std::uint32_t>(value.size());
Write(file, size);
fwrite(value.c_str(), size, 1, file);
return true;
}
template<typename T>
bool List::Read(FILE* file, T* value)
{
if (!file)
return false;
fread(value, 1, sizeof(T), file);
return true;
}
template<>
bool List::Read(FILE* file, string* s) {
if (!file)
return false;
uint32_t size;
fread(&size, 1, sizeof(int32_t), file);
char* buf = new char[size + 1];
fread(buf, 1, size, file);
buf[size] = '\0';
*s = string(buf);
delete[]buf;
return true;
}
void List::GetRandomNodeForAll() {
random_device rd;
mt19937 gen;
gen.seed(rd());
uniform_int_distribution<> rand(0, count);
for (ListNode* i = head; i != NULL; i = i->next)
{
ListNode* j = head;
for (int r = rand(gen); r != 0; r--) {
if (r == count) {
j = nullptr;
break;
}
j = j->next;
}
i->rand = j;
}
}
void List::Serialize(FILE* file)
{
Write(file, count);
for (ListNode* node = head; node != NULL; node = node->next) {
Write(file, node);
Write(file, node->rand);
Write(file, node->data);
}
}
void List::Deserialize(FILE* file)
{
int num;
vector<vector<string> > ivec;
Read(file, &num);
ivec.resize(num);
ListNode* one;
ListNode* two;
for (int i = 0; i < num; i++) {
ListNode* node = new ListNode;
Read(file, &one);
Read(file, &two);
Read(file, &(node->data));
ostringstream first, second; //Избыточность для читаемости
first << one;
second << two;
ivec[i].push_back(first.str());
ivec[i].push_back(second.str());
this->Add(node->data);
delete node;
}
//Algorithm for linkinig of random nodes;
int i = 0;
for (ListNode* n = head; n != NULL; n = n->next) {
int nodenumber = -1;
n->rand = nullptr;
for (int j = 0; j < ivec.size(); j++)
if (ivec[i][1] == ivec[j][0])
{
nodenumber = j;
break;
}
ListNode* k = head;
if (nodenumber == -1)
n->rand = nullptr;
else {
for (int g = nodenumber; g != 0; g--)
k = k->next;
n->rand = k;
}
i++;
}
}
void own_int_to_binary(int n);
void RemovePups(char* str);
int main()
{
List F,S,T;
FILE* ptrFile;
cout << "----first -----" << endl;
F.Add("First");
F.Add("Second");
F.Add("Third");
F.Add("Fourth");
F.GetRandomNodeForAll();
F.Display();
fopen_s(&ptrFile, "file.txt", "wb");
F.Serialize(ptrFile);
fclose(ptrFile);
cout << "-----Copy of the first ------" << endl;
fopen_s(&ptrFile, "file.txt", "rb");
S.Deserialize(ptrFile);
fclose(ptrFile);
S.Display();
cout << "-----Added one more line and changed all random nodes ------" << endl;
fopen_s(&ptrFile, "file.txt", "wb");
S.Add("Fifth");
S.GetRandomNodeForAll();
S.Display();
S.Serialize(ptrFile);
fclose(ptrFile);
cout << "-----Copy of the second ------" << endl;
fopen_s(&ptrFile, "file.txt", "rb");
T.Deserialize(ptrFile);
fclose(ptrFile);
T.Display();
cout << "\n \n \n";
char data[] = "ABAA BBCASB ASDFVSSDVWWRV SD";
cout << "---data before---" << endl << data << endl;
RemovePups(data);
cout << "---data after ---" << endl << data << "\n \n \n";
int a = -9;
cout << "Number: " << a << endl;
own_int_to_binary(a);
}
void own_int_to_binary(int n) // first bit is a sign then binary numbers
{
int size = sizeof(int) * 8;
char* a = new char[size];
int sum = n;
char one = '1';
char two = '0';
//Sign
if (n < 0) {
n = -n;
one = '0';
two = '1';
}
//Algorithm
for (int i = size - 1; i >= 0; i--)
{
if (n / pow(2, i) >= 1) {
n -= pow(2, i);
a[size - 1 - i] = one;
}
else
a[size - 1 - i] = two;
}
if (one == '0') {
for (int i = size - 1; i >= 0; i--) {
if (a[i] == '0'){
a[i] = '1';
break;
}
else
a[i] = '0';
}
}
for (int i = 0; i < size; i++)
cout << a[i] << " ";
cout << endl;
delete[]a;
}
void RemovePups(char* str)
{
for (int i = 1; i < strlen(str); i++)
if (str[i - 1] == str[i]) {
for (int j = i; str[j] != '\0'; j++)
str[j] = str[j + 1];
i--;
}
}