Имеется задача:
1) Создать базовый класс с одним целым полем. Разработать два конструктора: первый – без параметров – заполняет поле случайным положительным числом; второй – с параметром – конструктор копирования. Разработать метод: вывод поля.
2) Класс «Натуральное число». Наследует базовый класс. Поле определяет натуральное число. Методы: а) количество цифр; б) сумма цифр; в) является ли палиндромом (true, false); г) «перевертыш» числа.
3) Класс «Массив натуральных чисел». Наследует класс «Натуральное число». Переопределить метод вывода. Отсортировать по количеству цифр и сумме цифр (т.е. в каждой группе с одинаковым количеством цифр сортируем по сумме цифр).
И код:
Код:
class BasedClass//базовый класс
{
//с одним целым полем
protected int a;
protected Random rand;
public BasedClass()//конструктор, заполняет случайно
{
rand = new Random();
a = rand.Next(0, 10001);
}
public BasedClass(int a)//конструктор копирования
{
this.a = a;
}
public virtual void Print()//вывод
{
Console.WriteLine("Задание первое:\nКонструктор без параметров a= {0} \nКонструктор с параметром копирования а= {1}", a, this.a);
}
}
class Natural : BasedClass//наследование
{
public Natural() : base() { }
public Natural(int a) : base(a) { }//конструкторы
public int Value
{
get
{
return a;
}
set
{
a = value;
}
}
public int Kolichestvo()
{
int tmp = a;
int k = 0;
if (tmp == 0) k = 1;
while (tmp != 0)
{
tmp = tmp / 10;
k++;
}
Console.WriteLine("Задание второе:\nКоличество цифр: {0}", k, a);
return k;
}
public int Kolichestvo1()
{
int tmp = a;
int k = 0;
if (tmp == 0) k = 1;
while (tmp != 0)
{
tmp = tmp / 10;
k++;
}
return k;
}
public int Summa()
{
int rep = a;
int s = 0;
while (rep != 0)
{
s += rep % 10;
rep /= 10;
}
Console.WriteLine("Сумма цифр: {0}", s);
return s;
}
public int Summa1()
{
int rep = a;
int s = 0;
while (rep != 0)
{
s += rep % 10;
rep /= 10;
}
return s;
}
public bool palind()
{
int n, digit, k = 0, rep = a;
bool A;
n = a;
do
{
digit = rep % 10;
k = (k * 10) + digit;
rep = rep / 10;
}
while (rep != 0);
if (n == k)
{
A = true;
}
else
{
A = false;
}
Console.WriteLine("Является ли палиндромом: {0}", A);
return A;
}
public int perevert()
{
int rep = a;
int k = 0, digit;
do
{
digit = rep % 10;
k = (k * 10) + digit;
rep = rep / 10;
}
while (rep != 0);
Console.WriteLine("Перевертыш числа: {0}", k);
return k;
}
}
class NaturalArray : Natural
{
private List<Natural> array;
public void Print1()
{
Console.WriteLine("Задание третье:");
}
public override void Print()//вывод
{
Console.WriteLine("Массив натуральных чисел");
array.ForEach(x => Console.WriteLine(x.Value));
}
public NaturalArray(Natural[] array)
{
this.array = new List<Natural>();
this.array.AddRange(array);
}
private int Max()
{
int max = 0;
foreach (var item in array)
{
int count = item.Kolichestvo1();
if (count > max) max = count;
}
return max;
}
public void BucketSort()
{
int numOfBuckets = Max();
List<Natural>[] buckets = new List<Natural>[numOfBuckets];
for (int i = 0; i < numOfBuckets; i++)
buckets[i] = new List<Natural>();
for (int i = 0; i < array.Count; i++)
{
int buckitChoice = (numOfBuckets / array[i].Value);
buckets[buckitChoice].Add(array[i]);
}
array.Clear();
for (int i = 0; i < numOfBuckets; i++)
{
List<Natural> temp = BubbleSortList(buckets[i]);
array.AddRange(temp);
}
}
public List<Natural> BubbleSortList(List<Natural> input)
{
for (int i = 0; i < input.Count; i++)
{
for (int j = 0; j < input.Count; j++)
{
if (input[i].Summa1() < input[j].Summa1())
{
int temp = input[i].Value;
input[i].Value = input[j].Value;
input[j].Value = temp;
}
}
}
return input;
}
public void Sort()
{
BucketSort();
Console.WriteLine("Массив отсортирован");
}
}
class Program
{
static void Main(string[] args)
{
BasedClass s = new BasedClass();
s.Print();
Natural s1 = new Natural();
s1.Kolichestvo();
s1.Summa();
s1.palind();
s1.perevert();
Random rand = new Random();
Natural[] array = new Natural[5];
for (int i = 0; i < array.Length; i++)
{
array[i] = new Natural(rand.Next(0, 1000));
}
NaturalArray naturalArray = new NaturalArray(array);
naturalArray.Print1();
naturalArray.Print();
naturalArray.Sort();
naturalArray.Print();
Console.ReadKey();
}
}
}
Помогите пожалуйста исправить 3 пункт в задаче