Переделать программный код, добавив использование методов - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication46
{
class Program
{
static void Main(string[] args)
{
int i, j, imax = 0, k,l=0; double max, p=0,n=0;
double[,] a = new double[7, 5];
double[] b = new double[8];
Console.WriteLine("Введите элементы массива: ");
for (i = 0; i < 7; i++)
for (j = 0; j < 5; j++)
a[i, j] = double.Parse(Console.ReadLine());
Console.WriteLine("Исходная матрица: ");
for (i = 0; i < 7; i++)
{
for (j = 0; j < 5; j++)
Console.Write("{0:f2} ", a[i, j]);
Console.WriteLine();
}
for (i = 0; i < 7; i++)
{
k = 0;
for (j = 0; j < 5; j++)
if (a[i, j] > 0)
k++;
b[i] = k;
}
for (i = 0; i < 7; i++)
{
max = b[i]; imax = i;
for (j = i + 1; j < 7; j++)
{
if (b[j] > max)
{
max = b[j];
imax = j;
}
}
n = b[i];
b[i] = b[imax];
b[imax] = n;
for (l = 0; l < 5; l++)
{
p = a[i, l];
a[i, l] = a[imax, l];
a[imax, l] = p;
}
}
Console.WriteLine("Массив, в котором строки поменяли местами так, что кол-ва элементов > 0 в строках следовали в порядке убывания:");
for (i = 0; i < 7; i++)
{
for (j = 0; j < 5; j++)
Console.Write("{0:f2} ", a[i, j]);
Console.WriteLine();
}
Console.ReadKey();
}
}
}Решение задачи: «Переделать программный код, добавив использование методов»
textual
Листинг программы
using System;
using System.Text;
namespace ConsoleApplication3
{
static class Extentions
{
public static void PrintArray<T>(this T[,] arr, String tag)
{
var s = new StringBuilder(tag).AppendLine();
for (var i = 0; i < arr.GetLength(0); i++)
{
for (var j = 0; j < arr.GetLength(1); j++)
s.AppendFormat(" {0:F2}", arr[i, j]);
s.AppendLine();
}
Console.WriteLine(s.ToString());
}
public static Double[,] InputArray(this Double[,] arr)
{
Console.WriteLine("Введите элементы массива: ");
for (var i = 0; i < arr.GetLength(0); i++)
for (var j = 0; j < arr.GetLength(1); j++)
{
Console.Write("a[" + (i+1) + "][" + (j+1) + "]=");
arr[i, j] = Double.Parse(Console.ReadLine());
}
return arr;
}
private static void SwapElems<T>(ref T x, ref T y)
{
var temp = x;
x = y;
y = temp;
}
private static void SwapLines(this Double[,] arr, Int32 x, Int32 y)
{
for (var j = 0; j < arr.GetLength(1); j++)
SwapElems(ref arr[x, j], ref arr[y, j]);
}
private static Int32[] GetPosCounts(this Int32[] y, Double[,] x)
{
for (var i = 0; i < x.GetLength(0); i++)
{
y[i] = 0;
for (var j = 0; j < x.GetLength(1); j++)
if (x[i, j] > 0)
y[i]++;
}
return y;
}
public static Double[,] SortByPosCount(this Double[,] arr)
{
var b = new Int32[arr.GetLength(0)].GetPosCounts(arr);
for (var i = 0; i < arr.GetLength(0); i++)
for (var j = i + 1; j < arr.GetLength(0); j++)
if (b[j] > b[i])
{
arr.SwapLines(i, j);
SwapElems(ref b[i], ref b[j]);
}
return arr;
}
}
class Program
{
private static Int32 InputSize(Char c)
{
Console.Write(c + ": ");
return Int32.Parse(Console.ReadLine());
}
static void Main(String[] args)
{
var a = new Double[InputSize('n'), InputSize('m')].InputArray();
a.PrintArray("Исходная матрица:");
a.SortByPosCount().PrintArray("Массив, в котором строки поменяли местами так, что кол-ва элементов > 0 в строках следовали в порядке невозрастания:");
Console.ReadKey();
}
}
}