Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- namespace V2DataProject
- {
- // Делегаты
- public delegate void FValues(double x, ref Complex y1, ref Complex y2);
- public delegate DataItem FDI(double x);
- // Структура DataItem
- public struct DataItem
- {
- public double X { get; set; }
- public Complex Y1 { get; set; }
- public Complex Y2 { get; set; }
- public DataItem(double x, Complex y1, Complex y2)
- {
- X = x;
- Y1 = y1;
- Y2 = y2;
- }
- public string ToString(string format)
- {
- return $"X: {X.ToString(format)}, Y1: {Y1.ToString(format)}, Y2: {Y2.ToString(format)}";
- }
- public override string ToString()
- {
- return $"X: {X}, Y1: {Y1}, Y2: {Y2}";
- }
- }
- // Абстрактный базовый класс V2Data
- public abstract class V2Data
- {
- public string Key { get; set; }
- public DateOnly Date { get; set; }
- protected V2Data(string key, DateOnly date)
- {
- Key = key;
- Date = date;
- }
- public abstract int XCount { get; }
- public abstract (double, double) MinMaxMagnitude { get; }
- public abstract string ToLongString(string format);
- public override string ToString()
- {
- return $"Key: {Key}, Date: {Date}";
- }
- }
- // Класс V2DataList
- public class V2DataList : V2Data
- {
- public List<DataItem> DataItems { get; set; }
- public V2DataList(string key, DateOnly date) : base(key, date)
- {
- DataItems = new List<DataItem>();
- }
- public V2DataList(string key, DateOnly date, double[] x, FDI F) : base(key, date)
- {
- DataItems = new List<DataItem>();
- HashSet<double> xSet = new HashSet<double>();
- foreach (var xi in x)
- {
- if (xSet.Add(xi))
- {
- DataItems.Add(F(xi));
- }
- }
- }
- public override int XCount => DataItems.Count;
- public override (double, double) MinMaxMagnitude
- {
- get
- {
- double min = double.MaxValue;
- double max = double.MinValue;
- foreach (var item in DataItems)
- {
- double mag1 = item.Y1.Magnitude;
- double mag2 = item.Y2.Magnitude;
- min = Math.Min(min, Math.Min(mag1, mag2));
- max = Math.Max(max, Math.Max(mag1, mag2));
- }
- return (min, max);
- }
- }
- public override string ToString()
- {
- return $"Type: V2DataList, {base.ToString()}, Count: {DataItems.Count}";
- }
- public override string ToLongString(string format)
- {
- string result = ToString() + "\n";
- foreach (var item in DataItems)
- {
- result += item.ToString(format) + "\n";
- }
- return result;
- }
- }
- // Класс V2DataArray
- public class V2DataArray : V2Data
- {
- public double[] XArray { get; set; }
- public Complex[] Y1Array { get; set; }
- public Complex[] Y2Array { get; set; }
- public V2DataArray(string key, DateOnly date) : base(key, date)
- {
- XArray = new double[0];
- Y1Array = new Complex[0];
- Y2Array = new Complex[0];
- }
- public V2DataArray(string key, DateOnly date, double[] x, FValues F) : base(key, date)
- {
- int n = x.Length;
- XArray = new double[n];
- Y1Array = new Complex[n];
- Y2Array = new Complex[n];
- for (int i = 0; i < n; i++)
- {
- XArray[i] = x[i];
- Complex y1 = new Complex();
- Complex y2 = new Complex();
- F(x[i], ref y1, ref y2);
- Y1Array[i] = y1;
- Y2Array[i] = y2;
- }
- }
- public DataItem? this[int index]
- {
- get
- {
- if (index >= 0 && index < XArray.Length)
- {
- return new DataItem(XArray[index], Y1Array[index], Y2Array[index]);
- }
- else
- {
- return null;
- }
- }
- }
- public override int XCount => XArray.Length;
- public override (double, double) MinMaxMagnitude
- {
- get
- {
- double min = double.MaxValue;
- double max = double.MinValue;
- foreach (var y in Y1Array)
- {
- double mag = y.Magnitude;
- min = Math.Min(min, mag);
- max = Math.Max(max, mag);
- }
- foreach (var y in Y2Array)
- {
- double mag = y.Magnitude;
- min = Math.Min(min, mag);
- max = Math.Max(max, mag);
- }
- return (min, max);
- }
- }
- public static explicit operator V2DataList(V2DataArray source)
- {
- V2DataList result = new V2DataList(source.Key, source.Date);
- for (int i = 0; i < source.XArray.Length; i++)
- {
- DataItem item = new DataItem(source.XArray[i], source.Y1Array[i], source.Y2Array[i]);
- result.DataItems.Add(item);
- }
- return result;
- }
- public override string ToString()
- {
- return $"Type: V2DataArray, {base.ToString()}";
- }
- public override string ToLongString(string format)
- {
- string result = ToString() + "\n";
- for (int i = 0; i < XArray.Length; i++)
- {
- result += $"X: {XArray[i].ToString(format)}, Y1: {Y1Array[i].ToString(format)}, Y2: {Y2Array[i].ToString(format)}\n";
- }
- return result;
- }
- }
- // Класс V2MainCollection
- public class V2MainCollection : List<V2Data>
- {
- public V2Data this[DateOnly date]
- {
- get
- {
- foreach (var item in this)
- {
- if (item.Date == date)
- {
- return item;
- }
- }
- return null;
- }
- }
- public bool Add(V2Data data)
- {
- foreach (var item in this)
- {
- if (item.Key == data.Key && item.Date == data.Date)
- {
- return false;
- }
- }
- base.Add(data);
- return true;
- }
- public V2MainCollection(int nA, int nL)
- {
- for (int i = 0; i < nA; i++)
- {
- double[] x = { i, i + 1, i + 2 };
- V2DataArray dataArray = new V2DataArray($"Array_{i}", DateOnly.FromDateTime(DateTime.Now.AddDays(i)), x, SampleFValues);
- Add(dataArray);
- }
- for (int i = 0; i < nL; i++)
- {
- double[] x = { i, i + 1, i + 2 };
- V2DataList dataList = new V2DataList($"List_{i}", DateOnly.FromDateTime(DateTime.Now.AddDays(i)), x, SampleFDI);
- Add(dataList);
- }
- }
- public string ToLongString(string format)
- {
- string result = "";
- foreach (var item in this)
- {
- result += item.ToLongString(format) + "\n";
- }
- return result;
- }
- public override string ToString()
- {
- string result = "";
- foreach (var item in this)
- {
- result += item.ToString() + "\n";
- }
- return result;
- }
- // Пример функций для делегатов
- public static void SampleFValues(double x, ref Complex y1, ref Complex y2)
- {
- y1 = new Complex(Math.Sin(x), Math.Cos(x));
- y2 = new Complex(Math.Cos(x), Math.Sin(x));
- }
- public static DataItem SampleFDI(double x)
- {
- Complex y1 = new Complex(Math.Sin(x), Math.Cos(x));
- Complex y2 = new Complex(Math.Cos(x), Math.Sin(x));
- return new DataItem(x, y1, y2);
- }
- }
- // Класс Program с методом Main
- class Program
- {
- static void Main(string[] args)
- {
- // 1. Создать объект V2DataArray и вывести его данные
- double[] xArray = { 0.0, 1.0, 2.0 };
- V2DataArray dataArray = new V2DataArray("ArrayKey", DateOnly.FromDateTime(DateTime.Now), xArray, V2MainCollection.SampleFValues);
- Console.WriteLine("V2DataArray ToLongString:");
- Console.WriteLine(dataArray.ToLongString("F2"));
- // Преобразовать в V2DataList
- V2DataList dataList = (V2DataList)dataArray;
- Console.WriteLine("Converted V2DataList ToLongString:");
- Console.WriteLine(dataList.ToLongString("F2"));
- // 2. Использование индексатора
- Console.WriteLine("Indexer test:");
- Console.WriteLine(dataArray[1]?.ToString("F2") ?? "Index out of range");
- Console.WriteLine(dataArray[10]?.ToString("F2") ?? "Index out of range");
- // 3. Создать V2MainCollection и вывести данные
- V2MainCollection mainCollection = new V2MainCollection(2, 2);
- Console.WriteLine("V2MainCollection ToLongString:");
- Console.WriteLine(mainCollection.ToLongString("F2"));
- // 4. Вывести xCount и MinMaxMagnitude для каждого элемента
- foreach (var item in mainCollection)
- {
- Console.WriteLine($"{item}: xCount = {item.XCount}, MinMaxMagnitude = {item.MinMaxMagnitude}");
- }
- // 5. Использование индексатора по DateOnly
- DateOnly existingDate = mainCollection[0].Date;
- V2Data foundItem = mainCollection[existingDate];
- Console.WriteLine(foundItem != null ? $"Found item with date {existingDate}: {foundItem}" : $"No item found with date {existingDate}");
- DateOnly nonExistingDate = DateOnly.FromDateTime(DateTime.Now.AddYears(1));
- V2Data notFoundItem = mainCollection[nonExistingDate];
- Console.WriteLine(notFoundItem != null ? $"Found item with date {nonExistingDate}: {notFoundItem}" : $"No item found with date {nonExistingDate}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement