-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileName.cpp
More file actions
143 lines (128 loc) · 6.07 KB
/
Copy pathFileName.cpp
File metadata and controls
143 lines (128 loc) · 6.07 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct ListNode { // ListNode модифицировать нельзя
ListNode* prev = nullptr; // указатель на предыдущий элемент или nullptr
ListNode* next = nullptr; // указатель на следующий элемент
ListNode* rand = nullptr; // указатель на произвольный элемент данного списка, либо `nullptr`
std::string data; // произвольные пользовательские данные
};
std::string patch = "inlet.in";
int main()
{
std::ifstream file; // для файла inlet.in
std::ofstream outfile; // для файла outlet.out
std::string text; // для хранения строки потока ввода
std::vector <ListNode*> list; //
std::vector <int> value; // для хранения индексов <rand_index> в int
int index;
int size = 0; // количество экземпляров структуры
// начало чтения файла inlet.in
file.open(patch, std::ios::in);
if (file.is_open())
{
while (getline(file, text )) {
index = text.find_last_of(';'); // получение индекса разделения строки
if (index == std::string::npos) { // проверка на конец строки
continue;
}
std::string data = text.substr(0, index); // разделение на пользовательские данные data
value.push_back(stoi(text.substr(index+1))); // разделение указателя от data на произвольный элемент данного списка, либо `nullptr`
ListNode* newNode = new ListNode();
newNode->data = data; // запись data в поле структуры
list.push_back(newNode); // определение временного экземлпяра структуры
std::cout << text << std::endl; // вывод строки на экран
}
size = list.size(); // подсчет количества строк
std::cout << std::endl;
}
file.close();
// конец чтения файла inlet.in
// заполнение rand, prev и next
for (int i = 0; i < size; i++)
{
if (i == 0) { (*list[i]).prev = nullptr; }
else (*list[i]).prev = list[i - 1];
if (i == size-1) { (*list[i]).next = nullptr; }
else (*list[i]).next = list[i + 1];
if (value[i] < 0)
{
(*list[i]).rand = nullptr;
}
else (*list[i]).rand = list[value[i]];
}
// конец блока заполнения rand, prev и next
// вывод объектов list на экран
for (int i = 0; i < size; i++) // вывод на экран полученной структуры
{
std::cout << " object " << i << std::endl;
std::cout << "Data" << i << " = " << (*list[i]).data << std::endl;
std::cout << "Prev" << i << " = " << (*list[i]).prev << std::endl;
std::cout << "Next" << i << " = " << (*list[i]).next << std::endl;
std::cout << "Rand" << i << " = " << (*list[i]).rand << std::endl;
}
// конец вывода объектов на экран
// начало записи в файл outlet.out
outfile.open("outlet.out" , std::ios::out | std::ios::binary);
if (outfile.is_open()) {
for (int i = 0; i < size; i++)
{
int len = list[i]->data.size();
outfile.write((char*)&len, sizeof(len)); // длина строки
outfile.write(list[i]->data.c_str(), len); // сама строка
outfile.write((char*)&value[i], sizeof(int)); // rand индекс
}
}
outfile.close();
// конец записи в файл outlet.out
// начало чтения файла outlet.out
std::ifstream infile;
std::vector<ListNode*> new_list; // новый список
std::vector<int> rand_indices; // для хранения индексов
infile.open("outlet.out", std::ios::binary);
if (infile.is_open())
{
while (true)
{
int len;
if (!infile.read((char*)&len, sizeof(len)))
break;
std::string data(len, '\0');
infile.read(&data[0], len); // чтение строки
int rand_index;
infile.read((char*)&rand_index, sizeof(int)); // читаем rand_index
ListNode* node = new ListNode(); // создаём узел
(*node).data = data;
new_list.push_back(node);
rand_indices.push_back(rand_index);
}
}
infile.close();
// конец чтения файла outlet.out
int new_size = new_list.size();
// заполнение rand, prev и next для new list
for (int i = 0; i < new_size; i++)
{
if (i == 0) (*new_list[i]).prev = nullptr;
else (*new_list[i]).prev = new_list[i - 1];
if (i == new_size - 1) (*new_list[i]).next = nullptr;
else (*new_list[i]).next = new_list[i + 1];
if (rand_indices[i] >= 0 && rand_indices[i] < new_size)
(*new_list[i]).rand = new_list[rand_indices[i]];
else
(*new_list[i]).rand = nullptr;
}
// конец заполнения rand, prev и next для new list
// вывод объектов new_list на экран
for (int i = 0; i < size; i++) // вывод на экран полученной структуры
{
std::cout << " object " << i << std::endl;
std::cout << "Data" << " = " << (*new_list[i]).data << std::endl;
std::cout << "Prev" << " = " << (*new_list[i]).prev << std::endl;
std::cout << "Next" << " = " << (*new_list[i]).next << std::endl;
std::cout << "Rand" << " = " << (*new_list[i]).rand << std::endl;
}
// конец вывода объектов new_list на экран
return 0;
}