-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_2.cpp
More file actions
121 lines (117 loc) · 4.88 KB
/
Copy pathlab_2.cpp
File metadata and controls
121 lines (117 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Лабораторная работа 2. Классы. Перегрузка операторов
// Спроектировать и реализовать класс для описания сущности многочлен (полином), раздела математики - Алгебра.
// Реализовать конструктор(ы), конструктор копирования, деструктор, а также следующие операторы:
// 1. =
// 2. ==, !=
// 3. +, - (унарный и бинарный), +=, -=
// 4. * , / (на число), *=, /=
// 5. <<, >>
// 6. [] (для получения коэффициента i-го члена)
#include <iostream>
#include <map>
#include <string>
#include <vector>
class polynomial {
private:
std::map<unsigned int, double> poly;
public:
polynomial()=default; // конструктор по умолчанию
explicit polynomial(const std::map<unsigned int, double> &p) // создание
: poly(p)
{}
polynomial(const polynomial &p) // конструктор копирования
: poly(p.poly)
{}
polynomial& operator=(const polynomial &p) {
poly = p.poly;
}
~polynomial()=default;
bool operator==(const polynomial &p) { // равны
return p.poly==poly;
}
bool operator!=(const polynomial &p) { // неравны
return !operator==(p);
}
polynomial operator+(const polynomial &p) { // сложение полиномов
polynomial p_rezult = *this;
p_rezult += p;
return p_rezult;
}
polynomial operator-() { // унарный минус
polynomial p;
for (auto i : poly) // проход по объектам
p.poly[i.first] = -poly.at(i.first);
return p;
}
polynomial operator-(const polynomial &p) { // бинарный минус
polynomial p_rezult = *this;
p_rezult -= p;
return p_rezult;
}
polynomial& operator+=(const polynomial &p) {
for (auto i : p.poly) { // проход по объектам
if (poly.count(i.first)) poly[i.first] = poly.at(i.first) + p.poly.at(i.first);
else poly[i.first] = p.poly.at(i.first);
}
return *this;
}
polynomial& operator-=(const polynomial &p) {
for (auto i : p.poly) { // проход по объектам
if (poly.count(i.first)) poly[i.first] = poly.at(i.first) - p.poly.at(i.first);
else poly[i.first] = -p.poly.at(i.first);
}
return *this;
}
polynomial operator*(const polynomial &p) { // множение полиномов
polynomial p_rezult = *this;
p_rezult *= p;
return p_rezult;
}
polynomial& operator*=(const polynomial &p) {
std::vector<int> degree;
for (auto i : p.poly)
for (auto j : poly)
degree.push_back(i.first + j.first);
for (auto i : p.poly)
for (auto j : poly) {
poly[degree.back()] = p.poly.at(i.first) * poly.at(j.first);
degree.pop_back();
}
return *this;
}
polynomial operator/(int a) { // деление полинома на число
polynomial p_rezult = *this;
p_rezult /= a;
return p_rezult;
}
polynomial& operator/=(const int a) {
for (auto i : poly)
poly[i.first] = poly.at(i.first) / a;
return *this;
}
double operator[](const int a) {
if (poly.count(a)) return poly.at(a);
else return 0;
}
friend std::ostream& operator<<(std::ostream &out, const polynomial &p);
friend std::istream& operator>>(std::istream &in, polynomial &p);
};
std::ostream &operator<<(std::ostream &out, const polynomial &p) { // перегрузка оператора
for (auto i : p.poly) { // проход по объектам
if (i.second) {
if (i.second > 0) out << "+" << i.second << "^" << i.first;
else out << i.second << "^" << i.first;
}
}
return out;
}
std::istream &operator>>(std::istream &in, polynomial &p) { // перегрузка оператора
std::cout << "Вводите многочлен х^2+6x-8 в виде 1 2 +6 1 -8 0. В конце поставьте ЕНТЕР";
double x;
int s;
while (in >> x && in >> s) p.poly[s] = x;
return in;
}
int main() {
return 0;
}