Skip to content

Commit 04cac03

Browse files
committed
Merge branch 'lab-14-dev'
2 parents e7b319a + d21fa9e commit 04cac03

File tree

4 files changed

+247
-45
lines changed

4 files changed

+247
-45
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Labs now ready: `7 / 7`
2424

2525
## Semester 2
2626

27-
Labs now ready: `7 / 8`
27+
Labs now ready: `8 / 8`
2828
| Lab | Dev | Ready | Pass | Dop |
2929
| :---- | :-: | :-: | :-: | :-: |
3030
| Lab 7 |||||
@@ -34,7 +34,7 @@ Labs now ready: `7 / 8`
3434
| Lab 11 |||||
3535
| Lab 12 |||||
3636
| Lab 13 |||||
37-
| Lab 14 | | | | |
37+
| Lab 14 | | | | |
3838

3939
### Total time spent on labs
4040

Semester_2/Lab_14/main.cpp

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,56 @@
1212
// Связаный список должен быть реализован с помощью двух классов
1313
// Node (элемент списка) и List (сам список)
1414

15-
#include <cstdlib>
16-
#include <string>
15+
// TODO:
16+
//* связный список (List) элементов (Node)
17+
//* вывод параметров фигуры через метод print()
18+
//* Circle: радиус, надпись произвольной длины
19+
//* Segment: координаты начала и коодинаты конца
1720

18-
using namespace std;
21+
//? исправить: сделать чтобы айдишники задавались по умолчанию автоматически
22+
//? доп: написать функцию сравнения длин контуров двух фигур
23+
// (круг сравнивать с отрезком -> длина окружности сравнивается с длиной отрезка)
1924

20-
class Shape {
21-
protected:
22-
int id;
23-
int x, y;
25+
#include "shape.h"
2426

25-
public:
26-
Shape(int id, int x, int y);
27-
void print();
28-
};
27+
int main() {
28+
FigureList list;
2929

30-
class Circle : public Shape {
31-
protected:
32-
int r;
33-
string text;
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});
3433

35-
public:
36-
Circle(int id, int x, int y, int r, string text);
37-
void print();
38-
};
34+
list.push_front(c_1);
35+
list.push_front(c_2);
36+
list.push_front(s_1);
3937

40-
class Segment : public Shape {
41-
protected:
42-
int x_start, y_start, x_end, y_end;
43-
44-
public:
45-
Segment(int id, int x_start, int y_start, int x_end, int y_end);
46-
void print();
47-
};
38+
list.print_all();
4839

49-
class FigureList {
50-
private:
51-
Shape* arr[100];
52-
int size = 0;
40+
cout << endl << "----------" << endl;
5341

54-
public:
55-
void add(Shape* s);
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();
5645

57-
Shape &get(int id);
46+
cout << endl << "----------" << endl;
5847

59-
// найти фигуру всписке по идентификатору
60-
Shape& findFigure(int id);
48+
list.get(2).print();
49+
cout << "---------\n";
50+
list.get(42).print();
6151

62-
// удалить фигуру из списка
63-
void erase(int id);
52+
cout << endl << "----------del 42----------" << endl;
53+
list.erase(42);
54+
list.print_all();
6455

65-
// вывести на экран в текстовом режиме информацию о всех фигурах в списке
66-
void printAll();
67-
};
56+
cout << endl << "----------del 2--------" << endl;
57+
list.erase(2);
58+
list.print_all();
6859

60+
cout << endl << "----------del 618--------" << endl;
61+
list.erase(618);
62+
list.print_all();
6963

70-
int main() {
71-
FigureList list;
64+
cout << endl << "----------" << endl;
65+
list.push_front(new Segment(42));
66+
list.print_all();
7267
}

Semester_2/Lab_14/shape.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "shape.h"
2+
using namespace std;
3+
4+
// class Point
5+
6+
Point::Point(int x, int y):
7+
x(x), y(y) {
8+
}
9+
10+
ostream& operator<<(ostream &out, Point &p) {
11+
out << "(" << p.x << ", " << p.y << ")";
12+
return out;
13+
}
14+
15+
// class Shape
16+
17+
Shape::Shape(int id, Point pos):
18+
id(id), pos(pos) {
19+
}
20+
21+
void Shape::print() {
22+
cout << "id: " << id << endl;
23+
cout << "pos: " << pos << endl;
24+
}
25+
26+
int Shape::get_id() {
27+
return id;
28+
}
29+
30+
// class Circle
31+
32+
Circle::Circle(int id, Point pos, int r, string text):
33+
Shape(id, pos), r(r), text(text) {
34+
}
35+
36+
void Circle::print() {
37+
cout << "type: Circle" << endl;
38+
Shape::print();
39+
cout << "radius: " << r << endl;
40+
cout << "text: " << text << endl;
41+
}
42+
43+
// class Segment
44+
45+
Segment::Segment(int id, Point start, Point end):
46+
Shape(id, start), start(start), end(end) {
47+
}
48+
49+
void Segment::print() {
50+
cout << "type: Segment" << endl;
51+
Shape::print();
52+
cout << "start pos: " << start << endl;
53+
cout << "end pos: " << end << endl;
54+
}
55+
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+
}
131+
}

Semester_2/Lab_14/shape.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <cstdlib>
5+
#include <string>
6+
7+
using namespace std;
8+
9+
class Point {
10+
public:
11+
int x, y;
12+
Point(int x = 0, int y = 0);
13+
friend ostream& operator<<(ostream &out, Point &p);
14+
};
15+
16+
class Shape {
17+
protected:
18+
int id;
19+
Point pos;
20+
21+
public:
22+
Shape(int id, Point pos = {0});
23+
virtual void print();
24+
int get_id();
25+
};
26+
27+
class Circle : public Shape {
28+
protected:
29+
int r;
30+
string text;
31+
32+
public:
33+
Circle(int id, Point pos = {0}, int r = 1, string text = "");
34+
virtual void print();
35+
};
36+
37+
class Segment : public Shape {
38+
protected:
39+
Point start, end;
40+
41+
public:
42+
Segment(int id, Point start = {0}, Point end = {0});
43+
virtual void print();
44+
};
45+
46+
class Node {
47+
public:
48+
Shape* shape;
49+
Node* next;
50+
Node(Shape* s = NULL, Node* next = NULL);
51+
};
52+
53+
class FigureList {
54+
private:
55+
Node* head;
56+
int size;
57+
// int ids[];
58+
59+
public:
60+
FigureList();
61+
62+
// добавить фигуру в начало списка
63+
void push_front(Shape* s);
64+
65+
// добавить фигуру в конец списка
66+
void push_back(Shape* s);
67+
68+
// найти фигуру всписке по идентификатору
69+
Shape &get(int id);
70+
71+
// удалить фигуру из списка
72+
void erase(int id);
73+
74+
// вывести на экран в текстовом режиме информацию о всех фигурах в списке
75+
void print_all();
76+
};

0 commit comments

Comments
 (0)