Skip to content

Commit e80c3ce

Browse files
committed
splitted into separate files & optimized
1 parent 39b8613 commit e80c3ce

File tree

5 files changed

+111
-103
lines changed

5 files changed

+111
-103
lines changed

Semester_2/Lab_14/list.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "shape.h"
2+
#include "list.h"
3+
4+
using namespace std;
5+
6+
// class Node
7+
8+
Node::Node(Shape* s, Node* next):
9+
shape(s), next(next) {
10+
}
11+
12+
// class FigureList
13+
14+
FigureList::FigureList() {
15+
head = NULL;
16+
size = 0;
17+
}
18+
19+
void FigureList::push_front(Shape* s) {
20+
Node *temp = new Node(s);
21+
if (size) {
22+
temp->next = head;
23+
head = temp;
24+
} else {
25+
head = temp;
26+
}
27+
size++;
28+
}
29+
30+
void FigureList::push_back(Shape* s) {
31+
Node *temp = new Node(s);
32+
if (size) {
33+
Node *last = head;
34+
while (last->next)
35+
last = last->next;
36+
last->next = temp;
37+
} else {
38+
head = temp;
39+
}
40+
size++;
41+
}
42+
43+
Shape& FigureList::get(int id) {
44+
Node *curr = head;
45+
while (curr) {
46+
if (curr->shape->get_id() == id)
47+
return *(curr->shape);
48+
curr = curr->next;
49+
}
50+
return *(curr->shape);
51+
}
52+
53+
void FigureList::erase(int id) {
54+
Node *curr = head, *prev = NULL;
55+
while (curr) {
56+
if (curr->shape->get_id() == id) {
57+
if (curr == head) { // первый элемент
58+
head = head->next;
59+
} else if (curr->next) { // элемент посередине
60+
prev->next = curr->next;
61+
} else { // последний элемент
62+
prev->next = NULL;
63+
}
64+
delete curr->shape;
65+
delete curr;
66+
size--;
67+
return;
68+
}
69+
prev = curr;
70+
curr = curr->next;
71+
}
72+
}
73+
74+
void FigureList::print_all() {
75+
Node* curr = head;
76+
int i = 1;
77+
while (curr) {
78+
cout << "\nitem: " << i++ << endl;
79+
curr->shape->print();
80+
curr = curr->next;
81+
}
82+
}

Semester_2/Lab_14/list.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include "shape.h"
4+
5+
class Node {
6+
public:
7+
Shape* shape;
8+
Node* next;
9+
Node(Shape* s = NULL, Node* next = NULL);
10+
};
11+
12+
class FigureList {
13+
private:
14+
Node* head;
15+
int size;
16+
// int ids[];
17+
18+
public:
19+
FigureList();
20+
void push_front(Shape* s);
21+
void push_back(Shape* s);
22+
Shape &get(int id);
23+
void erase(int id);
24+
void print_all();
25+
};

Semester_2/Lab_14/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//? (круг сравнивать с отрезком -> длина окружности сравнивается с длиной отрезка)
2424

2525
#include "shape.h"
26+
#include "list.h"
2627

2728
int main() {
2829
FigureList list;
@@ -67,7 +68,7 @@ int main() {
6768

6869
cout << endl << "---------- dop ----------" << endl;
6970

70-
cout << "id_42 == id_24: "
71+
cout << endl << "id_42 == id_24: "
7172
<< (list.get(42) == list.get(24) ? "True" : "False")
7273
<< endl;
7374

Semester_2/Lab_14/shape.cpp

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "shape.h"
2-
32
#include "math.h"
4-
#define PI 3.1415926535898
3+
#define M_2PI 6.283185307179586232
54

65
using namespace std;
76

@@ -53,7 +52,7 @@ void Circle::print() {
5352
}
5453

5554
double Circle::get_contour_length() {
56-
return 2 * PI * r;
55+
return M_2PI * r;
5756
}
5857

5958
// class Segment
@@ -71,81 +70,4 @@ void Segment::print() {
7170

7271
double Segment::get_contour_length() {
7372
return sqrt(pow(end.x - start.x, 2) + pow(end.y - start.y, 2));
74-
}
75-
76-
// class Node
77-
78-
Node::Node(Shape* s, Node* next):
79-
shape(s), next(next) {
80-
}
81-
82-
// class FigureList
83-
84-
FigureList::FigureList() {
85-
head = NULL;
86-
size = 0;
87-
}
88-
89-
void FigureList::push_front(Shape* s) {
90-
Node *temp = new Node(s);
91-
if (size) {
92-
temp->next = head;
93-
head = temp;
94-
} else {
95-
head = temp;
96-
}
97-
size++;
98-
}
99-
100-
void FigureList::push_back(Shape* s) {
101-
Node *temp = new Node(s);
102-
if (size) {
103-
Node *last = head;
104-
while (last->next)
105-
last = last->next;
106-
last->next = temp;
107-
} else {
108-
head = temp;
109-
}
110-
size++;
111-
}
112-
113-
Shape& FigureList::get(int id) {
114-
Node *curr = head;
115-
while (curr) {
116-
if (curr->shape->get_id() == id)
117-
return *(curr->shape);
118-
curr = curr->next;
119-
}
120-
return *(curr->shape);
121-
}
122-
123-
void FigureList::erase(int id) {
124-
Node *curr = head, *prev = NULL;
125-
while (curr) {
126-
if (curr->shape->get_id() == id) {
127-
if (curr == head) { // первый элемент
128-
head = head->next;
129-
} else if (curr->next) { // элемент посередине
130-
prev->next = curr->next;
131-
} else { // последний элемент
132-
prev->next = NULL;
133-
}
134-
delete curr->shape;
135-
delete curr;
136-
return;
137-
}
138-
prev = curr;
139-
curr = curr->next;
140-
}
141-
}
142-
143-
void FigureList::print_all() {
144-
Node* curr = head;
145-
int i = 1;
146-
while (curr) {
147-
cout << "\nitem: " << i++ << endl;
148-
curr->shape->print();
149-
curr = curr->next;
150-
}
15173
}

Semester_2/Lab_14/shape.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,4 @@ class Segment : public Shape {
4545
Segment(int id, Point start = {0}, Point end = {0});
4646
virtual void print();
4747
virtual double get_contour_length();
48-
};
49-
50-
class Node {
51-
public:
52-
Shape* shape;
53-
Node* next;
54-
Node(Shape* s = NULL, Node* next = NULL);
55-
};
56-
57-
class FigureList {
58-
private:
59-
Node* head;
60-
int size;
61-
// int ids[];
62-
63-
public:
64-
FigureList();
65-
void push_front(Shape* s);
66-
void push_back(Shape* s);
67-
Shape &get(int id);
68-
void erase(int id);
69-
void print_all();
7048
};

0 commit comments

Comments
 (0)