Skip to content

Commit 68fa458

Browse files
committed
Merge branch 'lab-14-dev'
2 parents 2bf94ad + 1fab8a2 commit 68fa458

File tree

6 files changed

+224
-152
lines changed

6 files changed

+224
-152
lines changed

Reports/Lab_14_report.pdf

47.3 KB
Binary file not shown.

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 << endl << "item: " << 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: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,94 @@
1919
//* Segment: координаты начала и коодинаты конца
2020

2121
//? исправить: сделать чтобы айдишники задавались по умолчанию автоматически
22-
//? доп: написать функцию сравнения длин контуров двух фигур
23-
// (круг сравнивать с отрезком -> длина окружности сравнивается с длиной отрезка)
22+
//? доп: написать функцию сравнения длин контуров(!) двух фигур
23+
//? (круг сравнивать с отрезком -> длина окружности сравнивается с длиной отрезка)
2424

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

2728
int main() {
2829
FigureList list;
2930

30-
Circle *c_1 = new Circle(42, {0, 0}, 1, "lol kek");
31-
Circle *c_2 = new Circle(24, {4, 2}, 7, "cheburek");
32-
Segment *s_1 = new Segment(1, {0, 0}, {42, 42});
31+
cout << endl << "---- id_2 id_1 id_0 -> [] ----" << endl;
3332

34-
list.push_front(c_1);
35-
list.push_front(c_2);
36-
list.push_front(s_1);
33+
Circle *c1 = new Circle({0, 0}, 1, "lol kek");
34+
Circle *c2 = new Circle({4, 2}, 7, "cheburek");
35+
Segment *s1 = new Segment({0, 0}, {42, 42});
3736

38-
list.print_all();
37+
list.push_front(c1);
38+
list.push_front(c2);
39+
list.push_front(s1);
3940

40-
cout << endl << "----------" << endl;
41+
list.print_all(); // list now: [id_2, id_1, id_0]
4142

42-
list.push_back(new Segment(2, {6, 18}, {4, 2}));
43-
list.push_front(new Circle(618, {7, 7}, 7, "kek"));
44-
list.print_all();
43+
cout << endl << "---- id_4 -> [] <- id_3 ----" << endl;
4544

46-
cout << endl << "----------" << endl;
45+
list.push_back(new Segment({6, 18}, {4, 2}));
46+
list.push_front(new Circle({7, 7}, 7, "kek"));
4747

48-
list.get(2).print();
49-
cout << "---------\n";
50-
list.get(42).print();
48+
list.print_all(); // list now: [id_4, id_2, id_1, id_0, id_3]
5149

52-
cout << endl << "----------del 42----------" << endl;
53-
list.erase(42);
54-
list.print_all();
50+
cout << endl << "---- [] -> id_3 id_1 ----" << endl;
5551

56-
cout << endl << "----------del 2--------" << endl;
57-
list.erase(2);
58-
list.print_all();
52+
cout << endl; list.get(3).print();
53+
cout << endl; list.get(1).print();
5954

60-
cout << endl << "----------del 618--------" << endl;
61-
list.erase(618);
62-
list.print_all();
55+
cout << endl << "---- [] -x-> id_4 ----" << endl;
6356

64-
cout << endl << "----------" << endl;
65-
list.push_front(new Segment(42));
66-
list.print_all();
57+
list.erase(4);
58+
list.print_all(); // list now: [id_2, id_1, id_0, id_3]
59+
60+
cout << endl << "---- [] -x-> id_0 ----" << endl;
61+
62+
list.erase(0);
63+
list.print_all(); // list now: [id_2, id_1, id_3]
64+
65+
cout << endl << "---- [] -x-> id_3 ----" << endl;
66+
67+
list.erase(3);
68+
list.print_all(); // list now: [id_2, id_1]
69+
70+
cout << endl << "---- id_5 -> [] ----" << endl;
71+
72+
list.push_front(new Segment);
73+
74+
list.print_all(); // list now: [id_5, id_2, id_1]
75+
76+
cout << endl << "-------- dop --------" << endl;
77+
78+
cout << endl << "contour id_1: " << list.get(1).get_contour_length();
79+
cout << endl << "contour id_2: " << list.get(2).get_contour_length();
80+
cout << endl << "contour id_5: " << list.get(5).get_contour_length();
81+
82+
cout << endl << endl << "id_1 == id_2: ";
83+
cout << (list.get(1) == list.get(2) ? "True" : "False");
84+
85+
cout << endl << "id_1 == id_5: ";
86+
cout << (list.get(1) == list.get(5) ? "True" : "False");
87+
88+
cout << endl << "id_2 == id_5: ";
89+
cout << (list.get(2) == list.get(5) ? "True" : "False");
90+
91+
// для теста нужно создать одинаковые Circle и Segment (например равные нулю)
92+
93+
cout << endl << endl << "---- [] <- id_6 ----" << endl;
94+
95+
list.push_back(new Circle); // list now: [id_5, id_2, id_1, id_6]
96+
97+
cout << endl << "contour id_6: " << list.get(6).get_contour_length();
98+
99+
cout << endl << "id_5 == id_6: ";
100+
cout << (list.get(5) == list.get(6) ? "True" : "False") << endl;
101+
102+
cout << endl << "------ list_2 ------" << endl;
103+
104+
FigureList list2;
105+
for (double i = 0; i < 5; i++) {
106+
list2.push_front(new Circle({0, 0}, i));
107+
list2.push_back(new Segment({0, 0}, {i, i}));
108+
}
109+
110+
list2.print_all();
111+
// list_2 now: [id_15, id_13, id_11, id_9, id_7, id_8, id_10, id_12, id_14, id_16]
67112
}

Semester_2/Lab_14/shape.cpp

Lines changed: 30 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "shape.h"
2+
#include "math.h"
3+
#define M_2PI 6.283185307179586232
4+
25
using namespace std;
36

47
// class Point
58

6-
Point::Point(int x, int y):
9+
Point::Point(double x, double y):
710
x(x), y(y) {
811
}
912

@@ -14,23 +17,36 @@ ostream& operator<<(ostream &out, Point &p) {
1417

1518
// class Shape
1619

17-
Shape::Shape(int id, Point pos):
18-
id(id), pos(pos) {
20+
Shape::Shape(Point pos):
21+
id(next_id()), pos(pos) {
22+
}
23+
24+
unsigned long Shape::next_id() {
25+
static unsigned long next_id = 0;
26+
return next_id++;
1927
}
2028

2129
void Shape::print() {
2230
cout << "id: " << id << endl;
2331
cout << "pos: " << pos << endl;
2432
}
2533

26-
int Shape::get_id() {
34+
unsigned long Shape::get_id() {
2735
return id;
2836
}
2937

38+
double Shape::get_contour_length() {
39+
return 0;
40+
}
41+
42+
bool Shape::operator==(Shape &s) {
43+
return get_contour_length() == s.get_contour_length();
44+
}
45+
3046
// class Circle
3147

32-
Circle::Circle(int id, Point pos, int r, string text):
33-
Shape(id, pos), r(r), text(text) {
48+
Circle::Circle(Point pos, double r, string text):
49+
Shape(pos), r(r), text(text) {
3450
}
3551

3652
void Circle::print() {
@@ -40,10 +56,14 @@ void Circle::print() {
4056
cout << "text: " << text << endl;
4157
}
4258

59+
double Circle::get_contour_length() {
60+
return M_2PI * r;
61+
}
62+
4363
// class Segment
4464

45-
Segment::Segment(int id, Point start, Point end):
46-
Shape(id, start), start(start), end(end) {
65+
Segment::Segment(Point start, Point end):
66+
Shape(start), start(start), end(end) {
4767
}
4868

4969
void Segment::print() {
@@ -53,79 +73,6 @@ void Segment::print() {
5373
cout << "end pos: " << end << endl;
5474
}
5575

56-
// class Node
57-
58-
Node::Node(Shape* s, Node* next):
59-
shape(s), next(next) {
60-
}
61-
62-
// class FigureList
63-
64-
FigureList::FigureList() {
65-
head = NULL;
66-
size = 0;
67-
}
68-
69-
void FigureList::push_front(Shape* s) {
70-
Node *temp = new Node(s);
71-
if (size) {
72-
temp->next = head;
73-
head = temp;
74-
} else {
75-
head = temp;
76-
}
77-
size++;
78-
}
79-
80-
void FigureList::push_back(Shape* s) {
81-
Node *temp = new Node(s);
82-
if (size) {
83-
Node *last = head;
84-
while (last->next)
85-
last = last->next;
86-
last->next = temp;
87-
} else {
88-
head = temp;
89-
}
90-
size++;
91-
}
92-
93-
Shape& FigureList::get(int id) {
94-
Node *curr = head;
95-
while (curr) {
96-
if (curr->shape->get_id() == id)
97-
return *(curr->shape);
98-
curr = curr->next;
99-
}
100-
return *(curr->shape);
101-
}
102-
103-
void FigureList::erase(int id) {
104-
Node *curr = head, *prev = NULL;
105-
while (curr) {
106-
if (curr->shape->get_id() == id) {
107-
if (curr == head) { // первый элемент
108-
head = head->next;
109-
} else if (curr->next) { // элемент посередине
110-
prev->next = curr->next;
111-
} else { // последний элемент
112-
prev->next = NULL;
113-
}
114-
delete curr->shape;
115-
delete curr;
116-
return;
117-
}
118-
prev = curr;
119-
curr = curr->next;
120-
}
121-
}
122-
123-
void FigureList::print_all() {
124-
Node* curr = head;
125-
int i = 1;
126-
while (curr) {
127-
cout << "\nitem: " << i++ << endl;
128-
curr->shape->print();
129-
curr = curr->next;
130-
}
76+
double Segment::get_contour_length() {
77+
return sqrt(pow(end.x - start.x, 2) + pow(end.y - start.y, 2));
13178
}

0 commit comments

Comments
 (0)