Advertisement
magneto903

lab1_mod

Dec 10th, 2024
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. namespace V2DataProject
  5. {
  6.     // Делегаты
  7.     public delegate void FValues(double x, ref Complex y1, ref Complex y2);
  8.     public delegate DataItem FDI(double x);
  9.     // Добавляем новый делегат для третьей компоненты
  10.     public delegate void F3Values(double x, ref Complex y3);
  11.     // Структура DataItem
  12.     public struct DataItem
  13.     {
  14.         public double X { get; set; }
  15.         public Complex Y1 { get; set; }
  16.         public Complex Y2 { get; set; }
  17.         public DataItem(double x, Complex y1, Complex y2)
  18.         {
  19.             X = x;
  20.             Y1 = y1;
  21.             Y2 = y2;
  22.         }
  23.         public string ToString(string format)
  24.         {
  25.             return $"X: {X.ToString(format)}, Y1: {Y1.ToString(format)}, Y2: {Y2.ToString(format)}";
  26.         }
  27.         public override string ToString()
  28.         {
  29.             return $"X: {X}, Y1: {Y1}, Y2: {Y2}";
  30.         }
  31.     }
  32.     // Абстрактный базовый класс V2Data
  33.     public abstract class V2Data
  34.     {
  35.         public string Key { get; set; }
  36.         public DateOnly Date { get; set; }
  37.         protected V2Data(string key, DateOnly date)
  38.         {
  39.             Key = key;
  40.             Date = date;
  41.         }
  42.         public abstract int XCount { get; }
  43.         public abstract (double, double) MinMaxMagnitude { get; }
  44.         public abstract string ToLongString(string format);
  45.         public override string ToString()
  46.         {
  47.             return $"Key: {Key}, Date: {Date}";
  48.         }
  49.     }
  50.     // Класс V2DataList
  51.     public class V2DataList : V2Data
  52.     {
  53.         public List<DataItem> DataItems { get; set; }
  54.         public V2DataList(string key, DateOnly date) : base(key, date)
  55.         {
  56.             DataItems = new List<DataItem>();
  57.         }
  58.         public V2DataList(string key, DateOnly date, double[] x, FDI F) : base(key, date)
  59.         {
  60.             DataItems = new List<DataItem>();
  61.             HashSet<double> xSet = new HashSet<double>();
  62.             foreach (var xi in x)
  63.             {
  64.                 if (xSet.Add(xi))
  65.                 {
  66.                     DataItems.Add(F(xi));
  67.                 }
  68.             }
  69.         }
  70.         public override int XCount => DataItems.Count;
  71.         public override (double, double) MinMaxMagnitude
  72.         {
  73.             get
  74.             {
  75.                 double min = double.MaxValue;
  76.                 double max = double.MinValue;
  77.                 foreach (var item in DataItems)
  78.                 {
  79.                     double mag1 = item.Y1.Magnitude;
  80.                     double mag2 = item.Y2.Magnitude;
  81.                     min = Math.Min(min, Math.Min(mag1, mag2));
  82.                     max = Math.Max(max, Math.Max(mag1, mag2));
  83.                 }
  84.                 return (min, max);
  85.             }
  86.         }
  87.         public override string ToString()
  88.         {
  89.             return $"Type: V2DataList, {base.ToString()}, Count: {DataItems.Count}";
  90.         }
  91.         public override string ToLongString(string format)
  92.         {
  93.             string result = ToString() + "\n";
  94.             foreach (var item in DataItems)
  95.             {
  96.                 result += item.ToString(format) + "\n";
  97.             }
  98.             return result;
  99.         }
  100.     }
  101.     // Класс V2DataArray
  102.     public class V2DataArray : V2Data
  103.     {
  104.         public double[] XArray { get; set; }
  105.         public Complex[] Y1Array { get; set; }
  106.         public Complex[] Y2Array { get; set; }
  107.         public V2DataArray(string key, DateOnly date) : base(key, date)
  108.         {
  109.             XArray = new double[0];
  110.             Y1Array = new Complex[0];
  111.             Y2Array = new Complex[0];
  112.         }
  113.         public V2DataArray(string key, DateOnly date, double[] x, FValues F) : base(key, date)
  114.         {
  115.             int n = x.Length;
  116.             XArray = new double[n];
  117.             Y1Array = new Complex[n];
  118.             Y2Array = new Complex[n];
  119.             for (int i = 0; i < n; i++)
  120.             {
  121.                 XArray[i] = x[i];
  122.                 Complex y1 = new Complex();
  123.                 Complex y2 = new Complex();
  124.                 F(x[i], ref y1, ref y2);
  125.                 Y1Array[i] = y1;
  126.                 Y2Array[i] = y2;
  127.             }
  128.         }
  129.         public DataItem? this[int index]
  130.         {
  131.             get
  132.             {
  133.                 if (index >= 0 && index < XArray.Length)
  134.                 {
  135.                     return new DataItem(XArray[index], Y1Array[index], Y2Array[index]);
  136.                 }
  137.                 else
  138.                 {
  139.                     return null;
  140.                 }
  141.             }
  142.         }
  143.         public override int XCount => XArray.Length;
  144.         public override (double, double) MinMaxMagnitude
  145.         {
  146.             get
  147.             {
  148.                 double min = double.MaxValue;
  149.                 double max = double.MinValue;
  150.                 foreach (var y in Y1Array)
  151.                 {
  152.                     double mag = y.Magnitude;
  153.                     min = Math.Min(min, mag);
  154.                     max = Math.Max(max, mag);
  155.                 }
  156.                 foreach (var y in Y2Array)
  157.                 {
  158.                     double mag = y.Magnitude;
  159.                     min = Math.Min(min, mag);
  160.                     max = Math.Max(max, mag);
  161.                 }
  162.                 return (min, max);
  163.             }
  164.         }
  165.         public static explicit operator V2DataList(V2DataArray source)
  166.         {
  167.             V2DataList result = new V2DataList(source.Key, source.Date);
  168.             for (int i = 0; i < source.XArray.Length; i++)
  169.             {
  170.                 DataItem item = new DataItem(source.XArray[i], source.Y1Array[i], source.Y2Array[i]);
  171.                 result.DataItems.Add(item);
  172.             }
  173.             return result;
  174.         }
  175.         public override string ToString()
  176.         {
  177.             return $"Type: V2DataArray, {base.ToString()}";
  178.         }
  179.         public override string ToLongString(string format)
  180.         {
  181.             string result = ToString() + "\n";
  182.             for (int i = 0; i < XArray.Length; i++)
  183.             {
  184.                 result += $"X: {XArray[i].ToString(format)}, Y1: {Y1Array[i].ToString(format)}, Y2: {Y2Array[i].ToString(format)}\n";
  185.             }
  186.             return result;
  187.         }
  188.     }
  189.     // Новый класс V2DDataArray
  190.     public class V2DDataArray : V2DataArray
  191.     {
  192.         public Complex[] Y3Array { get; set; }
  193.         public V2DDataArray(string key, DateOnly date, double[] x, FValues Fy, F3Values Fy3) : base(key, date)
  194.         {
  195.             int n = x.Length;
  196.             XArray = new double[n];
  197.             Y1Array = new Complex[n];
  198.             Y2Array = new Complex[n];
  199.             Y3Array = new Complex[n];
  200.             for (int i = 0; i < n; i++)
  201.             {
  202.                 XArray[i] = x[i];
  203.                 Complex y1 = new Complex();
  204.                 Complex y2 = new Complex();
  205.                 Complex y3 = new Complex();
  206.                 Fy(x[i], ref y1, ref y2);
  207.                 Fy3(x[i], ref y3);
  208.                 Y1Array[i] = y1;
  209.                 Y2Array[i] = y2;
  210.                 Y3Array[i] = y3;
  211.             }
  212.         }
  213.         public override (double, double) MinMaxMagnitude
  214.         {
  215.             get
  216.             {
  217.                 double min = double.MaxValue;
  218.                 double max = double.MinValue;
  219.                 foreach (var y in Y1Array)
  220.                 {
  221.                     double mag = y.Magnitude;
  222.                     min = Math.Min(min, mag);
  223.                     max = Math.Max(max, mag);
  224.                 }
  225.                 foreach (var y in Y2Array)
  226.                 {
  227.                     double mag = y.Magnitude;
  228.                     min = Math.Min(min, mag);
  229.                     max = Math.Max(max, mag);
  230.                 }
  231.                 foreach (var y in Y3Array)
  232.                 {
  233.                     double mag = y.Magnitude;
  234.                     min = Math.Min(min, mag);
  235.                     max = Math.Max(max, mag);
  236.                 }
  237.                 return (min, max);
  238.             }
  239.         }
  240.         public override string ToString()
  241.         {
  242.             // Имя типа + данные базового класса
  243.             return $"Type: V2DDataArray, {base.ToString()}";
  244.         }
  245.         public override string ToLongString(string format)
  246.         {
  247.             string result = ToString() + "\n";
  248.             for (int i = 0; i < XArray.Length; i++)
  249.             {
  250.                 result += $"X: {XArray[i].ToString(format)}, Y1: {Y1Array[i].ToString(format)}, Y2: {Y2Array[i].ToString(format)}, Y3: {Y3Array[i].ToString(format)}\n";
  251.             }
  252.             return result;
  253.         }
  254.     }
  255.     // Класс V2MainCollection
  256.     public class V2MainCollection : List<V2Data>
  257.     {
  258.         public V2Data this[DateOnly date]
  259.         {
  260.             get
  261.             {
  262.                 foreach (var item in this)
  263.                 {
  264.                     if (item.Date == date)
  265.                     {
  266.                         return item;
  267.                     }
  268.                 }
  269.                 return null;
  270.             }
  271.         }
  272.         public bool Add(V2Data data)
  273.         {
  274.             foreach (var item in this)
  275.             {
  276.                 if (item.Key == data.Key && item.Date == data.Date)
  277.                 {
  278.                     return false;
  279.                 }
  280.             }
  281.             base.Add(data);
  282.             return true;
  283.         }
  284.         // Конструктор без параметров, добавляющий по одному элементу каждого типа
  285.         public V2MainCollection()
  286.         {
  287.             double[] xExample = { 0.0, 1.0, 2.0 };
  288.             // Добавляем V2DataArray
  289.             var dataArray = new V2DataArray("ArrayKey", DateOnly.FromDateTime(DateTime.Now), xExample, SampleFValues);
  290.             Add(dataArray);
  291.             // Добавляем V2DDataArray
  292.             var dDataArray = new V2DDataArray("DDataKey", DateOnly.FromDateTime(DateTime.Now.AddDays(1)), xExample, SampleFValues, SampleF3Values);
  293.             Add(dDataArray);
  294.             // Добавляем V2DataList
  295.             var dataList = new V2DataList("ListKey", DateOnly.FromDateTime(DateTime.Now.AddDays(2)), xExample, SampleFDI);
  296.             Add(dataList);
  297.         }
  298.         public string ToLongString(string format)
  299.         {
  300.             string result = "";
  301.             foreach (var item in this)
  302.             {
  303.                 result += item.ToLongString(format) + "\n";
  304.             }
  305.             return result;
  306.         }
  307.         public override string ToString()
  308.         {
  309.             string result = "";
  310.             foreach (var item in this)
  311.             {
  312.                 result += item.ToString() + "\n";
  313.             }
  314.             return result;
  315.         }
  316.         // Пример функций для делегатов
  317.         public static void SampleFValues(double x, ref Complex y1, ref Complex y2)
  318.         {
  319.             y1 = new Complex(Math.Sin(x), Math.Cos(x));
  320.             y2 = new Complex(Math.Cos(x), Math.Sin(x));
  321.         }
  322.         public static DataItem SampleFDI(double x)
  323.         {
  324.             Complex y1 = new Complex(Math.Sin(x), Math.Cos(x));
  325.             Complex y2 = new Complex(Math.Cos(x), Math.Sin(x));
  326.             return new DataItem(x, y1, y2);
  327.         }
  328.         public static void SampleF3Values(double x, ref Complex y3)
  329.         {
  330.             // Пример: y3 = sin(x) + i*cos(x)
  331.             y3 = new Complex(Math.Sin(x), Math.Cos(x));
  332.         }
  333.     }
  334.     // Класс Program с методом Main
  335.     class Program
  336.     {
  337.         static void Main(string[] args)
  338.         {
  339.             // Создаем V2MainCollection используя конструктор без параметров
  340.             V2MainCollection mainCollection = new V2MainCollection();
  341.             Console.WriteLine("V2MainCollection ToLongString (format = F2):");
  342.             Console.WriteLine(mainCollection.ToLongString("F2"));
  343.             // Вывести для двух элементов xCount и MinMaxMagnitude
  344.             int countShown = 0;
  345.             foreach (var item in mainCollection)
  346.             {
  347.                 Console.WriteLine($"{item}: xCount = {item.XCount}, MinMaxMagnitude = {item.MinMaxMagnitude}");
  348.                 countShown++;
  349.                 if (countShown == 2) break;
  350.             }
  351.             // Проверка индексатора по DateOnly
  352.             DateOnly existingDate = mainCollection[0].Date;
  353.             V2Data foundItem = mainCollection[existingDate];
  354.             Console.WriteLine(foundItem != null ? $"Found item with date {existingDate}: {foundItem}" : $"No item found with date {existingDate}");
  355.             DateOnly nonExistingDate = DateOnly.FromDateTime(DateTime.Now.AddYears(1));
  356.             V2Data notFoundItem = mainCollection[nonExistingDate];
  357.             Console.WriteLine(notFoundItem != null ? $"Found item with date {nonExistingDate}: {notFoundItem}" : $"No item found with date {nonExistingDate}");
  358.         }
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement