173 lines
4.6 KiB
C++
173 lines
4.6 KiB
C++
#include <cstring>
|
||
#include <iostream>
|
||
|
||
using namespace std;
|
||
|
||
class DynArr {
|
||
private:
|
||
int* arr;
|
||
uint size;
|
||
uint width;
|
||
uint height;
|
||
|
||
public:
|
||
void set(uint h, uint w, int value)
|
||
{
|
||
if (h < height && w < width) {
|
||
if (-100 <= value && value <= 100) {
|
||
arr[h * width + w] = value;
|
||
} else {
|
||
arr[h * width + w] = value % 100;
|
||
cerr << "Tried to set element " << h << " x " << w << " with " << value << " but this value is not in [-100; 100]. Will assign (val % 10)\n";
|
||
}
|
||
|
||
} else {
|
||
cerr << "Tried to set element " << h << " x " << w << " with " << value << " but the array size is " << size << ". Skipping.\n";
|
||
}
|
||
}
|
||
int get(uint h, uint w)
|
||
{
|
||
if (h < height && w < width) {
|
||
return arr[h * width + w];
|
||
}
|
||
cerr << "Tried to get element " << h << " x " << w << " from an array with size " << size << ". Returning 0.\n";
|
||
return 0;
|
||
}
|
||
uint length()
|
||
{
|
||
return size;
|
||
}
|
||
uint getWidth()
|
||
{
|
||
return width;
|
||
}
|
||
uint getHeight()
|
||
{
|
||
return height;
|
||
}
|
||
void print()
|
||
{
|
||
cout << "Size is " << width << " x " << height << endl;
|
||
for (uint i = 0; i < height; i++) {
|
||
for (uint j = 0; j < width; j++)
|
||
cout << arr[i * width + j] << " ";
|
||
cout << "\n";
|
||
}
|
||
cout << "\n";
|
||
}
|
||
DynArr operator+(const DynArr& second)
|
||
{
|
||
DynArr result(*this);
|
||
uint target_height, target_width;
|
||
if (second.height > height) {
|
||
target_height = height;
|
||
} else {
|
||
target_height = second.height;
|
||
}
|
||
if (second.width > width) {
|
||
target_width = width;
|
||
} else {
|
||
target_width = second.width;
|
||
}
|
||
|
||
for (uint i = 0; i < target_height; i++) {
|
||
for (uint j = 0; j < target_width; j++) {
|
||
result.set(i, j, result.get(i, j) + second.arr[i * second.height + j]);
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
DynArr operator-(const DynArr& second)
|
||
{
|
||
DynArr result(*this);
|
||
uint target_height, target_width;
|
||
if (second.height > height) {
|
||
target_height = height;
|
||
} else {
|
||
target_height = second.height;
|
||
}
|
||
if (second.width > width) {
|
||
target_width = width;
|
||
} else {
|
||
target_width = second.width;
|
||
}
|
||
|
||
for (uint i = 0; i < target_height; i++) {
|
||
for (uint j = 0; j < target_width; j++) {
|
||
result.set(i, j, result.get(i, j) - second.arr[i * second.height + j]);
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
DynArr(uint dwidth, uint dheight)
|
||
{
|
||
arr = new int[dwidth * dheight];
|
||
width = dwidth;
|
||
height = dheight;
|
||
size = width * height;
|
||
for (uint i = 0; i < size; i++) {
|
||
arr[i] = rand() % 100;
|
||
}
|
||
}
|
||
DynArr(const DynArr& original)
|
||
{
|
||
size = original.size;
|
||
height = original.height;
|
||
width = original.width;
|
||
arr = new int[original.size];
|
||
memcpy(arr, original.arr, sizeof(int) * size);
|
||
}
|
||
~DynArr()
|
||
{
|
||
delete[] arr;
|
||
}
|
||
};
|
||
|
||
int main()
|
||
{
|
||
int i, j, temp;
|
||
cout << "Введите размер первого массива в формате h w: ";
|
||
cin >> i;
|
||
cin >> j;
|
||
DynArr arr(i, j);
|
||
i = 0;
|
||
j = 0;
|
||
while (i >= 0) {
|
||
cout << "Введите координаты элемента через пробел или -1 чтобы закончить ввод: ";
|
||
cin >> i;
|
||
if (i >= 0) {
|
||
cin >> j;
|
||
cout << "Введите его значение: ";
|
||
cin >> temp;
|
||
arr.set(i, j, temp);
|
||
}
|
||
}
|
||
cout << "Первый массив:\n";
|
||
arr.print();
|
||
|
||
cout << "Введите размер второго массива: ";
|
||
cin >> i;
|
||
cin >> j;
|
||
DynArr arr2(i, j);
|
||
i = 0;
|
||
while (i >= 0) {
|
||
cout << "Введите координаты элемента через пробел или -1 чтобы закончить ввод: ";
|
||
cin >> i;
|
||
if (i >= 0) {
|
||
cin >> j;
|
||
cout << "Введите его значение: ";
|
||
cin >> temp;
|
||
arr.set(i, j, temp);
|
||
}
|
||
}
|
||
cout << "Первый массив:\n";
|
||
arr.print();
|
||
cout << "Второй массив:\n";
|
||
arr2.print();
|
||
cout << "Первый + второй:\n";
|
||
(arr + arr2).print();
|
||
cout << "Первый - второй:\n";
|
||
(arr - arr2).print();
|
||
return 0;
|
||
} |