Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба2 / Laba 2 (3)
.cpp#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;
class my_vector {
private:
int len;
int *v;
public:
my_vector(int length = 0) : len(length) {
v = new int[len];
for (int i = 0; i < len; i++) v[i] = 0;
}
my_vector(const my_vector& other) : len(other.len) {
v = new int[len];
for (int i = 0; i < len; i++) v[i] = other.v[i];
}
~my_vector() {
delete[] v;
}
my_vector& operator=(const my_vector& other) {
if (this != &other) {
delete[] v;
len = other.len;
v = new int[len];
for (int i = 0; i < len; i++) v[i] = other.v[i];
}
return *this;
}
double length() {
double sum = 0;
for (int i = 0; i < len; i++) sum += v[i] * v[i];
return sqrt(sum);
}
int dot(const my_vector& other) {
int result = 0;
for (int i = 0; i < len; i++) result += v[i] * other.v[i];
return result;
}
my_vector multiply(int constant) {
my_vector result(len);
for (int i = 0; i < len; i++) result.v[i] = v[i] * constant;
return result;
}
my_vector add(const my_vector& other) {
my_vector result(len);
for (int i = 0; i < len; i++) result.v[i] = v[i] + other.v[i];
return result;
}
my_vector& next() {
for (int i = 0; i < len; i++) v[i]++;
return *this;
}
void set(int index, int value) { v[index] = value; }
int get(int index) const { return v[index]; }
int size() const { return len; }
};
int main() {
SetConsoleOutputCP(65001);
my_vector v1(3), v2(3);
v1.set(0, 1); v1.set(1, 2); v1.set(2, 3);
v2.set(0, 4); v2.set(1, 5); v2.set(2, 6);
cout << "Длина вектора v1: " << v1.length() << endl;
cout << "Скалярное произведение v1 и v2: " << v1.dot(v2) << endl;
cout << "Координаты v1 до next(): " << v1.get(0) << ", " << v1.get(1) << ", " << v1.get(2) << endl;
v1.next();
cout << "Координаты v1 после одного next(): " << v1.get(0) << ", " << v1.get(1) << ", " << v1.get(2) << endl;
v1.next().next();
cout << "Координаты v1 после v1.next().next(): " << v1.get(0) << ", " << v1.get(1) << ", " << v1.get(2) << endl;
cout << "v.next().next() увеличивает координаты дважды" << endl;
return 0;
}
Соседние файлы в папке Лаба2
