2020-10-05 06:59:09 +00:00
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
class DynArr {
|
|
|
|
|
private:
|
|
|
|
|
int* arr;
|
|
|
|
|
uint size;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void set(uint index, int value)
|
|
|
|
|
{
|
|
|
|
|
if (index < size) {
|
|
|
|
|
if (-100 <= value && value <= 100) {
|
|
|
|
|
arr[index] = value;
|
|
|
|
|
} else {
|
|
|
|
|
throw invalid_argument("Element is not in [-100; 100]!");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-10-05 08:16:28 +00:00
|
|
|
|
throw out_of_range("Tried to set element out of array bounds!");
|
2020-10-05 06:59:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int get(uint index)
|
|
|
|
|
{
|
|
|
|
|
if (index < size) {
|
|
|
|
|
return arr[index];
|
|
|
|
|
}
|
2020-10-05 08:16:28 +00:00
|
|
|
|
throw out_of_range("Tried to get element out of array bounds!");
|
2020-10-05 06:59:09 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
uint length()
|
|
|
|
|
{
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
void print()
|
|
|
|
|
{
|
|
|
|
|
cout << "Size is " << size << endl;
|
|
|
|
|
for (uint i = 0; i < size; i++) {
|
|
|
|
|
cout << arr[i] << " ";
|
|
|
|
|
}
|
|
|
|
|
cout << "\n";
|
|
|
|
|
}
|
|
|
|
|
void dynAdd(int value)
|
|
|
|
|
{
|
|
|
|
|
if (!(-100 <= value && value <= 100)) {
|
|
|
|
|
throw invalid_argument("Element is not in [-100; 100]!");
|
|
|
|
|
}
|
|
|
|
|
int* newArr = new int[size + 1];
|
|
|
|
|
memcpy(newArr, arr, sizeof(int) * size);
|
|
|
|
|
delete[] arr;
|
|
|
|
|
arr = newArr;
|
|
|
|
|
arr[size] = value;
|
|
|
|
|
size = size + 1;
|
|
|
|
|
}
|
|
|
|
|
DynArr operator+(const DynArr& second)
|
|
|
|
|
{
|
|
|
|
|
DynArr result(second);
|
|
|
|
|
uint target_size = size;
|
|
|
|
|
if (second.size < size)
|
|
|
|
|
target_size = second.size;
|
|
|
|
|
for (uint i = 0; i < target_size; i++) {
|
|
|
|
|
result.set(i, result.get(i) + arr[i]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
DynArr operator-(const DynArr& second)
|
|
|
|
|
{
|
|
|
|
|
DynArr result(second);
|
|
|
|
|
uint target_size = size;
|
|
|
|
|
if (second.size < size)
|
|
|
|
|
target_size = second.size;
|
|
|
|
|
for (uint i = 0; i < target_size; i++) {
|
|
|
|
|
result.set(i, arr[i] - result.get(i));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
DynArr(uint dsize)
|
|
|
|
|
{
|
|
|
|
|
arr = new int[dsize];
|
|
|
|
|
size = dsize;
|
|
|
|
|
for (uint i = 0; i < size; i++) {
|
|
|
|
|
arr[i] = rand() % 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DynArr(const DynArr& original)
|
|
|
|
|
{
|
|
|
|
|
size = original.size;
|
|
|
|
|
arr = new int[original.size];
|
|
|
|
|
memcpy(arr, original.arr, sizeof(int) * size);
|
|
|
|
|
}
|
|
|
|
|
~DynArr()
|
|
|
|
|
{
|
|
|
|
|
delete[] arr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int i, temp;
|
|
|
|
|
cout << "Введите размер первого массива: ";
|
|
|
|
|
cin >> i;
|
|
|
|
|
DynArr arr(i);
|
|
|
|
|
i = 0;
|
|
|
|
|
while (i >= 0) {
|
|
|
|
|
cout << "Введите номер элемента: ";
|
|
|
|
|
cin >> i;
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
cout << "Введите его значение: ";
|
|
|
|
|
cin >> temp;
|
|
|
|
|
arr.set(i, temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << "Первый массив:\n";
|
|
|
|
|
arr.print();
|
|
|
|
|
|
|
|
|
|
cout << "Введите размер второго массива: ";
|
|
|
|
|
cin >> i;
|
2020-10-05 08:16:28 +00:00
|
|
|
|
|
2020-10-05 06:59:09 +00:00
|
|
|
|
DynArr arr2(i);
|
2020-10-05 08:16:28 +00:00
|
|
|
|
|
2020-10-05 06:59:09 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
|
|
while (i >= 0) {
|
|
|
|
|
try {
|
|
|
|
|
cout << "Введите номер элемента: ";
|
|
|
|
|
cin >> i;
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
cout << "Введите его значение: ";
|
|
|
|
|
cin >> temp;
|
|
|
|
|
arr2.set(i, temp);
|
|
|
|
|
}
|
2020-10-05 08:16:28 +00:00
|
|
|
|
} catch (const std::out_of_range& e) {
|
2020-10-05 06:59:09 +00:00
|
|
|
|
cerr << e.what() << endl;
|
2020-10-05 08:16:28 +00:00
|
|
|
|
} catch (const std::invalid_argument& e) {
|
|
|
|
|
cerr << "Invalid number" << endl;
|
2020-10-05 06:59:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << "Первый массив:\n";
|
|
|
|
|
arr.print();
|
|
|
|
|
cout << "Второй массив:\n";
|
|
|
|
|
arr2.print();
|
|
|
|
|
cout << "Первый + второй:\n";
|
|
|
|
|
try {
|
|
|
|
|
(arr + arr2).print();
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
|
}
|
|
|
|
|
cout << "Первый - второй:\n";
|
|
|
|
|
try {
|
|
|
|
|
(arr - arr2).print();
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
|
}
|
|
|
|
|
cout << "Расширенный второй\n";
|
|
|
|
|
arr2.dynAdd(80);
|
|
|
|
|
arr2.print();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|