Witam,
Napisałem program sortujący podane przez użytkownika liczby. Działa, ale nie jest do końca taki, jaki powinien być. Zgodnie z zadaniem, elementami tablicy powinny być obiekty klasy Element. Dopiero zaczynam pracę z C# i książki, które czytam, nie pomagają mi tego ruszyć. Jakieś podpowiedzi/sugestie?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Projekt_1__konsola_
{
sealed class Element
{
int val;
public Element(int e)
{
val = e;
}
public int v
{
get
{
return val;
}
}
static void PodajLiczbę(string komunikat, out int liczba)
{
while (true)
{
Console.Write(komunikat);
string str = Console.ReadLine();
try
{
liczba = int.Parse(str);
break;
}
catch (FormatException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wprowadzono liczbę w złym formacie");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (OverflowException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wartość jest za duża albo za mała, pamiętaj że możesz podać liczby z zakresu 1 do 4294967295");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (ArgumentNullException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Napotkano koniec strumienia");
Console.ForegroundColor = ConsoleColor.Black;
}
Console.WriteLine("Spróbuj jeszcze raz");
}
}
static void Sort(int[] tablica)
{
for (uint i = 1; i < tablica.Length; i++)
{
uint j = i;
int buf = tablica[j];
while ((j > 0) && (tablica[j - 1] > buf))
{
tablica[j] = tablica[j - 1];
j--;
}
tablica[j] = buf;
}
}
static void Main(string[] args)
{
int liczba, element, l, m;
Console.Title = "Projekt"
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
PodajLiczbę("Podaj liczbę elementów do posortowania: ", out liczba);
int[] tablica = new int[liczba];
for (l = 0; l < liczba; l++)
{
PodajLiczbę("Podaj element [" + l + "]: ", out element);
for (m = 0; m <= tablica.Length; m++)
{
tablica[l] = element;
}
}
Sort(tablica);
Console.WriteLine("Posortowane elementy: ");
for (l = 0; l < liczba; l++)
{
Console.WriteLine("Element [{0}] = {1}", l, tablica[l]);
}
}
}
}