Структуры данных: структуры (C): различия между версиями
Перейти к навигации
Перейти к поиску
| Строка 2: | Строка 2: | ||
* [https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D1%80%D1%83%D0%BA%D1%82%D1%83%D1%80%D0%B0_(%D1%8F%D0%B7%D1%8B%D0%BA_%D0%A1%D0%B8) Структуры языка Си (Википедия)] | * [https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D1%80%D1%83%D0%BA%D1%82%D1%83%D1%80%D0%B0_(%D1%8F%D0%B7%D1%8B%D0%BA_%D0%A1%D0%B8) Структуры языка Си (Википедия)] | ||
* [https://docs.microsoft.com/ru-ru/cpp/c-language/structure-declarations?view=msvc-160 Объявления структур (справка Микрософт)] | * [https://docs.microsoft.com/ru-ru/cpp/c-language/structure-declarations?view=msvc-160 Объявления структур (справка Микрософт)] | ||
| + | |||
| + | == Задачи == | ||
| + | === Длина отрезка между двумя точками === | ||
| + | <syntaxhighlight lang="c" line> | ||
| + | #include <stdio.h> | ||
| + | #include <math.h> | ||
| + | |||
| + | typedef struct point { | ||
| + | double x,y; | ||
| + | } Point; | ||
| + | |||
| + | 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, p2; | ||
| + | p1.x = 0; | ||
| + | p1.y = 0; | ||
| + | p2.x = 1; | ||
| + | p2.y = 1; | ||
| + | double res = GetLength(p1,p2); | ||
| + | printf("length = %.15f", res); | ||
| + | } | ||
| + | |||
| + | </syntaxhighlight> | ||
Версия 15:35, 16 июня 2021
Структуры
Задачи
Длина отрезка между двумя точками
1 #include <stdio.h>
2 #include <math.h>
3
4 typedef struct point {
5 double x,y;
6 } Point;
7
8 double GetLength(Point p1, Point p2) {
9 return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
10 }
11
12 int main() {
13 Point p1, p2;
14 p1.x = 0;
15 p1.y = 0;
16 p2.x = 1;
17 p2.y = 1;
18 double res = GetLength(p1,p2);
19 printf("length = %.15f", res);
20 }