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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
| #include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
#include <string>
#include <locale.h>
#include <fstream>
using namespace std;
/////////////////////////////////////////
struct Car
{
string brand;
string model;
double price;
int mileage;
int age;
bool vip;
};
struct Node
{
Car *pcar;
Node *next;
Node *prev;
Node *multy;
};
Node *head = 0, *tail = 0, *head_m = 0; // *head_m - для мультисписка
string file_name; // переменная под имя файла
void instruction()
{
cout << endl
<< "Эта программа предназначена, чтобы сохранять и просматривать" << endl
<< "записи о <<Прокате авто>> ." << endl
<< "Пункт меню <<5. Сохранить файл БД>> предназначен для сохранения данных " << endl
<< "в файл, имя которого выбирается пользователем, находящийся в папке выполняемой программы." << endl
<< "Пункт меню <<6. Загрузить файл БД>> предназначен для загрузки данных" << endl
<< "из файла, имя которого выбирается пользователем, находящегося в папке выполняемой программы." << endl
<< "Пункт меню <<9. Сохранить, как HTML файл>> предназначен для сохранения данных" << endl
<< "в файл, имя которого выбирается пользователем (.html)." << endl
<< "Пункт меню <<\"-\" Выход>> предназначен для завершения работы программы." << endl;
}
void AddNodeToHead(string brand_, string model_, double price_, int mileage_, int age_, bool vip_)
{
Node *pnode;
Car *p;
try { pnode = new Node; p = new Car; }
catch (...){ cout << "Нет памяти!" << endl; _getch(); _exit(-1); }
if (head == 0)
{
pnode->next = 0;
pnode->prev = 0;
head = pnode;
tail = pnode;
p->brand = brand_;
p->model = model_;
p->price = price_;
p->mileage = mileage_;
p->age = age_;
p->vip = vip_;
pnode->pcar = p;
}
else
{
pnode->next = head;
pnode->prev = 0;
head->prev = pnode;
head = pnode;
p->brand = brand_;
p->model = model_;
p->price = price_;
p->mileage = mileage_;
p->age = age_;
p->vip = vip_;
pnode->pcar = p;
}
pnode->multy = 0;
if (vip_)
{
pnode->multy = head_m;
head_m = pnode;
}
}
void AddRecord()
{
string brand_;
string model_;
double price_;
int mileage_;
int age_;
bool vip_;
cout << "Введите фирму авто: ";
cin >> brand_;
cout << "Введите модель авто: ";
cin >> model_;
cout << "Введите цену авто: ";
cin >> price_;
cout << "Введите пробег авто: ";
cin >> mileage_;
cout << "Введите возраст авто: ";
cin >> age_;
cout << "Введите наличие категории vip: ";
cin >> vip_;
cout << endl;
AddNodeToHead(brand_, model_, price_, mileage_, age_, vip_);
}
void PrintAll()
{
Node *cur = head;
int n_ost = 0;
cout << endl;
while (cur != 0)
{
n_ost++;
cout << "[" << cur->pcar->brand << ", " << cur->pcar->model << ", " << cur->pcar->price << ", "
<< cur->pcar->mileage << ", " << cur->pcar->age << ", " << cur->pcar->vip << "]" << endl;
if (n_ost % 20 == 0)
_getch();
cur = cur->next;
}
}
void PrintMulty()
{
Node *cur = head_m;
cout << endl;
while (cur != 0)
{
cout << "[" << cur->pcar->brand << ", " << cur->pcar->model << ", " << cur->pcar->price << ", "
<< cur->pcar->mileage << ", " << cur->pcar->age << ", " << cur->pcar->vip << "]" << endl;
cur = cur->next;
}
}
void Savetxt()
{
ofstream f_out;
cout << "Введите имя файла: ";
cin >> file_name;
f_out.open(file_name, ios::out | ios::trunc);
if (!f_out)
{
cout << "Save_F No working!!" << endl;
_getch();
_exit(-1);
}
Node *cur = head;
cout << endl;
while (cur != 0)
{
f_out << cur->pcar->brand << endl;
f_out << cur->pcar->model << endl;
f_out << cur->pcar->price << endl;
f_out << cur->pcar->mileage << endl;
f_out << cur->pcar->age << endl;
f_out << cur->pcar->vip << endl;
cur = cur->next;
}
f_out.close();
cout << endl << "Сохоранено!" << endl;
}
void Read()
{
ifstream f_in;
cout << "Введите имя файла: ";
cin >> file_name;
f_in.open(file_name);
if (!f_in)
{
cout << "Save_F No working!!" << endl;
_getch();
_exit(-2);
}
string brand_;
string model_;
double price_;
int mileage_;
int age_;
bool vip_;
cout << endl;
while (! f_in.eof())
{
if (f_in.bad())
return;
f_in >> brand_;
f_in >> model_;
f_in >> price_;
f_in >> mileage_;
f_in >> age_;
f_in >> vip_;
AddNodeToHead(brand_, model_, price_, mileage_, age_, vip_);
}
f_in.close();
cout << endl << "Загружено!" << endl;
}
void SearchBrand()
{
cout << endl;
cout << "Введите название фирмы для поиска: ";
string search_brand;
cin >> search_brand;
Node *cur = head;
while (cur != 0)
{
if (cur->pcar->brand == search_brand)
{
cout << "[" << cur->pcar->brand << ", " << cur->pcar->model << ", " << cur->pcar->price << ", "
<< cur->pcar->mileage << ", " << cur->pcar->age << ", " << cur->pcar->vip << "]" << endl;
}
cur = cur->next;
}
}
void SearchFirstCharBrand()
{
cout << endl;
cout << "Введите первую букву фирмы для поиска: ";
string search_brand;
cin >> search_brand;
Node *cur = head;
while (cur != 0)
{
if (cur->pcar->brand[0] == search_brand[0])
{
cout << "[" << cur->pcar->brand << ", " << cur->pcar->model << ", " << cur->pcar->price << ", "
<< cur->pcar->mileage << ", " << cur->pcar->age << ", " << cur->pcar->vip << "]" << endl;
}
cur = cur->next;
}
}
void Sort()
{
Node *cur = head, *cur_=head;
string brand_;
string model_;
double price_;
int mileage_;
int age_;
bool vip_;
for (cur = head; cur != 0; cur=cur->next)
for (cur_ = cur->next; cur_ != 0; cur_ = cur_->next)
{
if (cur_->pcar->brand < cur->pcar->brand)
{
brand_ = cur->pcar->brand;
model_ = cur->pcar->model;
price_ = cur->pcar->price;
mileage_ = cur->pcar->mileage;
age_ = cur->pcar->age;
vip_ = cur->pcar->vip;
cur->pcar->brand = cur_->pcar->brand;
cur->pcar->model = cur_->pcar->model;
cur->pcar->price = cur_->pcar->price;
cur->pcar->mileage = cur_->pcar->mileage;
cur->pcar->age = cur_->pcar->age;
cur->pcar->vip = cur_->pcar->vip;
cur_->pcar->brand = brand_;
cur_->pcar->model = model_;
cur_->pcar->price = price_;
cur_->pcar->mileage = mileage_;
cur_->pcar->age = age_;
cur_->pcar->vip = vip_;
}
}
cout << "Отсортированно!" << endl;
}
void SaveHtml()
{
ofstream f_out;
cout << "Введите имя html файла: ";
cin >> file_name;
f_out.open(file_name, ios::out | ios::trunc);
if (!f_out)
{
cout << "Save_F No working!!" << endl;
_getch();
_exit(-1);
}
Node *cur = head;
cout << endl;
f_out << "<html><head><title> Прокат авто </title></head>" << endl;
f_out << "<body bgcolor=#00aaaa>" << endl;
f_out << "<table><tr><td> Фирма </td><td> Модель </td><td> Цена </td><td>Пробег </td><td>Возраст </td><td>vip </td></tr>";
while (cur != 0)
{
f_out << "<tr>" <<"<td>" << cur->pcar->brand << "</td>" << endl;
f_out << "<td>" << cur->pcar->model << "</td>" << endl;
f_out << "<td>" << cur->pcar->price << "</td>" << endl;
f_out << "<td>" << cur->pcar->mileage << "</td>" << endl;
f_out << "<td>" << cur->pcar->age << "</td>" << endl;
f_out << "<td>" << cur->pcar->vip << "</td>" << "</tr>" << endl;
cur = cur->next;
}
f_out << "</table></body></html>"<< endl;
f_out.close();
cout << endl << "Сохоранено!" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");
while (1)
{
cout << "Меню:" << endl;
cout << "1. Инструкция работы с программой" << endl;
cout << "2. Добавить запись" << endl;
cout << "3. Просмотреть все записи" << endl;
cout << "4. Просмотреть записи c категорией vip" << endl; // по мульти указателю
cout << "5. Сохранить файл БД " << endl;
cout << "6. Загрузить файл БД" << endl;
cout << "7. Выполнить поиск записи по фирме" << endl;
cout << "8. Выполнить поиск записи по первой букве фирмы" << endl;
cout << "9. Сохранить, как HTML файл" << endl;
cout << "0. Отсортировать записи по фирме по алфавиту" << endl;
cout << "\"-\" Выход" << endl;
switch (_getch())
{
case '1': instruction(); break;
case '2': AddRecord(); break;
case '3': PrintAll(); break;
case '4': PrintMulty(); break;
case '5': Savetxt(); break;
case '6': Read(); break;
case '7': SearchBrand(); break;
case '8': SearchFirstCharBrand(); break;
case '9': SaveHtml(); break;
case '0': Sort(); break;
case 27:
case '-': _exit(1);
}
cout << endl;
}
_getch();
return 0;
} |