Описать класс для хранения массива чисел. Перегрузить в классе оператор * - C#
Формулировка задачи:
Привет.
Описать класс для хранения массива чисел. Перегрузить в классе оператор *, который должен находить произведение четных элементов двух массивов.
Решение задачи: «Описать класс для хранения массива чисел. Перегрузить в классе оператор *»
textual
Листинг программы
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- MyArray array = new MyArray(new int[] { 1, 2, 3, 4, 5 });
- MyArray array2 = new MyArray(new int[] { 6, 7, 8, 9, 10, 11, 12 });
- Console.WriteLine(array * array2);
- Console.ReadKey();
- }
- }
- class MyArray
- {
- int[] array;
- public int Length {get; private set;}
- public MyArray() { }
- public MyArray(int size)
- {
- Length = size;
- array = new int[size];
- array.Initialize();
- }
- public MyArray(params int[] elements) : this(elements.Length)
- {
- Array.Copy(elements, array, elements.Length);
- }
- public int this[int index]
- {
- get
- {
- return array[index];
- }
- set
- {
- array[index] = value;
- }
- }
- public static int? operator *(MyArray a1, MyArray a2)
- {
- int? mul = null;
- for (int i = 0; i < a1.Length; i++)
- if (a1[i] % 2 == 0)
- mul = mul == null ? a1[i] : mul * a1[i];
- for (int i = 0; i < a2.Length; i++)
- if (a2[i] % 2 == 0)
- mul = mul == null ? a2[i] : mul * a2[i];
- return mul;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д