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

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
 
(не показаны 2 промежуточные версии этого же участника)
Строка 6: Строка 6:
 
=== Длина отрезка между двумя точками ===
 
=== Длина отрезка между двумя точками ===
 
<syntaxhighlight lang="c#" line>
 
<syntaxhighlight lang="c#" line>
 +
using System;
  
 +
namespace ConsoleApp1
 +
{
 +
    public struct Point
 +
    {
 +
        public double x, y;
 +
    }
 +
   
 +
    class Program
 +
    {
 +
        static void Main(string[] args)
 +
        {
 +
            Point p1, p2;
 +
            p1.x = 0; p1.y = 0;
 +
            p2.x = 1; p2.y = 1;
 +
            double res = GetLength(p1, p2);
 +
            Console.WriteLine($"{res}");
 +
        }
 +
 +
        private static double GetLength(Point p1, Point p2)
 +
        {
 +
            return Math.Sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
 +
        }
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Правильность даты ===
 
=== Правильность даты ===
 
<syntaxhighlight lang="c#" line>
 
<syntaxhighlight lang="c#" line>
 +
using System;
 +
 +
namespace ConsoleApp1
 +
{
 +
    public struct MyDate
 +
    {
 +
        public int Day;
 +
        public int Month;
 +
        public int Year;
 +
    }
 +
   
 +
    class Program
 +
    {
 +
        static void Main(string[] args)
 +
        {
 +
            MyDate d;
 +
            d.Day = 32;
 +
            d.Month = 2;
 +
            d.Year = 2000;
 +
 +
            var res = IsCorrect(d);
 +
            Console.WriteLine(res);
 +
        }
 +
 +
        private static Boolean IsCorrect(MyDate date)
 +
        {
 +
            if ((date.Day < 0) || (date.Month < 0) || (date.Year < 0) ||
 +
                (date.Day > 31) || (date.Month > 12))
 +
            {
 +
                return false;
 +
            }
  
 +
            return true;
 +
        }
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Текущая версия на 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 }