Facebook
From Ungracious Matamata, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 606
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. int find(int value, int size, int *arr);
  6. int read_value(int index, int end, int standard_value, int *offsets, int *values);
  7. int *copy_array(int *source, int new_size, int amount);
  8. std::string to_string(int length, int standard_value, int amount, int *offsets, int *values);
  9. void swap(int A, int B, int *offsets, int *values);
  10.  
  11. int main() {
  12.         int standard_value;                     //podstawowa wartosc vectora
  13.         int length = 0;                         //dlugosc wektora
  14.         int amount = 0;                         //ilosc elementow roznych od podst. wartosci wektora
  15.         int arrays_size = 0;            //wielkosc tablic przechowujacych wartosci wektora
  16.         int *offsets = nullptr;         //indexy poszczegolnych wartosci
  17.         int *values = nullptr;          //poszczegolne wartosci
  18.  
  19.         std::string command;            //obsluga menu
  20.         while (true) {
  21.                 std::cin >> command;
  22.  
  23.                 if (command == "mvec") {
  24.                         std::cin >> length;
  25.                         std::cin >> standard_value;
  26.                         amount = 0;
  27.                         arrays_size = 0;
  28.  
  29.                         delete[] offsets;
  30.                         delete[] values;
  31.  
  32.                         offsets = nullptr;
  33.                         values = nullptr;
  34.  
  35.                 } else if (command == "def") {
  36.                         int index;
  37.                         int value;
  38.                         std::cin >> index;
  39.                         std::cin >> value;
  40.  
  41.                         if (index > 0 && index < length) {
  42.                                 if (offsets != nullptr && values != nullptr) {
  43.                                         if (arrays_size == amount) {
  44.                                                 offsets = copy_array(offsets, arrays_size * 2, amount);
  45.                                                 values = copy_array(values, arrays_size * 2, amount);
  46.                                         }
  47.  
  48.                                         if (find(index, amount, offsets) != -1) {
  49.                                                 values[find(index, amount, offsets)] = value;
  50.                                         }
  51.                                         else {
  52.                                                 offsets[amount] = index;
  53.                                                 values[amount] = value;
  54.                                                 amount++;
  55.                                         }
  56.  
  57.                                 }
  58.                                 else {
  59.                                         offsets = new int[1];
  60.                                         values = new int[1];
  61.  
  62.                                         offsets[0] = index;
  63.                                         values[0] = value;
  64.                                         amount = 1;
  65.                                         arrays_size = 1;
  66.                                 }
  67.                         } else {
  68.                                 std::cout << "Error\n";
  69.                         }
  70.  
  71.                 } else if (command == "len") {
  72.                         int new_length;
  73.                         std::cin >> new_length;
  74.  
  75.                         if (new_length < length && (offsets != nullptr || offsets != nullptr)) {
  76.                                 int new_amount = 0;
  77.                                 while (offsets[new_amount] < new_length) {
  78.                                         new_amount++;
  79.                                 }
  80.  
  81.                                 offsets = copy_array(offsets, new_amount, new_amount);
  82.                                 values = copy_array(values, new_amount, new_amount);
  83.  
  84.                                 arrays_size = new_amount;
  85.                                 amount = new_amount;
  86.                         }
  87.                         length = new_length;
  88.  
  89.                 } else if (command == "print") {
  90.                         std::cout << to_string(length, amount, standard_value, offsets, values) << '\n';
  91.  
  92.                 } else if (command == "del") {
  93.                         amount = 0;
  94.                         arrays_size = 0;
  95.  
  96.                         delete[] offsets;
  97.                         delete[] values;
  98.  
  99.                         offsets = nullptr;
  100.                         values = nullptr;
  101.                 } else {
  102.                         std::cout << "Error\n";
  103.                 }
  104.         }
  105. }
  106.  
  107.  
  108. /*******************************************
  109.  * ZWRACA POZYCJE SZUKANEGO ELEMNTU LUB -1 *
  110.  *******************************************/
  111. int find(int value, int size, int *arr) {
  112.         int i = 0;
  113.         while (i < size && arr[i] != value) {
  114.                 i++;
  115.         }
  116.  
  117.         return i < size ? i : -1;
  118. }
  119.  
  120.  
  121. /***************************************
  122.  * ODCZYTUJE WARTOSC Z DANEGO ELEMENTU *
  123.  ***************************************/
  124. int read_value(int index, int end, int standard_value, int *offsets, int *values) {
  125.         int i = 0;
  126.         while (i < end && offsets[i] != index) {
  127.                 i++;
  128.         }
  129.  
  130.         return i < end ? values[i] : standard_value;
  131. }
  132.  
  133. /**************************************************************
  134.  * TWORZY NOWA TABLICE I KOPIUJE DO NIEJ ZAWARTOSC POPRZEDNIEJ*
  135.  **************************************************************/
  136. int *copy_array(int *source, int new_size, int amount) {
  137.         int *arr = new int[new_size];
  138.  
  139.         for (int i = 0; i < amount; i++) {
  140.                 arr[i] = source[i];
  141.         }
  142.  
  143.         delete[] source;
  144.         return arr;
  145. }
  146.  
  147. /***************************************
  148.  * ZWRACA STRING REPREZENTUJACY WEKTOR *
  149.  ***************************************/
  150. std::string to_string(int length, int amount, int standard_value, int *offsets, int *values) {
  151.         std::string result = "len: " + std::to_string(length) + " values: ";
  152.  
  153.         if (offsets == nullptr || values == nullptr) {
  154.                 for (int i = 0; i < length; i++) {
  155.                         result += std::to_string(standard_value) + ',';
  156.                 }
  157.         } else {
  158.                 for (int i = 0; i < length; i++) {
  159.                         result += std::to_string(read_value(i, amount, standard_value, offsets, values)) + ',';
  160.                 }
  161.         }
  162.  
  163.         return result;
  164. }