allekco

roslov_1lab

Dec 26th, 2019
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <random>
  8.  
  9. using namespace std;
  10.  
  11. template <typename TElement> class Sequence {
  12. public:
  13. virtual void Append(TElement item) = 0;
  14. virtual void Prepend(TElement item) = 0;
  15. virtual void InsertAt(int index, TElement item) = 0;
  16. virtual int getLength() = 0;
  17. virtual int getIsEmpty() = 0;
  18. virtual TElement Get(int index) = 0;
  19. virtual TElement GetFirst() = 0;
  20. virtual TElement GetLast() = 0;
  21. virtual void Remove(TElement item) = 0;
  22. virtual void Swap(int i, int j) = 0;
  23. virtual void Show() = 0;
  24.  
  25. };
  26.  
  27. template <typename TElement> struct Node //узел
  28. {
  29. TElement value;
  30. Node<TElement>* next;
  31. Node<TElement>* prev;//Указатели на адреса следующего и предыдущего элементов списка
  32. };
  33.  
  34. template <typename TElement> class ListSequence : public Sequence<TElement> {
  35. private:
  36. int Length;
  37. Node<TElement>* head;
  38. Node<TElement>* tail;
  39. public:
  40.  
  41. ListSequence()
  42. {
  43. head = nullptr;
  44. tail = nullptr;
  45. Length = 0;
  46. }
  47.  
  48. virtual void Append(TElement item) override
  49. {
  50. struct Node<TElement>* temp;
  51. temp = new Node<TElement>;
  52. temp->value = item;
  53. if (head == nullptr) { //если список пустой
  54. head = temp;
  55. tail = temp;
  56. head->prev = nullptr;
  57. tail->next = nullptr;
  58. Length = 1;
  59. }
  60. else { //список не пустой
  61. temp->prev = tail;
  62. tail->next = temp;
  63. tail = temp;
  64. tail->next = nullptr;
  65. Length += 1;
  66. }
  67. }
  68.  
  69. virtual void Prepend(TElement item)override
  70. {
  71. struct Node<TElement>* temp;
  72. struct Node<TElement>* p = head;
  73. temp = new Node<TElement>;
  74. temp->prev = nullptr;
  75. temp->value = item;
  76. if (head == nullptr) { //если список пустой
  77. head = temp;
  78. tail = temp;
  79. temp->next = nullptr;
  80. Length = 1;
  81. }
  82. else { //список не пустой
  83. p->prev = temp;
  84. temp->next = head;
  85. head = temp;
  86. Length += 1;
  87.  
  88. }
  89. }
  90.  
  91. virtual void InsertAt(int index, TElement item) override
  92. {
  93. index = index + 1;
  94. if (index == 1) return Prepend(item);
  95. if (index == Length + 1) return Append(item);
  96. if (index < 1 || index > Length + 1)
  97. cout << "This index out of length" << endl;
  98. else {
  99. struct Node<TElement>* temp1;
  100. struct Node<TElement>* p3;
  101. temp1 = new Node<TElement>;
  102. int i;
  103. p3 = head;
  104. for (i = 1; i < index; i++) {
  105. p3 = p3->next;
  106. }
  107. temp1->value = item;
  108. temp1->prev = p3->prev;
  109. temp1->next = p3;
  110. p3 = p3->prev;
  111. p3->next = temp1;
  112. p3 = temp1->next;
  113. p3->prev = temp1;
  114. Length++;
  115. }
  116. }
  117.  
  118. virtual int getLength() override
  119. {
  120. return Length;
  121. }
  122.  
  123. virtual int getIsEmpty() override
  124. {
  125. if (Length == 0) {
  126. cout << "Sequence is empty" << endl;
  127. return 1;
  128. }
  129. else {
  130. cout << "Sequence isn't empty" << endl;
  131. return -1;
  132. }
  133. }
  134.  
  135. virtual TElement Get(int index) override
  136. {
  137. if ((Length <= index) || (index < 0)) {
  138. cout << "Element with index " << index << " didn't find" << endl;
  139. return -1;
  140. }
  141. else {
  142. int i;
  143. Node<TElement>* temp;
  144. temp = head;
  145. for (i = 0; i < index; i++) {
  146. temp = temp->next;
  147. }
  148. return temp->value;
  149. }
  150. }
  151.  
  152. virtual TElement GetFirst() override
  153. {
  154. return Get(0);
  155. }
  156.  
  157. virtual TElement GetLast() override
  158. {
  159. return Get(Length - 1);
  160. }
  161.  
  162. virtual void Remove(TElement item) override //по значению
  163. {
  164. int i;
  165. Node<TElement>* p = head;
  166. Node<TElement>* p2 = head;
  167. if (head->value == item) {
  168. RemoveFirst();
  169. Length -= 1;
  170. }
  171. else {
  172. for (i = 1; i < Length; i++) {
  173. if (!p->next) {
  174. break;
  175. }
  176. if ((p->next->value == item) && (p->next->next != nullptr)) {
  177. Length -= 1;
  178. p->next = p->next->next;
  179. p = p->next;
  180. p->prev = p2;
  181. break;
  182. }
  183. if ((p->next->value == item) && (p->next->next == nullptr)) {
  184. RemoveLast();
  185. break;
  186. }
  187. p = p->next;
  188. p2 = p2->next;
  189. }
  190. }
  191. }
  192.  
  193. void RemoveIndex(TElement index)
  194. {
  195. int i;
  196. Node<TElement>* p = head;
  197. Node<TElement>* p2 = head;
  198. if (index >= Length || index < 0) {
  199. cout << "Out of lenght" << endl;
  200. }
  201. else
  202. {
  203. if (index == 0) RemoveFirst();
  204. else {
  205. if (index == Length - 1) RemoveLast();
  206. else
  207. {
  208. for (i = 0; i < index - 1; i++)
  209. {
  210. p = p->next;
  211. p2 = p2->next;
  212. }
  213. p2 = p2->next->next;
  214. p->next = p2;
  215. p2->prev = p;
  216. }
  217. }
  218. Length -= 1;
  219. }
  220. }
  221.  
  222. void RemoveFirst()
  223. {
  224. if (head == tail) {
  225. head = nullptr;
  226. tail = nullptr;
  227. }
  228. else {
  229. Node<TElement>* q = head;
  230. head = q->next;
  231. q->next->prev = head;
  232. }
  233. }
  234. void RemoveLast()
  235. {
  236. Node<TElement>* l = tail;
  237. tail = l->prev;
  238. l->prev->next = tail;
  239. tail->next = nullptr;
  240. }
  241.  
  242. ListSequence GetSubsequence(int startIndex, int endIndex)
  243. {
  244. ListSequence B;
  245. Node<TElement>* p = head;
  246. if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
  247. cout << "Error with index" << endl;
  248. }
  249. else {
  250. for (int i = 0; i < startIndex; i++) {
  251. p = p->next;
  252. }
  253. for (int j = startIndex; j <= endIndex; j++) {
  254. B.Append(p->value);
  255. p = p->next;
  256. }
  257. }
  258. return B;
  259. }
  260.  
  261. virtual void Swap(int i, int j) override
  262. {
  263. int a, b;
  264. a = Get(i);
  265. b = Get(j);
  266. RemoveIndex(i);
  267. RemoveIndex(j - 1);
  268. InsertAt(i, b);
  269. InsertAt(j, a);
  270. }
  271.  
  272. virtual void Show() override
  273. {
  274. Node<TElement>* temp = head;
  275. while (temp != nullptr) {
  276. cout << temp->value << " ";
  277. temp = temp->next;
  278. }
  279. cout << endl;
  280. }
  281.  
  282. ~ListSequence() {}
  283. };
  284.  
  285. template <typename TElement> class ArraySequence
  286. : public Sequence<TElement>
  287. {
  288. private:
  289. TElement* point; // указатель на массив
  290. int Length; // размер массива
  291.  
  292. public:
  293. ArraySequence()
  294. {
  295. Length = 0; // по умолчанию размер массива = 10 элементов
  296. point = new TElement[Length]; // выделить место в памяти для массива
  297. }
  298.  
  299. virtual void Append(TElement item) override
  300. {
  301. int i;
  302. TElement* tmp;
  303. tmp = new TElement[Length]; // выделяем память
  304. for (i = 0; i < Length; i++) {
  305. tmp[i] = point[i];
  306. }
  307. Length = Length + 1; // увеличиваем размер массива на 1
  308. point = new TElement[Length];
  309. for (i = 0; i < Length - 1; i++) {
  310. point[i] = tmp[i];
  311. }
  312. point[Length - 1] = item;
  313. delete[] tmp;
  314. }
  315.  
  316. virtual void Prepend(TElement item) override
  317. {
  318. int i;
  319. TElement* tmp;
  320. tmp = new TElement[Length]; // выделяем память
  321. for (i = 0; i < Length; i++) {
  322. tmp[i] = point[i];
  323. }
  324. Length = Length + 1; // увеличиваем размер массива на 1
  325. point = new TElement[Length];
  326. point[0] = item;
  327. for (i = 1; i < Length; i++) {
  328. point[i] = tmp[i - 1];
  329. }
  330. delete[] tmp;
  331. }
  332.  
  333. virtual void InsertAt(int index, TElement item) override
  334. {
  335. if ((Length + 1 <= index) || (index < 0)) {
  336. cout << "This index out of length" << endl;
  337. }
  338. else {
  339. int i;
  340. TElement* tmp;
  341. tmp = new TElement[Length]; // выделяем память
  342. for (i = 0; i < Length; i++) {
  343. tmp[i] = point[i];
  344. }
  345. Length = Length + 1; // увеличиваем размер массива на 1
  346. delete[] point;
  347. point = new TElement[Length];
  348. for (i = 0; i < index && i < Length; i++)
  349. point[i] = tmp[i];
  350. point[index] = item;
  351. for (i = index + 1; i < Length; i++)
  352. point[i] = tmp[i - 1];
  353. delete[] tmp;
  354. }
  355. }
  356.  
  357. virtual int getLength() override
  358. {
  359. return Length;
  360. }
  361.  
  362. virtual int getIsEmpty() override
  363. {
  364. if (Length == 0) {
  365. cout << "Sequence is empty" << endl;
  366. return 1;
  367. }
  368. else {
  369. cout << "Sequence isn't empty" << endl;
  370. return -1;
  371. }
  372. }
  373.  
  374. virtual TElement Get(int index) override
  375. {
  376. if ((Length <= index) || (index < 0)) {
  377. cout << "Element with index " << index << " didn't find" << endl;
  378. return -1;
  379. }
  380. else {
  381. return point[index];
  382. }
  383. }
  384.  
  385. virtual TElement GetFirst() override
  386. {
  387. if (Length != 0) {
  388. return point[0];
  389. }
  390. else {
  391. cout << "Sequance is empty" << endl;
  392. return -1;
  393. }
  394. }
  395.  
  396. virtual TElement GetLast() override
  397. {
  398. if (Length != 0) {
  399. return point[Length - 1];
  400. }
  401. else {
  402. cout << "Sequance is empty" << endl;
  403. return -1;
  404. }
  405. }
  406.  
  407. virtual void Remove(TElement item) override
  408. {
  409. int i = 0, index, key = 0;
  410. while (i < Length) {
  411. if (point[i] == item) {
  412. index = i;
  413. i = Length;
  414. key = 1;
  415. }
  416. i++;
  417. }
  418. if (key) { //удаляем
  419. TElement* tmp;
  420. tmp = new TElement[Length];
  421. for (i = 0; i < Length; i++) {
  422. tmp[i] = point[i];
  423. }
  424. Length = Length - 1;
  425. point = new TElement[Length];
  426. for (i = 0; i < index && i < Length; i++) {
  427. point[i] = tmp[i];
  428. }
  429. for (i = index; i < Length; i++) {
  430. point[i] = tmp[i + 1];
  431. }
  432. }
  433. }
  434.  
  435. void RemoveIndex(TElement index)
  436. {
  437. int i;
  438. TElement* tmp;
  439. tmp = new TElement[Length];
  440. for (i = 0; i < Length; i++) {
  441. tmp[i] = point[i];
  442. }
  443. Length = Length - 1;
  444. delete[] point;
  445. point = new TElement[Length];
  446. for (i = 0; i < index && i < Length; i++) {
  447. point[i] = tmp[i];
  448. }
  449. for (i = index; i < Length; i++) {
  450. point[i] = tmp[i + 1];
  451. }
  452. delete[] tmp;
  453. }
  454.  
  455. ArraySequence GetSubsequence(int startIndex, int endIndex)
  456. {
  457. ArraySequence B;
  458. if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
  459. cout << "Error with index" << endl;
  460. }
  461. else {
  462. for (int i = startIndex; i <= endIndex; i++) {
  463. B.Append(point[i]);
  464. }
  465. }
  466. return B;
  467. }
  468.  
  469. virtual void Swap(int i, int j) override
  470. {
  471. int a, b;
  472. a = Get(i);
  473. b = Get(j);
  474. RemoveIndex(i);
  475. RemoveIndex(j - 1);
  476. InsertAt(i, b);
  477. InsertAt(j, a);
  478. }
  479.  
  480. virtual void Show() override
  481. {
  482. int i;
  483. for (i = 0; i < Length; i++) {
  484. cout << point[i] << " ";
  485. }
  486. cout << endl;
  487. }
  488.  
  489. ~ArraySequence() {}
  490. };
  491.  
  492. template <typename TElement> class Sorter {
  493. public:
  494. virtual void Sort(Sequence<TElement>&) = 0;
  495.  
  496. };
  497.  
  498. template <typename TElement, typename Cmp>
  499. class InsertionSort
  500. : public Sorter<TElement>
  501. {
  502. private:
  503. Cmp m_compare;
  504.  
  505. public:
  506. InsertionSort() = delete;
  507. InsertionSort(Cmp cmp) : m_compare(cmp)
  508. {}; //конструктор
  509. InsertionSort(const InsertionSort&) = default;
  510. InsertionSort& operator=(const InsertionSort&) = default; //оператор копирования
  511. void Sort(Sequence<TElement>& seq) override
  512. {
  513. int i, j;
  514. for (i = 1; i < seq.getLength(); i++) //первый элемент не трогаем
  515. {
  516. for (j = i; j > 0 && m_compare(seq.Get(j - 1), seq.Get(j)) == 1; j--) //ищем место для вставки
  517. {
  518. seq.Swap(j - 1, j);
  519. }
  520. }
  521. };
  522. };
  523.  
  524. template <typename TElement, typename Cmp>
  525. class HeapSort
  526. : public Sorter<TElement>
  527. {
  528. private:
  529. Cmp m_compare;
  530.  
  531. void MaxHeapify(Sequence<TElement>& seq, int n, int i)
  532. {
  533. int l, r, largest = i;
  534. l = 2 * i + 1; //левый
  535. r = 2 * i + 2; //правый
  536.  
  537. // Если левый дочерний элемент больше корня
  538. if ((l < n) && (m_compare(seq.Get(l), seq.Get(largest)) == 1))
  539. {
  540. largest = l;
  541. }
  542.  
  543. // Если правый дочерний элемент больше, чем самый большой элемент на данный момент
  544. if ((r < n) && (m_compare(seq.Get(r), seq.Get(largest)) == 1))
  545. {
  546. largest = r;
  547. }
  548.  
  549. // Если самый большой элемент не корень
  550. if (largest != i)
  551. {
  552. seq.Swap(i, largest);
  553. MaxHeapify(seq, n, largest);
  554. }
  555. }
  556.  
  557.  
  558. public:
  559. HeapSort() = delete;
  560. HeapSort(Cmp cmp) : m_compare(cmp)
  561. {}; //конструктор
  562. HeapSort(const HeapSort&) = default;
  563. HeapSort& operator=(const HeapSort&) = default; //оператор копирования
  564. void Sort(Sequence<TElement>& seq) override
  565. {
  566. int n = seq.getLength();
  567.  
  568. int i;
  569.  
  570. for (i = n / 2 - 1; i >= 0; i--)
  571. {
  572. MaxHeapify(seq, n, i);
  573. }
  574.  
  575. for (i = n - 1; i > 0; i--)
  576. {
  577. seq.Swap(0, i);
  578. MaxHeapify(seq, i, 0);
  579. }
  580. }
  581. };
  582.  
  583.  
  584. int randomazer(int min, int max)
  585. {
  586. static random_device rd;
  587. static mt19937 generator(rd());
  588. static uniform_int_distribution<> distribution(min, max);
  589.  
  590. return distribution(generator);
  591. }
  592.  
  593. void test1(ArraySequence<int> A, ArraySequence<int> A2)
  594. {
  595. A.getIsEmpty();
  596. A.getLength();
  597. cout << endl;
  598. A.Append(23);
  599. A.getLength();
  600. A.GetFirst();
  601. A.GetLast();
  602. A.Get(0);
  603. A.Get(-1);
  604. A.Get(1);
  605. cout << endl;
  606. A.Append(43);
  607. A.Show();
  608. A.getLength();
  609. A.GetFirst();
  610. A.GetLast();
  611. A.Get(0);
  612. A.Get(1);
  613. A.Get(-1);
  614. A.Get(2);
  615. cout << endl;
  616. A.Prepend(53);
  617. A.getLength();
  618. A.GetFirst();
  619. A.GetLast();
  620. A.Get(0);
  621. A.Get(1);
  622. A.Get(-1);
  623. A.Get(3);
  624. cout << endl;
  625. A2 = A.GetSubsequence(1, 1);
  626. A2.getLength();
  627. A2.GetFirst();
  628. A2.GetLast();
  629. cout << endl;
  630. }
  631.  
  632. void test2(ListSequence<int> A, ListSequence<int> A2)
  633. {
  634. A.getIsEmpty();
  635. A.getLength();
  636. cout << endl;
  637. A.Append(23);
  638. A.getLength();
  639. A.GetFirst();
  640. A.GetLast();
  641. A.Get(0);
  642. A.Get(-1);
  643. A.Get(1);
  644. cout << endl;
  645. A.Append(43);
  646. A.Show();
  647. A.getLength();
  648. A.GetFirst();
  649. A.GetLast();
  650. A.Get(0);
  651. A.Get(1);
  652. A.Get(-1);
  653. A.Get(2);
  654. cout << endl;
  655. A.Prepend(53);
  656. A.getLength();
  657. A.GetFirst();
  658. A.GetLast();
  659. A.Get(0);
  660. A.Get(1);
  661. A.Get(-1);
  662. A.Get(3);
  663. cout << endl;
  664. A2 = A.GetSubsequence(1, 1);
  665. A2.getLength();
  666. A2.GetFirst();
  667. A2.GetLast();
  668. cout << endl;
  669. }
  670.  
  671. void lab0()
  672. {
  673. int i = 1;
  674. while (i) {
  675. cout << "Which Sequence do you want?" << endl;
  676. cout << "1 - Array Sequance" << endl;
  677. cout << "2 - List Sequance" << endl;
  678. cout << "0 - EXIT" << endl;
  679. int n;
  680. cin >> n;
  681. cout << endl;
  682. ArraySequence<int> A1;
  683. ArraySequence<int> A2;
  684. ListSequence<int> B1;
  685. ListSequence<int> B2;
  686. switch (n) {
  687. case 1:
  688. test1(A1, A2);
  689. break;
  690. case 2:
  691. test2(B1, B2);
  692. break;
  693. case 0:
  694. i = 0;
  695. break;
  696. default:
  697. break;
  698. }
  699. }
  700. }
  701.  
  702. int main()
  703. {
  704. unsigned int start_time;
  705. unsigned int end_time = clock(); // конечное время
  706. unsigned int search_time;
  707. auto cmp = [](int a, int b) { return (a < b) ? -1 : ((a > b) ? 1 : 0); };
  708. InsertionSort<int, decltype(cmp)> InsSorter(cmp); //InsSorter - обьект сортировки
  709. HeapSort<int, decltype(cmp)> HeapSorter(cmp);
  710. //shSorter.Sort(seq2);
  711. ArraySequence<int> ArSeqInser;
  712. ListSequence<int> ListSeqInser;
  713. ArraySequence<int> ArSeqHeap;
  714. ListSequence<int> ListSeqHeap;
  715.  
  716. int LenghtOfSequance = 20;
  717.  
  718. int k; //Заполнение списка рандомными числами
  719.  
  720. for (int y = 0; y < LenghtOfSequance; y++) {
  721. k = randomazer(-10, 10);
  722. ArSeqHeap.Append(k);
  723. ListSeqHeap.Append(k);
  724. ArSeqInser.Append(k);
  725. ListSeqInser.Append(k);
  726. }
  727.  
  728. int i = 1, j;
  729. while (i)
  730. {
  731. cout << "Which sort do you want?" << endl;
  732. cout << "1 - Intersection sort" << endl;
  733. cout << "2 - Heapsort" << endl;
  734. cout << "0 - EXIT" << endl;
  735. int n;
  736. cin >> n;
  737. cout << endl;
  738. switch (n)
  739. {
  740. case 1: //Intersection
  741. j = 1;
  742. while (j)
  743. {
  744. cout << "you choose INTERSECTION SORT" << endl;
  745. cout << "Which sequance do you want?" << endl;
  746. cout << "1 - List" << endl;
  747. cout << "2 - Array" << endl;
  748. cout << "0 - BACK" << endl;
  749. cin >> n;
  750. cout << endl;
  751. switch (n)
  752. {
  753. case 1:
  754. cout << "Before: ";
  755. ListSeqInser.Show();
  756. start_time = clock();
  757. InsSorter.Sort(ListSeqInser);
  758. end_time = clock();
  759. cout << "After: ";
  760. ListSeqInser.Show();
  761. cout << "Time: " << end_time - start_time << endl;
  762. break;
  763. case 2:
  764. cout << "Before: ";
  765. ArSeqInser.Show();
  766. start_time = clock();
  767. InsSorter.Sort(ArSeqInser);
  768. end_time = clock();
  769. cout << "After: ";
  770. ArSeqInser.Show();
  771. cout << "Time: " << end_time - start_time << endl;
  772. break;
  773. case 0:
  774. j = 0;
  775. break;
  776. default:
  777. break;
  778. }
  779.  
  780. }
  781. break;
  782. case 2: //Heap sort
  783. j = 1;
  784. while (j)
  785. {
  786. cout << "you choose HEAP SORT" << endl;
  787. cout << "Which sequance do you want?" << endl;
  788. cout << "1 - List" << endl;
  789. cout << "2 - Array" << endl;
  790. cout << "0 - BACK" << endl;
  791. cin >> n;
  792. cout << endl;
  793. switch (n)
  794. {
  795. case 1:
  796. cout << "Before: ";
  797. ListSeqHeap.Show();
  798. start_time = clock();
  799. HeapSorter.Sort(ListSeqHeap);
  800. end_time = clock();
  801. cout << "After: ";
  802. ListSeqHeap.Show();
  803. cout << "Time: " << end_time - start_time << endl;
  804. break;
  805. case 2:
  806. cout << "Before: ";
  807. ArSeqHeap.Show();
  808. start_time = clock();
  809. HeapSorter.Sort(ArSeqHeap);
  810. end_time = clock();
  811. cout << "After: ";
  812. ArSeqHeap.Show();
  813. cout << "Time: " << end_time - start_time << endl;
  814. break;
  815. case 0:
  816. j = 0;
  817. break;
  818. default:
  819. break;
  820. }
  821.  
  822. }
  823. break;
  824. case 0:
  825. i = 0;
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831.  
  832. return 0;
  833. }
Advertisement
Add Comment
Please, Sign In to add comment