Нужно добавить свойства - C#
Формулировка задачи:
Свойства:
-Возвращающие общее количество элементов в массиве(доступное только для чтения)
-Позволяющее увеличить значение всех элементов на скаляр(доступное только для записи)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Класс_7_new
{
class DoubleMatrix
{
public double[,] DoubleArray;
public int n, m;
public DoubleMatrix(int n, int m) //конструктор для изначально созданных значений
{
this.n = n;
this.m = m;
DoubleArray = new double[n, m];
}
public void Input()//ввод массива поэлементно с клавиатуры
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) //заполнения линии массива
{
Console.Write("Введите значение элемента: ");
DoubleArray[i, j] = double.Parse(Console.ReadLine());
}
Console.WriteLine("--Следующая строка массива--");
}
}
public void Output()//метод вывода массива на экран
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(DoubleArray[i, j] + " ");
}
Console.WriteLine();
}
}
public void Sort() //Метод первой сортировки,прямой выбор(Линейная)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m - 1; j++)
{
int jmax = j;
for (int jj = j + 1; jj < m; jj++)
{
if (DoubleArray[i, jj] > DoubleArray[i, jmax])
{
jmax = jj;
}
}
double tmp = DoubleArray[i, jmax];
DoubleArray[i, jmax] = DoubleArray[i, j];
DoubleArray[i, j] = tmp;
}
}
}
public int Count //(только для чтения)
{
get //блок, через который возвращает значение с помо. ключ. слова return
{
return n * m;
}
}
}
}Основная программа:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Класс_7_new
{
class Program
{
static void Main(string[] args)
{
DoubleMatrix matrix = null;
do
{
Console.Clear();
Console.WriteLine("Выберите действие: ");
Console.WriteLine("_______________________________");
Console.WriteLine("1.Ввод элементов массива с клавиатуры");
Console.WriteLine("2.Вывод массива на экран");
Console.WriteLine("3.Отсортировать каждый подмассив в порядке убывания");
Console.WriteLine("4.Показать кол-во элементов всего");
Console.WriteLine("0.ВЫХОД");
ConsoleKeyInfo key = Console.ReadKey(true);
Console.Clear();
//-----------------------Работа с классом---------------------------
switch (key.KeyChar)
{
case '1':
Console.Write("Введите количество строк: ");
int n = int.Parse(Console.ReadLine());
Console.Write("Введите количество столбцов: ");
int m = int.Parse(Console.ReadLine());
matrix = new DoubleMatrix(n, m);
matrix.Input();
break;
case '2':
if (matrix != null)
{
matrix.Output();
}
else
{
Console.WriteLine("Вы не ввели матрицу!");
}
break;
case '3':
if (matrix != null)
{
matrix.Sort();
matrix.Output();
}
else
{
Console.WriteLine("Вы не ввели матрицу!");
}
break;
case '4':
if (matrix != null)
{
Console.WriteLine(matrix.Count);
}
else
{
Console.WriteLine("Вы не ввели матрицу!");
}
break;
case '0': return;
}
Console.WriteLine("Для продолжения нажмите любую клавишу...");
Console.ReadKey(true);
}
while (true);
}
}
}Решение задачи: «Нужно добавить свойства»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Класс_7_new
{
class DoubleMatrix
{
public double[,] DoubleArray;
public int n, m;
public DoubleMatrix(int n, int m) //конструктор для изначально созданных значений
{
this.n = n;
this.m = m;
DoubleArray = new double[n, m];
}
public void Input()//ввод массива поэлементно с клавиатуры
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) //заполнения линии массива
{
Console.Write("Введите значение элемента: ");
DoubleArray[i, j] = double.Parse(Console.ReadLine());
}
Console.WriteLine("--Следующая строка массива--");
}
}
public void Output()//метод вывода массива на экран
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(DoubleArray[i, j] + " ");
}
Console.WriteLine();
}
}
public void Sort() //Метод первой сортировки,прямой выбор(Линейная)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m - 1; j++)
{
int jmax = j;
for (int jj = j + 1; jj < m; jj++)
{
if (DoubleArray[i, jj] > DoubleArray[i, jmax])
{
jmax = jj;
}
}
double tmp = DoubleArray[i, jmax];
DoubleArray[i, jmax] = DoubleArray[i, j];
DoubleArray[i, j] = tmp;
}
}
}
// Это как раз 1е затребованное свойство
public int Count //(только для записи)
{
get //блок, через который возвращает значение с помо. ключ. слова return
{
return n * m;
}
}
//Второе свойство
public int Up //(только для чтения)
{
set
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m - 1; j++)
{
DoubleArray[i,j]+=value;
}
}
}
}
}
}