Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.26 KB | None | 0 0
  1. // OOP_3rdLaba.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include"stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <cstdlib> // для cls
  8. #include <fstream> // для файловых потоков
  9. #include <windows.h> // для setconsole
  10. #include <string> // для string
  11. #include <iomanip> // для setw
  12.  
  13. using namespace std;
  14.  
  15. const int N = 100;
  16. struct avto
  17. {
  18. string mark;
  19. int power;
  20. int capacity;
  21. int weight;
  22. };
  23. struct record
  24. {
  25. avto av;
  26. string fam;
  27. string name;
  28. string otch;
  29. string number;
  30. };
  31.  
  32. struct marka
  33. {
  34. string mrk;
  35. int nm;
  36. };
  37. struct ves
  38. {
  39. int wght;
  40. int nw;
  41. };
  42. struct moshnost
  43. {
  44. int mst;
  45. int nmo;
  46. };
  47.  
  48. class masrecord
  49. {
  50. private:
  51. record x[N];
  52. int n;
  53. public:
  54. masrecord() :n(0) {} // конструктор без параметров
  55. void inputMasRecordFile();
  56. void addRecord();
  57. void outputMasRecord();
  58. void outputMasRecordFile();
  59. void deleteRecord();
  60. void sortPower();
  61. void sortMark();
  62. void sortFamNameOtch();
  63. friend void perechMark(masrecord v1, masmark &v2);
  64. friend void perechWeight(masrecord v1, masweight &v3);
  65. friend void perechPower(masrecord v1, maspower &v4);
  66. };
  67. class masmark
  68. {
  69. private:
  70. marka z[N];
  71. int l;
  72. public:
  73. masmark() :l(0) {}
  74. friend void perechMark(masrecord v1, masmark &v2);
  75. void outputMasMark();
  76. void outputMasMarkFile();
  77. void sortMark();
  78. };
  79. class masweight
  80. {
  81. private:
  82. ves z[N];
  83. int l;
  84. public:
  85. masweight() :l(0) {}
  86. friend void perechWeight(masrecord v1, masweight &v3);
  87. void outputMasMark();
  88. void outputMasMarkFile();
  89. void sortChislo();
  90. };
  91. class maspower
  92. {
  93. private:
  94. moshnost z[N];
  95. int l;
  96. public:
  97. maspower() :l(0) {}
  98. friend void perechPower(masrecord v1, maspower &v4);
  99. void outputMasMark();
  100. void outputMasMarkFile();
  101. void sortChislo();
  102. };
  103.  
  104. class Agregat
  105. {
  106. private:
  107. masrecord w1;
  108. masmark w2;
  109. masweight w3;
  110. maspower w4;
  111. public:
  112. void inputvFile();
  113. void outputv();
  114. void outputw();
  115. void outputwFile();
  116. void perechMark();
  117. Agregat(Agregat &z);
  118. Agregat operator= (Agregat &z);
  119. };
  120.  
  121. void masrecord::addRecord()
  122. {
  123. SetConsoleCP(1251);
  124. SetConsoleOutputCP(1251);
  125. record t;
  126. if (n == N) { cout << "Нет места"; _getch(); return; }
  127. cout << "Марка автомобиля: "; cin >> t.av.mark;
  128. cout << "Мощность двигателя(в лошадиных силах): "; cin >> t.av.power;
  129. cout << "Объем бака(в литрах): "; cin >> t.av.capacity;
  130. cout << "Вес автомобиля(в килограммах): "; cin >> t.av.weight;
  131. cout << "Фамилия владельца: "; cin >> t.fam;
  132. cout << "Имя владельца: "; cin >> t.name;
  133. cout << "Отчество владельца: "; cin >> t.otch;
  134. cout << "Номер автомобиля: "; cin >> t.number;
  135. x[n] = t;
  136. n++;
  137. cout << "Запись добавлена." << endl;
  138. _getch();
  139. }
  140. void masrecord::outputMasRecord()
  141. {
  142. SetConsoleCP(1251);
  143. SetConsoleOutputCP(1251);
  144. int i;
  145. cout << "БД владельцев автомобилей:\n";
  146. cout << left;
  147. cout << " ------------------------------------------------------------------------------------------------\n";
  148. cout << " |Номер |Марка |Мощность |Объем|Вес | | | |Номер |\n";
  149. cout << " |записи|автомобиля|двигателя|бака |автомобиля|Фамилия |Имя |Отчество |автомобиля|\n";
  150. cout << " ------------------------------------------------------------------------------------------------\n";
  151. for (i = 0; i < n; i++)
  152. cout << " |" << setw(6) << i + 1 << "|" << setw(10) << x[i].av.mark << "|" << setw(9) << x[i].av.power << "|" << setw(5) << x[i].av.capacity << "|" << setw(10) << x[i].av.weight << "|" << setw(12) << x[i].fam << "|" << setw(10) << x[i].name << "|" << setw(14) << x[i].otch << "|" << setw(10) << x[i].number << "|" << endl;
  153. cout << " ------------------------------------------------------------------------------------------------\n";
  154. _getch(); return;
  155. }
  156. void masrecord::inputMasRecordFile()
  157. {
  158. SetConsoleCP(1251);
  159. SetConsoleOutputCP(1251);
  160. string file;
  161. ifstream fin;
  162. cout << "Имя файла, который вы хотите открыть: ";
  163. cin >> file;
  164. fin.open(file);
  165. if (fin.fail()) { cout << file << " " << "Невозможно открыть файл...\n"; _getch(); return; }
  166. n = 0;
  167. while (true)
  168. {
  169. fin >> x[n].av.mark >> x[n].av.power >> x[n].av.capacity >> x[n].av.weight >> x[n].fam >> x[n].name >> x[n].otch >> x[n].number;
  170. if (fin.fail()) break;
  171. n++;
  172. }
  173. cout << "Файл введен\n";
  174. fin.close();
  175. _getch();
  176. }
  177. void masrecord::outputMasRecordFile()
  178. {
  179. SetConsoleCP(1251);
  180. SetConsoleOutputCP(1251);
  181. string file;
  182. ofstream fout;
  183. cout << "Имя файла, который вы хотите создать: ";
  184. cin >> file;
  185. fout.open(file);
  186. if (fout.fail()) { cout << file << " Файл не создан...\n"; _getch(); return; }
  187. fout << " ------------------------------------------------------------------------------------------------\n";
  188. fout << " |Номер |Марка |Мощность |Объем|Вес | | | |Номер |\n";
  189. fout << " |записи|автомобиля|двигателя|бака |автомобиля|Фамилия |Имя |Отчество |автомобиля|\n";
  190. fout << " ------------------------------------------------------------------------------------------------\n";
  191. for (int i = 0; i < n; ++i)
  192. {
  193. fout << " |" << setw(6) << i + 1 << "|" << setw(10) << x[i].av.mark << "|" << setw(9) << x[i].av.power << "|" << setw(5) << x[i].av.capacity << "|" << setw(10) << x[i].av.weight << "|" << setw(12) << x[i].fam << "|" << setw(10) << x[i].name << "|" << setw(14) << x[i].otch << "|" << setw(10) << x[i].number << "|" << endl;
  194. }
  195. fout << " ------------------------------------------------------------------------------------------------\n";
  196. fout.close();
  197. cout << "Результаты сохранены в файле " << file; _getch(); return;
  198. }
  199. void masrecord::deleteRecord()
  200. {
  201. int j, i;
  202. char ch;
  203. outputMasRecord();
  204. cout << "Удалить строку: ";
  205. cin >> j;
  206. if (j < 1 || j > n) { cout << "Такой строки нет.\n"; _getch(); return; }
  207. j--; // уменьшаем чтобы обратиться к индексу массива
  208. cout << x[j].av.mark << " " << x[j].av.power << " " << x[j].av.capacity << " " << x[j].av.weight << " " << x[j].fam << " " << x[j].name << " " << x[j].otch << " " << x[j].number << endl;
  209. cout << "Удалить?(y/n): ";
  210. cin >> ch;
  211. if (ch == 'n')return;
  212. if (ch == 'y') {
  213. for (i = j + 1; i < n; i++)
  214. x[i - 1] = x[i];
  215. n--;
  216. cout << "Запись удалена.\n";
  217. _getch();
  218. }
  219. else cout << "Вы ввели неверный вариант выбора.\n"; _getch(); return;
  220. }
  221. void masrecord::sortPower()
  222. {
  223. int i, fl;
  224. record t;
  225. do
  226. {
  227. fl = 0;
  228. for (i = 0; i < n - 1; i++)
  229. if (x[i].av.power > x[i + 1].av.power)
  230. {
  231. fl = 1;
  232. t = x[i];
  233. x[i] = x[i + 1];
  234. x[i + 1] = t;
  235. }
  236. } while (fl == 1);
  237. cout << "Массив упорядочен\n";
  238. _getch();
  239. }
  240. void masrecord::sortMark()
  241. {
  242. int i, fl;
  243. record t;
  244. do
  245. {
  246. fl = 0;
  247. for (i = 0; i < n - 1; i++)
  248. if (x[i].av.mark > x[i + 1].av.mark)
  249. {
  250. fl = 1;
  251. t = x[i];
  252. x[i] = x[i + 1];
  253. x[i + 1] = t;
  254. }
  255. } while (fl == 1);
  256. cout << "Массив упорядочен\n";
  257. _getch();
  258. }
  259. int sravnFamNameOtch(record v, record w)
  260. {
  261. if (v.fam > w.fam) return 1;
  262. if (v.fam < w.fam) return -1;
  263. if (v.name > w.name) return 1;
  264. if (v.name < w.name) return -1;
  265. if (v.otch > w.otch) return 1;
  266. if (v.otch < w.otch) return -1;
  267. return 0;
  268. }
  269. void masrecord::sortFamNameOtch()
  270. {
  271. int i, fl;
  272. record t;
  273. do
  274. {
  275. fl = 0;
  276. for (i = 0; i < n - 1; i++)
  277. if (sravnFamNameOtch(x[i], x[i + 1])>0)
  278. {
  279. fl = 1;
  280. t = x[i];
  281. x[i] = x[i + 1];
  282. x[i + 1] = t;
  283. }
  284. } while (fl == 1);
  285. cout << "Массив упорядочен\n";
  286. _getch();
  287. }
  288.  
  289. Agregat::Agregat(Agregat &z)
  290. {
  291. w1 = z.w1;
  292. w2 = z.w2;
  293. w3 = z.w3;
  294. w4 = z.w4;
  295. }
  296. Agregat Agregat::operator= (Agregat &z)
  297. {
  298. if (this == &z) return *this;
  299. w1 = z.w1;
  300. w2 = z.w2;
  301. w3 = z.w3;
  302. w4 = z.w4;
  303. return *this;
  304. }
  305.  
  306. void Agregat::inputvFile()
  307. {
  308. w1.masrecord::inputMasRecordFile(); // w1.input_from_file()
  309. }
  310. void Agregat::outputv()
  311. {
  312. w1.masrecord::outputMasRecord();
  313. }
  314.  
  315. void Agregat::perechMark() {
  316. perechMark(w1, w2);
  317. }
  318.  
  319. void perechMark(masrecord v1, masmark &v2)
  320. {
  321. int fl, n, l;
  322. n = v1.n;
  323. l = v2.l;
  324. for (int i = 0; i < n; ++i)
  325. {
  326. fl = 0;
  327. for (int j = 0; j < l; ++j)
  328. {
  329. if (v1.x[i].av.mark == v2.z[j].mrk) {
  330. fl = 1;
  331. v2.z[j].nm++;
  332. }
  333. }
  334. if (fl == 0) {
  335. v2.z[l].mrk = v1.x[i].av.mark;
  336. v2.z[l].nm = 1;
  337. l++;
  338. }
  339. }
  340. v2.l = l;
  341. cout << "Перечень создан.\n"; _getch();
  342. }
  343.  
  344. /*void perechMrk(masrecord&c, masrecord&d)
  345. {
  346. int i, j, fl, n, l;
  347. l = 0;
  348. n = c.n;
  349. for (i = 0; i < n; i++)
  350. {
  351. for(j=0;j<l;j++)
  352. if (x[i].av.mark==d.y[j].mark)
  353. {
  354. fl = 1; d.y[j].nm++;
  355. }
  356. if (fl == 0)
  357. {
  358. d.y[l].mark == c.x[i].av.mark;
  359. d.y[l].nm = 1;
  360. l++;
  361. }
  362. }
  363. d.l = l;
  364. cout << ""; _getch();
  365. }
  366. */
  367.  
  368. /*masrecord::masrecord(masrecord&y)
  369. {
  370. int i, j;
  371. n = y.n;
  372. if (n == 0) x == NULL;
  373. else {
  374. if (x == NULL) { cout << "Нет памяти.\n"; _getch(); exit(1); }
  375. for (i = 0; i < n; i++)
  376. x[i] = y.x[i];
  377. }
  378. l = y.l;
  379. if (l == 0) z == NULL;
  380. else {
  381. if (z == NULL) { cout << "Нет памяти.\n"; _getch(); exit(1); }
  382. for (j = 0; j <l; j++)
  383. z[j] = y.z[j];
  384. }
  385. cout << "Сработал конструктор копирования.\n";
  386. }
  387. masrecord& masrecord::operator=(masrecord& y)
  388. {
  389. if (this == &y) return *this;
  390. if (n == y.n)
  391. for (int i = 0; i < n; i++)
  392. x[i] = y.x[i];
  393. else {
  394. if (x != NULL)delete[]x;
  395. n = y.n;
  396. if (x == NULL) {}
  397. for (int i = 0; i < n; i++)
  398. x[i] = y.x[i];
  399. }
  400. if (l == y.l)
  401. for (int j = 0; j < l; j++)
  402. z[j] = y.z[j];
  403. else {
  404. if (z != NULL)delete[]x;
  405. l = y.l;
  406. if (z == NULL) {}
  407. for (int j = 0; j < l; j++)
  408. z[j] = y.z[j];
  409. }
  410. cout << "Присваивание выполнено.\n"; _getch();
  411. return *this;
  412. }
  413. */
  414. int main()
  415. {
  416. setlocale(0, "");
  417. masrecord a;
  418. Agregat aa;
  419. int j;
  420. while (true)
  421. {
  422. system("cls");
  423. cout << "1-Загрузка из файла\n";
  424. cout << "2-Сохранение результатов обработки в файл\n";
  425. cout << "3-Добавление записи\n";
  426. cout << "4-Удаление записи\n";
  427. cout << "5-Вывод на экран дисплея данных и результатов обработки\n";
  428. cout << "6-Алфавитная сортировка по Ф.И.О.\n";
  429. cout << "7-Алфавитная сортировка по названию марки автомобиля\n";
  430. cout << "8-Числовая сортировка по мощности двигателя\n";
  431. cout << "9-Перечень марок автомобиля с указанием их числа (результат отсортирован по названию марки автомобиля в алфавитном порядке)\n";
  432. cout << "10-Перечень весов автомобиля с указанием числа автомобилей (результат отсортирован по числу автомобилей)\n";
  433. cout << "11-Перечень мощностей двигателя с указанием числа автомобилей (результат отсортирован по мощности двигателя)\n";
  434. cout << "12-Сохранения перечня в файл\n";
  435. cout << "13-Вывод перечня\n";
  436. cout << "14-Проверка работы конструктора копирования\n";
  437. cout << "15-Проверка работы перегруженной операции присваивания\n";
  438. cout << "16-Выход из программы\n";
  439. cout << "\nВаш выбор (1-16):";
  440. cin >> j;
  441. switch (j)
  442. {
  443. case 1:aa.inputvFile(); break;
  444. case 2:a.outputMasRecordFile(); break;
  445. case 3:a.addRecord(); break;
  446. case 4:a.deleteRecord(); break;
  447. case 5:aa.outputv(); break;
  448. case 6:a.sortFamNameOtch(); break;
  449. case 7:a.sortMark(); break;
  450. case 8:a.sortPower(); break;
  451. case 9:
  452. {
  453. aa.perechMark();
  454. // aa.sortMarkPerech(); break;
  455. }
  456. case 10:
  457. {
  458. //aa.perechWeight();
  459. //aa.sortKolichestvoPerech(); break;
  460. }
  461. case 11:
  462. {
  463. // aa.perechPower();
  464. // aa.sortPowerPerech(); break;
  465. }
  466. case 12:aa.outputw(); break;
  467. case 13:aa.outputwFile(); break;
  468. /*case 14:
  469. {
  470. masrecord b(a);
  471. a.outputMasRecord();
  472. b.outputMasRecord();
  473. a.outputPerech();
  474. b.outputPerech(); break;
  475. }
  476. case 15:
  477. {
  478. c = d = a;
  479. a.outputMasRecord();
  480. a.outputoutputPerech();
  481. c.outputMasRecord();
  482. c.outputPerech();
  483. d.outputMasRecord();
  484. d.outputPerech(); break;
  485. }
  486. */
  487. case 15:cout << "Завершение работы..."; _getch(); return 0;
  488. default:cout << "Нет такого пункта в меню"; _getch(); break;
  489. }
  490.  
  491.  
  492. }
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement