Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using static System.Console;
- namespace zad1
- {
- class Person:IComparable
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public DateTime BirthDay { get; set; }
- public int CompareTo(object obj)
- {
- throw new NotImplementedException();
- }
- public override string ToString()
- {
- return String.Format($"{FirstName} {LastName} {BirthDay.ToString("yyyy-MM-dd")}");
- }
- }
- class Program
- {
- private static Random random = new Random();
- static void Main(string[] args)
- {
- const string ENTER = "Naciśnij ENTER, aby kontynuować";
- const int SIZE = 5;
- Person[] a = new Person[SIZE];
- Person[] b = new Person[SIZE];
- WriteLine("Naciśnij ESC, aby zakończyć działanie programu.");
- WriteLine(ENTER);
- while (ReadKey().Key != ConsoleKey.Escape)
- {
- WriteLine("\nMenu: ");
- WriteLine("1 - dodanie obiektu/-ów do tablicy A");
- WriteLine("2 - usuwanie obiektu/-ów z tablicy A");
- WriteLine("3 - sortowanie obiektów w tablicy A");
- WriteLine("4 - kopiowanie wycinka z tablicy A do tablicy B");
- WriteLine("5 - zmiana rozmiaru tablicy A");
- WriteLine("6 - dodanie obiektu/-ów do tablicy B");
- WriteLine("7 - usuwanie obiektu/-ów z tablicy B");
- WriteLine("8 - sortowanie obiektów w tablicy B");
- WriteLine("9 - zmiana rozmiaru tablicy B");
- WriteLine("a - wyprowadzenie zawartości tablicy A na monitor");
- WriteLine("b - wyprowadzenie zawartości tablicy B na monitor");
- WriteLine("r - randomizuj wartości obu tablic");
- WriteLine("z - Zakończ program");
- string choice;
- choice = ReadLine();
- if (choice.Length > 1 && choice != "")
- {
- choice = choice.Substring(0, 1);
- }
- if(choice == "1")
- {
- a = addToTable(a);
- }
- if (choice == "2")
- {
- a = deleteFromTable(a);
- }
- if (choice == "3")
- {
- Array.Sort(a);
- }
- if (choice == "4")
- {
- b = copyPartOfTableToTable(a, b);
- }
- if (choice == "5")
- {
- a = changeTableSize(a);
- }
- if (choice == "6")
- {
- b = addToTable(b);
- }
- if (choice == "7")
- {
- b = deleteFromTable(b);
- }
- if (choice == "8")
- {
- Array.Sort(b);
- }
- if (choice == "9")
- {
- b = changeTableSize(b);
- }
- if (choice == "a" || choice == "A")
- {
- showTable(a);
- }
- if (choice == "b" || choice == "A")
- {
- showTable(b);
- }
- if (choice == "r" || choice == "R")
- {
- a = randomValuesInTable(a);
- b = randomValuesInTable(b);
- WriteLine("ZRANDOMIZOWANO !!");
- }
- if (choice == "z" || choice == "Z")
- {
- break;
- }
- }
- WriteLine("Do widzenia !!!");
- }
- public static Person[] changeTableSize(Person[] p)
- {
- WriteLine("Proszę wpisać nowy rozmiar tablicy ");
- int size = Int32.Parse(ReadLine());
- Person[] temp = new Person[size];
- if (size < p.Length)
- {
- WriteLine("Wpisany rozmiar jest mniejszy od dotychczasowego! Niektóre elementy zostaną usunięte!");
- for (int i = 0; i < size; i++)
- {
- temp[i] = p[i];
- }
- }
- else
- {
- for (int i = 0; i < p.Length; i++)
- {
- temp[i] = p[i];
- }
- }
- return temp;
- }
- public static Person[] addToTable(Person[] p)
- {
- WriteLine($"Od którego indexu chcesz dodawać? - możesz od 0 do {p.Length - 1}");
- int index = Int32.Parse(ReadLine());
- if(index < 0 || index > (p.Length - 1))
- {
- WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
- index = 0;
- }
- int people;
- int max = p.Length - index;
- WriteLine($"Ile osób chcesz dodać do tablicy? - max:{max}");
- people = Int32.Parse(ReadLine());
- if (people > max)
- {
- WriteLine("Niepoprawna liczba osób!");
- people = 0;
- }
- for (int i = 0; i < people; i++)
- {
- WriteLine($"\n Dodanie osoby {i+1}");
- WriteLine("Proszę podać imię:");
- string name = ReadLine();
- WriteLine("Proszę podać nazwisko:");
- string surname = ReadLine();
- WriteLine("Proszę podać datę urodzenia:");
- DateTime date = DateTime.Parse(ReadLine());
- p[index] = new Person { LastName = surname, FirstName = name, BirthDay = date };
- index++;
- }
- return p;
- }
- public static Person[] deleteFromTable(Person[] p)
- {
- WriteLine($"Od którego indexu chcesz usuwać? - możesz od 0 do {p.Length - 1}");
- int index = Int32.Parse(ReadLine());
- if (index < 0 || index > (p.Length - 1))
- {
- WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
- index = 0;
- }
- int people;
- int max = p.Length - index;
- WriteLine($"Ile osób chcesz usunąć z tablicy? - max:{max}");
- people = Int32.Parse(ReadLine());
- if (people > max)
- {
- WriteLine("Niepoprawna liczba osób!");
- people = 0;
- }
- for (int i = 0; i < people; i++)
- {
- p[index] = null;
- index++;
- }
- return p;
- }
- public static Person[] copyPartOfTableToTable(Person[] a, Person[] b)
- {
- WriteLine($"Od którego indexu chcesz skopiować tablicę? - możesz od 0 do {a.Length - 1}");
- int index = Int32.Parse(ReadLine());
- if (index < 0 || index > (a.Length - 1))
- {
- WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
- index = 0;
- }
- int people;
- int max = a.Length - index;
- WriteLine($"Ile osób chcesz przenieść? - max:{max}");
- people = Int32.Parse(ReadLine());
- if (people > max)
- {
- WriteLine("Niepoprawna liczba osób!");
- people = 0;
- }
- Person[] temp = new Person[people];
- for(int i = 0; i < temp.Length; i++)
- {
- temp[i] = a[index];
- index++;
- }
- WriteLine($"Od którego indexu chcesz wkleić skopiowaną część tablicy? - możesz od 0 do {b.Length - 1}");
- int indexB = Int32.Parse(ReadLine());
- if (indexB < 0 || indexB > (b.Length - 1))
- {
- WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
- indexB = 0;
- }
- for (int j = 0; j < people; j++)
- {
- b[indexB] = temp[j];
- indexB++;
- }
- return b;
- }
- public static Person[] randomValuesInTable(Person[] p)
- {
- for(int i = 0; i < p.Length; i++)
- {
- string name = randomString(5);
- string surname = randomString(10);
- DateTime date = randomDay();
- p[i] = new Person {LastName = surname, FirstName = name, BirthDay = date };
- }
- return p;
- }
- public static DateTime randomDay()
- {
- DateTime start = new DateTime(1995, 1, 1);
- int range = (DateTime.Today - start).Days;
- return start.AddDays(random.Next(range));
- }
- public static string randomString(int length)
- {
- const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklzxcvbnm";
- return new string(Enumerable.Repeat(chars, length)
- .Select(s => s[random.Next(s.Length)]).ToArray());
- }
- public static void showTable(Person[] p)
- {
- for(int i = 0; i < p.Length; i++)
- {
- if (p[i] == null)
- {
- WriteLine($"osoba - {i}");
- }
- else {
- WriteLine(p[i].ToString());
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement