Advertisement
allekco

lab0 ver. 1.0

Nov 3rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. using namespace std;
  4.  
  5. class Sequence {
  6. private:
  7.     int* point; // указатель на массив
  8.     int Length; // размер массива
  9. public:
  10.     void newEmpty() {
  11.         Length = 0; // по умолчанию размер массива = 10 элементов
  12.         point = new int[Length]; // выделить место в памяти для массива
  13.     }
  14.     int getLength() {
  15.         cout << "Lenght of sequence: ";
  16.         cout << Length << endl;
  17.         return Length;
  18.     }
  19.     int getIsEmpty() {
  20.         if (Length == 0) {
  21.             cout << "Sequence is empty" << endl;
  22.             return 1;
  23.         }
  24.         else {
  25.             cout << "Sequence isn't empty" << endl;
  26.             return -1;
  27.         }
  28.     }
  29.     int Get(int index) {
  30.         if ((Length <= index) || (index < 0)) {
  31.             cout << "This element didn't find" << endl;
  32.             return -1;
  33.         }
  34.         else {
  35.             cout << "Element with index " << index << ": ";
  36.             cout << point[index] << endl;
  37.             return point[index];
  38.         }
  39.     }
  40.     void Append(int item) {
  41.         int i;
  42.         int* tmp;
  43.         tmp = new int[Length]; // выделяем память
  44.         for (i = 0; i < Length; i++) {
  45.             tmp[i] = point[i];
  46.         }
  47.         Length = Length + 1; // увеличиваем размер массива на 1
  48.         point = new int[Length];
  49.         for (i = 0; i < Length-1; i++) {
  50.             point[i] = tmp[i];
  51.         }
  52.         point[Length-1] = item;
  53.         delete tmp;
  54.     }
  55.     void Prepend(int item) {
  56.  
  57.     }
  58.     void OutputSequance() {
  59.         int i;
  60.         for ( i = 0; i < Length; i++) {
  61.             cout << point[i] << " ";
  62.         }
  63.         cout << endl;
  64.     }
  65.     int GetFirst() {
  66.         if (Length != 0) {
  67.             cout << "First element: " << point[0] << endl;
  68.             return point[0];
  69.         }
  70.         else {
  71.             cout << "Sequance is empty" << endl;
  72.             return -1;
  73.         }
  74.     }
  75.     int GetLast() {
  76.         if (Length != 0) {
  77.             cout << "Last element: " << point[Length-1] << endl;
  78.             return point[Length-1];
  79.         }
  80.         else {
  81.             cout << "Sequance is empty" << endl;
  82.             return -1;
  83.         }
  84.     }
  85. };
  86.  
  87. int main() {
  88.     Sequence A;
  89.     A.newEmpty();
  90.     A.getLength();
  91.     A.getIsEmpty();
  92.     A.Append(1);
  93.     A.Append(2);
  94.     A.Append(4);
  95.     A.OutputSequance();
  96.     A.getLength();
  97.     A.getIsEmpty();
  98.     A.Get(1);
  99.     A.GetFirst();
  100.     A.GetLast();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement