HLPL2/Sem3/task1.cpp

160 lines
3.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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 {
throw out_of_range("Tried to set element out of array bounds!");
}
}
int get(uint index)
{
if (index < size) {
return arr[index];
}
throw out_of_range("Tried to get element out of array bounds!");
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;
DynArr arr2(i);
i = 0;
while (i >= 0) {
try {
cout << "Введите номер элемента: ";
cin >> i;
if (i >= 0) {
cout << "Введите его значение: ";
cin >> temp;
arr2.set(i, temp);
}
} catch (const std::out_of_range& e) {
cerr << e.what() << endl;
} catch (const std::invalid_argument& e) {
cerr << "Invalid number" << endl;
}
}
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;
}