-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataBase.hpp
119 lines (107 loc) · 2.79 KB
/
DataBase.hpp
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
//
// DataBase.hpp
// [Backend] Операции с многочленами
//
// Created by Андрей Москалёв on 23/04/2019.
// Copyright © 2019 Андрей Москалёв. All rights reserved.
//
#ifndef DataBase_hpp
#define DataBase_hpp
#include <stdio.h>
#include "Polynomial.hpp"
#include "polynomial_funcs.hpp"
#include "exceptions.hpp"
struct ElemDB {
ElemDB * next = nullptr, * prev = nullptr;
Polynomial * polynomial = nullptr;
ElemDB() {}
ElemDB(Polynomial * p) {
polynomial = p;
}
};
struct DataBase {
int count = 0;
ElemDB * elem = nullptr;
void add(Polynomial * p) {
++count;
if (!elem) {
elem = new ElemDB(p);
return;
}
ElemDB * q = elem;
for (int i = 0; i < count - 2; ++i) {
q = q->next;
}
q->next = new ElemDB(p);
q->next->prev = q;
}
void remove(Polynomial p) {
ElemDB * q = elem;
for (int i = 0; i < count; ++i) {
if (equals(*q->polynomial, p)) {
if (q->prev) {
q->prev->next = q->next;
}
if (q->next) {
q->next->prev = q->prev;
}
delete q;
return;
}
q = q->next;
}
throw DataBaseException("This polynomial does not exist.");
}
void remove(int n) {
n -= 1;
if (n < 0) {
throw DataBaseException("number of polynomial can`t be lower zero.");
}
if (n > count) {
throw DataBaseException("This polynomial does not exist.");
}
if (n == 0) {
ElemDB * q = elem;
elem = elem->next;
delete q;
--count;
return;
}
ElemDB * q = elem;
for (int i = 0; i < n && q->next != nullptr; ++i) {
q = q->next;
}
if (q->prev) {
q->prev->next = q->next;
}
if (q->next) {
q->next->prev = q->prev;
}
delete q;
--count;
}
Polynomial get(int n) {
n -= 1;
if (n < 0) {
throw DataBaseException("number of polynomial can`t be lower zero.");
}
if (n > count) {
throw DataBaseException("This polynomial does not exist.");
}
ElemDB * q = elem;
for (int i = 0; i < n && q->next != nullptr; ++i) {
q = q->next;
}
return *q->polynomial;
}
std::vector<Polynomial *> getAll() {
std::vector<Polynomial *> ans;
ElemDB * q = elem;
for (int i = 0; i < count; ++i) {
ans.push_back(q->polynomial);
q = q->next;
}
return ans;
}
};
#endif /* DataBase_hpp */