Структуры данных: структуры (C-Sharp): различия между версиями

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
 
Строка 62: Строка 62:
 
         private static Boolean IsCorrect(MyDate date)
 
         private static Boolean IsCorrect(MyDate date)
 
         {
 
         {
             if ((date.Day < 1) || (date.Month < 1) || (date.Year < 0) ||
+
             if ((date.Day < 0) || (date.Month < 0) || (date.Year < 0) ||
 
                 (date.Day > 31) || (date.Month > 12))
 
                 (date.Day > 31) || (date.Month > 12))
 
             {
 
             {

Текущая версия на 16:04, 16 июня 2021

Структуры

Задачи

Длина отрезка между двумя точками

 1 using System;
 2 
 3 namespace ConsoleApp1
 4 {
 5     public struct Point
 6     {
 7         public double x, y;
 8     }
 9     
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Point p1, p2;
15             p1.x = 0; p1.y = 0;
16             p2.x = 1; p2.y = 1;
17             double res = GetLength(p1, p2);
18             Console.WriteLine($"{res}");
19         }
20 
21         private static double GetLength(Point p1, Point p2)
22         {
23             return Math.Sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
24         }
25     }
26 }

Правильность даты

 1 using System;
 2 
 3 namespace ConsoleApp1
 4 {
 5     public struct MyDate
 6     {
 7         public int Day;
 8         public int Month;
 9         public int Year;
10     }
11     
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             MyDate d;
17             d.Day = 32;
18             d.Month = 2;
19             d.Year = 2000;
20 
21             var res = IsCorrect(d);
22             Console.WriteLine(res);
23         }
24 
25         private static Boolean IsCorrect(MyDate date)
26         {
27             if ((date.Day < 0) || (date.Month < 0) || (date.Year < 0) ||
28                 (date.Day > 31) || (date.Month > 12))
29             {
30                 return false;
31             }
32 
33             return true;
34         }
35     }
36 }