Наследование и перестановка столбцов матрицы - C#
Формулировка задачи:
Даны 2 класса, связанные наследованием. Наличие конструктора - обязательно. Переставить в каждом столбце прямоугольной матрицы все отрицательные элементы в конец столба. Вывести часть полученной матрицы, состоящую из n первых строк, не имеющих отрицательных элементов.
Решение задачи: «Наследование и перестановка столбцов матрицы»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1234
{
class Program
{
class Parent
{
public int n;
public int m;
public int [,] mas;
public Parent(int nn, int mm)
{
n = nn;
m = mm;
mas=new int[n,m];
}
public void vvod(int n, int m)
{
/* Console.WriteLine("Введите кол-во строк n");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите кол-во столбцов m");
int m = Convert.ToInt32(Console.ReadLine());*/
int[,] mas = new int[n, m];
Console.WriteLine("Введите элементы массива");
for (int i = 0; i < mas.GetLength(0); i++)
{
Console.WriteLine("Введите элементы " + i + "-й строки:");
for (int j = 0; j < mas.GetLength(1); j++)
{
Console.WriteLine();
mas[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
}
public void vyvod(int n, int m)
{
//int[,] mas = new int[n, m];
for (int i = 0; i < mas.GetLength(0); ++i)
{
for (int j = 0; j < mas.GetLength(1); ++j)
{
Console.Write("{0,3} ", mas[i, j]);
}
Console.WriteLine();
}
}
}
class Child : Parent
{
public Child(int n, int m) : base(n, m) { }
static void Swap<T>(ref T aa, ref T bb)
{
T temp;
temp = aa;
aa = bb;
bb = temp;
}
public void smena(int n, int m)
{
// int[,] mas = new int[n, m];
for (int j = 0; j < mas.GetLength(1); ++j)
{
int konec_stroki = mas.GetLength(0) - 1;
for (int i = 0; i <= konec_stroki; )
{
if (mas[i, j] < 0)
{
Swap(ref mas[i, j], ref mas[konec_stroki, j]);
konec_stroki--;
}
else
{
i++;
}
}
}
Console.WriteLine();
}
public void otric(int n, int m)
{
//int[,] mas = new int[n, m];
int neotric_stroki = 0;
bool otric_esli = false;
for (int i = 0; i < mas.GetLength(0); ++i)
{
for (int j = 0; j < mas.GetLength(1); ++j)
{
if (mas[i, j] < 0)
{
otric_esli = true;
break;
}
}
if (otric_esli)
break;
neotric_stroki++;
}
Console.WriteLine();
Console.WriteLine("Часть строк матрицы с неотрицательными элементами: ");
for (int i = 0; i < neotric_stroki; ++i)
{
for (int j = 0; j < mas.GetLength(1); ++j)
{
Console.Write("{0,3} ", mas[i, j]);
}
Console.WriteLine();
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
Child c=new Child(4,3);
c.vvod(4, 3);
c.vyvod(4, 3);
c.smena(4, 3);
c.otric(4, 3);
}
}
}