Структуры данных: структуры (C++): различия между версиями
Перейти к навигации
Перейти к поиску
Строка 2: | Строка 2: | ||
* [http://www.cplusplus.com/doc/tutorial/structures/ Структуры (cplusplus)] | * [http://www.cplusplus.com/doc/tutorial/structures/ Структуры (cplusplus)] | ||
* [https://www.tutorialspoint.com/difference-between-c-structures-and-cplusplus-structures Разница между структурами в C и C++] | * [https://www.tutorialspoint.com/difference-between-c-structures-and-cplusplus-structures Разница между структурами в C и C++] | ||
+ | |||
+ | <syntaxhighlight lang="C++" line> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | |||
+ | struct point { | ||
+ | double x, y; | ||
+ | |||
+ | std::string ToString() { | ||
+ | return "{ x: " + std::to_string(this->x) + " y: " + std::to_string(this->y) + " }"; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | double GetLength(point p1, point p2) { | ||
+ | return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)); | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | point p1 {0,6}, p2 {1,1}; | ||
+ | p1.y = 0; | ||
+ | double length = GetLength(p1,p2); | ||
+ | std::cout << p1.ToString() << std::endl; | ||
+ | std::cout << p2.ToString() << std::endl; | ||
+ | std::cout << "length = " << length << std::endl; | ||
+ | } | ||
+ | </syntaxhighlight> |
Текущая версия на 08:31, 26 июня 2021
Структуры
1 #include <iostream>
2 #include <string>
3
4 struct point {
5 double x, y;
6
7 std::string ToString() {
8 return "{ x: " + std::to_string(this->x) + " y: " + std::to_string(this->y) + " }";
9 }
10 };
11
12 double GetLength(point p1, point p2) {
13 return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
14 }
15
16 int main() {
17 point p1 {0,6}, p2 {1,1};
18 p1.y = 0;
19 double length = GetLength(p1,p2);
20 std::cout << p1.ToString() << std::endl;
21 std::cout << p2.ToString() << std::endl;
22 std::cout << "length = " << length << std::endl;
23 }