Advertisement
amsavchenko

Untitled

Sep 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. template <typename TElement>
  8. class Sequence { // <--- КЛАСС ПОСЛЕДОВАТЕЛЬНОСТЬ
  9. protected:
  10. int length;
  11. bool isEmpty;
  12. public:
  13. Sequence();
  14. ~Sequence();
  15. int getLength ();
  16. bool getIsEmpty ();
  17. virtual TElement Get (int index) = 0;
  18. virtual TElement GetFirst() = 0;
  19. virtual TElement GetLast () = 0;
  20. // virtual Sequence<TElement> GetSubsequence (int startIndex, int endIndex) = 0;
  21. virtual void Append (TElement item) = 0;
  22. virtual void Prepend (TElement item) = 0;
  23. virtual void InsertAt (int index, TElement item) = 0;
  24. virtual void Remove (TElement item) = 0;
  25.  
  26. };
  27. template <typename TElement>
  28. Sequence<TElement>::Sequence() {
  29. length = 0;
  30. isEmpty = true;
  31. }
  32.  
  33. template <typename TElement>
  34. Sequence<TElement>::~Sequence() {
  35.  
  36. }
  37. template <typename TElement>
  38. int Sequence<TElement>::getLength() {
  39. return length;
  40. }
  41. template <typename TElement>
  42. bool Sequence<TElement>::getIsEmpty() {
  43. return isEmpty;
  44. }
  45.  
  46.  
  47. template <typename TElement>
  48. class ArraySequence : public Sequence<TElement> { // <--- ПОДКЛАСС ПОСЛЕДОВАТЕЛЬНОСТЬ С МАССИВАМИ
  49. private:
  50. TElement *Array;
  51. public:
  52. ArraySequence();
  53. ~ArraySequence();
  54. TElement Get (int index);
  55. TElement GetFirst();
  56. TElement GetLast ();
  57. ArraySequence<TElement> GetSubsequence (int startIndex, int endIndex);
  58. void Append (TElement item);
  59. void Prepend (TElement item);
  60. void InsertAt (int index, TElement item);
  61. void Remove (TElement item);
  62.  
  63. //
  64. void qwe(TElement *array);
  65. void print_all ();
  66. };
  67.  
  68. class ExceptionOutOfBounds : public exception { // <--- КЛАСС ИСКЛЮЧЕНИЙ выход за границу массива
  69. int wrongIndex;
  70. public:
  71. ExceptionOutOfBounds(int index);
  72. void what() {
  73. cout << "Индекс " << wrongIndex << " oшибка: выход за границы массива " << endl;
  74. }
  75. };
  76. ExceptionOutOfBounds:: ExceptionOutOfBounds(int index) {
  77. wrongIndex = index;
  78. }
  79.  
  80. template <typename TElement>
  81. ArraySequence<TElement>::ArraySequence() {
  82. Array = new TElement;
  83. }
  84.  
  85. template <typename TElement>
  86. ArraySequence<TElement>::~ArraySequence() {
  87. delete Array;
  88. }
  89.  
  90. template <typename TElement>
  91. TElement ArraySequence<TElement>:: Get (int index) {
  92. if (index < 0 || index >= this->length) {
  93. throw ExceptionOutOfBounds(index);
  94. }
  95. return Array[index];
  96.  
  97. }
  98.  
  99.  
  100. template <typename TElement>
  101. TElement ArraySequence<TElement>:: GetFirst () {
  102. return Array[0];
  103. }
  104.  
  105. template <typename TElement>
  106. TElement ArraySequence<TElement>:: GetLast() {
  107. return Array [this->length - 1];
  108. }
  109. template <typename TElement> // удалить потом
  110. void ArraySequence<TElement>:: qwe(TElement *array) {
  111. Array = array;
  112. }
  113.  
  114. template <typename TElement>
  115. void ArraySequence<TElement>:: Append(TElement item) { // Append. вставить в конец
  116. if (this->length == 0) {
  117. this->length ++;
  118. this->isEmpty = false;
  119. Array [0] = item;
  120. }
  121. else {
  122. this->length ++;
  123. TElement *newArray = new TElement [this->length];
  124. for (int i = 0; i < this->length - 1; i++) {
  125. newArray[i] = Array [i];
  126. }
  127. newArray[this->length - 1] = item;
  128. Array = newArray;
  129. }
  130. }
  131.  
  132. template <typename TElement>
  133. void ArraySequence<TElement>:: Prepend(TElement item) { // Prepend. вставить в начало
  134. if (this->length == 0) {
  135. this->length ++;
  136. this->isEmpty = false;
  137. Array [0] = item;
  138. }
  139. else {
  140. this->length ++;
  141. TElement *newArray = new TElement [this->length];
  142. for (int i = 1; i < this->length; i++) {
  143. newArray[i] = Array [i-1];
  144. }
  145. newArray[0] = item;
  146. Array = newArray;
  147. }
  148. }
  149.  
  150. template <typename TElement>
  151. void ArraySequence<TElement>:: InsertAt(int index, TElement item) {
  152. if (index < 0 || index >= this->length) {
  153. throw ExceptionOutOfBounds(index);
  154. }
  155. if (index == 0) {
  156. this->Prepend(item);
  157. }
  158. if (index == this->length) this->Append(item); // или вообще запретить это?
  159. else {
  160. this->length ++;
  161. TElement *newArray = new TElement [this->length];
  162. for (int i = 0; i < index; i ++) {
  163. newArray[i] = Array[i];
  164. }
  165. newArray[index] = item;
  166. for (int i = index + 1; i < this->length; i++) {
  167. newArray [i] = Array [i-1];
  168. }
  169. Array = newArray;
  170. }
  171. }
  172.  
  173.  
  174. template <typename TElement>
  175. void ArraySequence<TElement>:: Remove(TElement item) {
  176. for (int i = 0; i < this->length; i ++) {
  177. //cout << Array[i] << " ";
  178. if (Array[i] == item) {
  179. this->length --;
  180. TElement *newArray = new TElement [this->length];
  181. for (int j = 0; j < i; j ++) {
  182. newArray[j] = Array [j];
  183. }
  184. for (int k = i; k < this->length; k ++) {
  185. newArray[k] = Array[k+1];
  186. }
  187. Array = newArray;
  188. i --;
  189. }
  190. }
  191. }
  192.  
  193. template <typename TElement>
  194. ArraySequence<TElement> ArraySequence<TElement>:: GetSubsequence(int startIndex, int endIndex) {
  195. if (startIndex < 0 || startIndex >= this->length) {
  196. throw ExceptionOutOfBounds(startIndex);
  197. }
  198. if (endIndex < 0 || endIndex >= this->length) {
  199. throw ExceptionOutOfBounds(endIndex);
  200. }
  201. ArraySequence<TElement> Subsequence;
  202. for (int i = startIndex; i <= endIndex; i ++) {
  203. Subsequence.Append(Array[i]);
  204. }
  205. return Subsequence;
  206. }
  207.  
  208.  
  209. template <typename TElement>
  210. void ArraySequence<TElement>:: print_all() {
  211. for (int i = 0; i < this->length; i ++) {
  212. cout << Array[i] << endl;
  213. }
  214. }
  215.  
  216. void TestArraySequence() {
  217. cout << "--- НАЧАЛО ТЕСТИРОВАНИЯ ---" << endl;
  218. ArraySequence<int> testArray;
  219.  
  220. if (testArray.getLength() == 0) cout << "1 - OK" << endl;
  221. else cout << "1 - Mistake" << endl;
  222.  
  223. testArray.Append(23);
  224.  
  225. if (testArray.getLength() == 1) cout << "2 - OK" << endl;
  226. else cout << "2 - Mistake" << endl;
  227.  
  228. if (testArray.GetFirst() == 23) cout << "3 - OK" << endl;
  229. else cout << "3 - Mistake" << endl;
  230.  
  231. if (testArray.GetLast() == 23) cout << "4 - OK" << endl;
  232. else cout << "4 - Mistake" << endl;
  233.  
  234. try {
  235. if (testArray.Get(0) == 23) cout << "5 - OK" << endl;
  236. else cout << "5 - Mistake" << endl;
  237. }
  238. catch (ExceptionOutOfBounds ex){
  239. ex.what();
  240. }
  241.  
  242. try {
  243. cout << testArray.Get(-1) << endl;
  244. }
  245. catch (ExceptionOutOfBounds ex){
  246. ex.what();
  247. }
  248.  
  249. try {
  250. testArray.Get(1);
  251. }
  252. catch (ExceptionOutOfBounds ex){
  253. ex.what();
  254. }
  255.  
  256. testArray.Append(43);
  257.  
  258. if (testArray.getLength() == 2) cout << "6 - OK" << endl;
  259. else cout << "6 - Mistake" << endl;
  260.  
  261. if (testArray.GetFirst() == 23) cout << "7 - OK" << endl;
  262. else cout << "7 - Mistake" << endl;
  263.  
  264. if (testArray.GetLast() == 43) cout << "8 - OK" << endl;
  265. else cout << "8 - Mistake" << endl;
  266.  
  267. try {
  268. if (testArray.Get(0) == 23) cout << "9 - OK" << endl;
  269. else cout << "9 - Mistake" << endl;
  270. }
  271. catch (ExceptionOutOfBounds ex){
  272. ex.what();
  273. }
  274.  
  275. try {
  276. if (testArray.Get(1) == 43) cout << "10 - OK" << endl;
  277. else cout << "10 - Mistake" << endl;
  278. }
  279. catch (ExceptionOutOfBounds ex){
  280. ex.what();
  281. }
  282.  
  283. try {
  284. testArray.Get(-1);
  285. }
  286. catch (ExceptionOutOfBounds ex){
  287. ex.what();
  288. }
  289.  
  290. try {
  291. testArray.Get(2);
  292. }
  293. catch (ExceptionOutOfBounds ex){
  294. ex.what();
  295. }
  296.  
  297. testArray.Prepend(53);
  298.  
  299. if (testArray.getLength() == 3) cout << "11 - OK" << endl;
  300. else cout << "11 - Mistake" << endl;
  301.  
  302. if (testArray.GetFirst() == 53) cout << "12 - OK" << endl;
  303. else cout << "12 - Mistake" << endl;
  304.  
  305. if (testArray.GetLast() == 43) cout << "13 - OK" << endl;
  306. else cout << "13 - Mistake" << endl;
  307.  
  308. try {
  309. if (testArray.Get(0) == 53) cout << "14 - OK" << endl;
  310. else cout << "14 - Mistake" << endl;
  311. }
  312. catch (ExceptionOutOfBounds ex){
  313. ex.what();
  314. }
  315.  
  316. try {
  317. if (testArray.Get(1) == 23) cout << "15 - OK" << endl;
  318. else cout << "15 - Mistake" << endl;
  319. }
  320. catch (ExceptionOutOfBounds ex){
  321. ex.what();
  322. }
  323.  
  324. try {
  325. testArray.Get(-1);
  326. }
  327. catch (ExceptionOutOfBounds ex){
  328. ex.what();
  329. }
  330.  
  331. try {
  332. testArray.Get(3);
  333. }
  334. catch (ExceptionOutOfBounds ex){
  335. ex.what();
  336. }
  337.  
  338. ArraySequence<int> testSubseq = testArray.GetSubsequence(1, 1);
  339.  
  340. if (testSubseq.getLength() == 1) cout << "16 - OK" << endl;
  341. else cout << "16 - Mistake" << endl;
  342.  
  343. if (testSubseq.GetFirst() == 23) cout << "17 - OK" << endl;
  344. else cout << "17 - Mistake" << endl;
  345.  
  346. if (testSubseq.GetLast() == 23) cout << "18 - OK" << endl;
  347. else cout << "18 - Mistake" << endl;
  348.  
  349. cout << "--- КОНЕЦ ТЕСТИРОВАНИЯ ---" << endl;
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement