- #include <iostream>
- #include <string>
- int find(int value, int size, int *arr);
- int read_value(int index, int end, int standard_value, int *offsets, int *values);
- int *copy_array(int *source, int new_size, int amount);
- std::string to_string(int length, int standard_value, int amount, int *offsets, int *values);
- void swap(int A, int B, int *offsets, int *values);
- int main() {
- int standard_value; //podstawowa wartosc vectora
- int length = 0; //dlugosc wektora
- int amount = 0; //ilosc elementow roznych od podst. wartosci wektora
- int arrays_size = 0; //wielkosc tablic przechowujacych wartosci wektora
- int *offsets = nullptr; //indexy poszczegolnych wartosci
- int *values = nullptr; //poszczegolne wartosci
- std::string command; //obsluga menu
- while (true) {
- std::cin >> command;
- if (command == "mvec") {
- std::cin >> length;
- std::cin >> standard_value;
- amount = 0;
- arrays_size = 0;
- delete[] offsets;
- delete[] values;
- offsets = nullptr;
- values = nullptr;
- } else if (command == "def") {
- int index;
- int value;
- std::cin >> index;
- std::cin >> value;
- if (index > 0 && index < length) {
- if (offsets != nullptr && values != nullptr) {
- if (arrays_size == amount) {
- offsets = copy_array(offsets, arrays_size * 2, amount);
- values = copy_array(values, arrays_size * 2, amount);
- }
- if (find(index, amount, offsets) != -1) {
- values[find(index, amount, offsets)] = value;
- }
- else {
- offsets[amount] = index;
- values[amount] = value;
- amount++;
- }
- }
- else {
- offsets = new int[1];
- values = new int[1];
- offsets[0] = index;
- values[0] = value;
- amount = 1;
- arrays_size = 1;
- }
- } else {
- std::cout << "Error\n";
- }
- } else if (command == "len") {
- int new_length;
- std::cin >> new_length;
- if (new_length < length && (offsets != nullptr || offsets != nullptr)) {
- int new_amount = 0;
- while (offsets[new_amount] < new_length) {
- new_amount++;
- }
- offsets = copy_array(offsets, new_amount, new_amount);
- values = copy_array(values, new_amount, new_amount);
- arrays_size = new_amount;
- amount = new_amount;
- }
- length = new_length;
- } else if (command == "print") {
- std::cout << to_string(length, amount, standard_value, offsets, values) << '\n';
- } else if (command == "del") {
- amount = 0;
- arrays_size = 0;
- delete[] offsets;
- delete[] values;
- offsets = nullptr;
- values = nullptr;
- } else {
- std::cout << "Error\n";
- }
- }
- }
- /*******************************************
- * ZWRACA POZYCJE SZUKANEGO ELEMNTU LUB -1 *
- *******************************************/
- int find(int value, int size, int *arr) {
- int i = 0;
- while (i < size && arr[i] != value) {
- i++;
- }
- return i < size ? i : -1;
- }
- /***************************************
- * ODCZYTUJE WARTOSC Z DANEGO ELEMENTU *
- ***************************************/
- int read_value(int index, int end, int standard_value, int *offsets, int *values) {
- int i = 0;
- while (i < end && offsets[i] != index) {
- i++;
- }
- return i < end ? values[i] : standard_value;
- }
- /**************************************************************
- * TWORZY NOWA TABLICE I KOPIUJE DO NIEJ ZAWARTOSC POPRZEDNIEJ*
- **************************************************************/
- int *copy_array(int *source, int new_size, int amount) {
- int *arr = new int[new_size];
- for (int i = 0; i < amount; i++) {
- arr[i] = source[i];
- }
- delete[] source;
- return arr;
- }
- /***************************************
- * ZWRACA STRING REPREZENTUJACY WEKTOR *
- ***************************************/
- std::string to_string(int length, int amount, int standard_value, int *offsets, int *values) {
- std::string result = "len: " + std::to_string(length) + " values: ";
- if (offsets == nullptr || values == nullptr) {
- for (int i = 0; i < length; i++) {
- result += std::to_string(standard_value) + ',';
- }
- } else {
- for (int i = 0; i < length; i++) {
- result += std::to_string(read_value(i, amount, standard_value, offsets, values)) + ',';
- }
- }
- return result;
- }