Skip to content

Instantly share code, notes, and snippets.

@pmikstacki
Created October 22, 2017 21:16
Show Gist options
  • Save pmikstacki/51c0aa66d8a1bdb2d9c2eb16a3bc4a4a to your computer and use it in GitHub Desktop.
Save pmikstacki/51c0aa66d8a1bdb2d9c2eb16a3bc4a4a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using lista;
namespace lista.Exercises
{
/// <summary>
/// Ćwiczenie pierwsze
/// </summary>
public class ExerciseOne : IExercise
{
public int Exercisenumber => 1;
public string Name => "Zliczanie Liter \"a\" i \"A\"";
public string Description => @"program, który pyta o imię użytkownika. Następnie zlicza ile liter ’a’ (nie
rozróżniamy dużych i małych liter) występuje w tym imieniu.";
public void Execute()
{
Console.WriteLine("Wpisz swoje Imię:");
string name = Console.ReadLine();
int ilerazy = 0;
foreach (char a in name)
{
if (a == 'a' || a == 'A')
{
ilerazy++;
}
}
Console.WriteLine("Ilość Razy: " + ilerazy.ToString());
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie drugie
/// </summary>
public class ExerciseTwo : IExercise
{
public int Exercisenumber => 2;
public string Name => "Linia z gwiazdek";
public string Description => @"program, który stworzy linię z gwiazdek o długości zadanej przez użytkownika.";
public void Execute()
{
try
{
Console.WriteLine("Ile Gwiazdek: ");
int ilegw = int.Parse(Console.ReadLine());
for (int i = 0; i <= ilegw; i++)
{
Console.Write("*");
}
Console.WriteLine("");
}
catch (Exception ex)
{
Console.WriteLine("Wpisałeś źle");
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie trzecie
/// </summary>
public class ExerciseThree : IExercise
{
public int Exercisenumber => 3;
public string Name => "Trójkąt z gwiazdek";
public string Description =>
@"program, który stworzy trójkąt z gwiazdek na podstawie liczby podanej przez użytkownika.";
public void Execute()
{
try
{
Console.WriteLine("Jaki duży trójkąt?");
int size = int.Parse(Console.ReadLine());
do
{
for (int i = 0; i < size; i++)
{
Console.Write('*');
}
Console.WriteLine();
size--;
} while (size > 0);
}
catch (Exception ex)
{
Console.WriteLine("Podałeś nieprawidłową wartość");
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie czwarte
/// </summary>
public class ExerciseFour : IExercise
{
public int Exercisenumber => 4;
public string Name => "Choinka";
public string Description => "Rysuje choinkę o zadanych parametrach";
public void Execute()
{
int h;
int stojak;
int odstepy;
int pomoc_odstepy_nozka;
int gwiazdki = 1;
Console.WriteLine("Podaj wysokosc choinki: ");
h = int.Parse(Console.ReadLine());
stojak = 2 * (h - 1) - 1;
odstepy = h - 2;
pomoc_odstepy_nozka = odstepy;
for (int i = 0; i < h - 1; i++)
{
for (int j = 0; j < odstepy; j++)
{
Console.Write(" ");
}
for (int j = 0; j < gwiazdki; j++)
{
Console.Write("*");
}
Console.WriteLine();
gwiazdki = gwiazdki + 2;
odstepy--;
}
for (int j = 0; j < pomoc_odstepy_nozka; j++)
{
Console.Write(" ");
}
Console.WriteLine("#");
for (int i = 0; i < stojak; i++)
{
Console.Write('-');
}
Console.WriteLine();
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie piąte
/// </summary>
public class ExerciseFive : IExercise
{
public int Exercisenumber => 5;
public string Name => "Szachownica";
public string Description => "Rysuje szachownicę o zadanej szerokości";
public void Execute()
{
try
{
bool hash = false, parz = false;
Console.WriteLine("Podaj szerokość szachownicy: ");
int ilosc = int.Parse(Console.ReadLine());
var iloscw = ilosc;
if (iloscw % 2 == 0)
{
parz = !parz;
}
do
{
for (int i = 0; i < iloscw; i++)
{
if (parz)
{
hash = !hash;
if (hash)
{
Console.Write("#");
}
else
{
Console.Write(" ");
}
}
else
{
if (hash)
{
Console.Write("#");
}
else
{
Console.Write(" ");
}
hash = !hash;
}
}
if (iloscw % 2 == 0)
{
parz = !parz;
Console.WriteLine();
}
else
{
Console.WriteLine();
}
ilosc--;
} while (ilosc > 0);
}
catch (Exception ex)
{
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie szóste
/// </summary>
public class ExerciseSix : IExercise
{
public int Exercisenumber => 6;
public string Name => "Szachownica (Jedna pętla)";
public string Description => "Rysuje szachownicę o zadanej szerokości (w jednej pętli)";
public void Execute()
{
try
{
Console.WriteLine("Podaj szerokość szachownicy: ");
var ilosc = int.Parse(Console.ReadLine());
var iloscw = ilosc;
var rzad = 1;
var kolumna = 1;
while (ilosc > 0)
{
if ((kolumna % 2 == 1 && rzad % 2 == 0) || (kolumna % 2 == 0 && rzad % 2 == 1))
{
Console.Write(" ");
}
else if ((kolumna % 2 == 0 && rzad % 2 == 0) || (kolumna % 2 == 1 && rzad % 2 == 1))
{
Console.Write("#");
}
kolumna += 1;
if (kolumna > iloscw)
{
Console.WriteLine();
kolumna = 1;
rzad++;
ilosc--;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie siódme
/// </summary>
public class ExerciseSeven : IExercise
{
public int Exercisenumber => 7;
public string Name => "Odwrotność";
public string Description => "Wypisuje słowo wpisane przez użytkownika w odwrotnej kolejności";
public void Execute()
{
Console.WriteLine("Wpisz Słowo: ");
string slowo = Console.ReadLine();
Console.WriteLine(new string(Enumerable.Range(1, slowo.Length).Select(i => slowo[slowo.Length - i]).ToArray()));
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie ósme
/// </summary>
public class ExerciseEight : IExercise
{
public int Exercisenumber => 8;
public string Name => "Duże litery bez spacji";
public string Description => "Wypisuje słowo wpisane przez użytkownika dużymi literami bez spacji";
public void Execute()
{
Console.WriteLine("Wpisz Słowo: ");
Console.WriteLine(Console.ReadLine().Replace(" ", "").ToUpper());
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie 9
/// </summary>
public class ExerciseNine : IExercise
{
public int Exercisenumber => 9;
public string Name => "Anagram";
public string Description => "Sprawdza czy słowo jest anagramem";
public void Execute()
{
Console.WriteLine("Wpisz swoje słowo: ");
string slowo = Console.ReadLine();
Console.WriteLine(
(slowo == new string(Enumerable.Range(1, slowo.Length).Select(i => slowo[slowo.Length - i]).ToArray()))
? "Jest Anagramem"
: "Nie jest anagramem");
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie dziesiąte
/// </summary>
public class ExerciseTen : IExercise
{
public int Exercisenumber => 10;
public string Name => "Zamiana kotek na piesek";
public string Description => "Zamienia w zdaniu słowo \"kotek\" na słowo \"piesek\"";
public void Execute()
{
Console.WriteLine("Wpisz zdanie: ");
Console.WriteLine(Console.ReadLine().Replace("kotek", "piesek"));
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie jedenaste
/// </summary>
public class ExerciseEleven : IExercise
{
public int Exercisenumber => 11;
public string Name => "Licznik sąsiadujących \"o\" ";
public string Description => "Liczy sąsiadujące literki o w zdaniu";
public void Execute()
{
Console.WriteLine("Wpisz zdanie zawierające pary \"o\": ");
int ilo = 0;
string ilos = Console.ReadLine();
string iloww = ilos.ToLower();
for (int i = 0; i < iloww.Length - 1; i++)
{
string ss = iloww[i].ToString() + iloww[i + 1];
if (ss == "oo")
{
ilo++;
}
}
Console.WriteLine("Ilość par \"o\": " + ilo.ToString());
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie dwunaste
/// </summary>
public class ExerciseTwelve : IExercise
{
public int Exercisenumber => 12;
public string Name => "Ilość słów w zdaniu";
public string Description => "Podaje ilość słów w zdaniu";
public void Execute()
{
Console.WriteLine("Wpisz swoje zdanie: ");
Console.WriteLine("To Zdanie ma " + Console.ReadLine().Split(new char[] {' '}).Length.ToString() + " słów.");
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
}
/// <summary>
/// Ćwiczenie trzynaste
/// </summary>
public class ExerciseThirteen : IExercise
{
public int Exercisenumber => 13;
public string Name => "Suma cyfr liczby naturalnej";
public string Description => "Oblicza sumę cyfr w liczbie naturalnej";
public void Execute()
{
try
{
int l = 0;
int ilo = 0;
while (true)
{
Console.WriteLine("Wpisz liczbę naturalna");
l = int.Parse(Console.ReadLine());
if (l < 0)
{
Console.WriteLine("Liczba nie jest naturalna!");
}
else
{
for (int i = 0; i < l.ToString().Length; i++)
{
ilo += int.Parse(l.ToString()[i].ToString());
}
break;
}
}
Console.WriteLine("Suma cyfr: " + ilo.ToString());
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception e)
{
}
}
}
/// <summary>
/// Ćwiczenie czternaste
/// </summary>
public class ExerciseFourteen : IExercise
{
public int Exercisenumber => 14;
public string Name => "Liczba binarna";
public string Description => "Program, który zapisuje podaną przez użytkownika liczbę naturalną wpostaci binarnej.";
public void Execute()
{
try
{
Console.WriteLine("Wpisz liczbę naturalną: ");
Console.WriteLine("Liczba w systemie binarnym: " + Convert.ToString(int.Parse(Console.ReadLine()), 2));
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception e)
{
}
}
}
public class ExerciseFifteen : IExercise
{
public int Exercisenumber => 15;
public string Name => "Tabliczka Mnożenia";
public string Description =>
"program, który wyświetla na ekranie tabliczkę mnożenia o wymiarze zadanym przez użytkownika.";
public void Execute()
{
try
{
Console.WriteLine("Wpisz wymiar tabliczki mnożenia: ");
int roz = int.Parse(Console.ReadLine());
for (int x = 1; x <= roz; x++)
{
for (int y = 1; y <= roz; y++)
{
if ((x * y).ToString().Length == 4)
Console.Write((x * y).ToString() + " ");
else if ((x * y).ToString().Length == 3)
Console.Write((x * y).ToString() + " ");
else if ((x * y).ToString().Length == 2)
Console.Write((x * y).ToString() + " ");
else
Console.Write((x * y).ToString() + " ");
}
Console.WriteLine();
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception ex)
{
}
}
}
public class ExerciseSixteen : IExercise
{
public int Exercisenumber => 16;
public string Name => "kalkulator";
public string Description =>
"Program pyta o liczbę a, o rodzaj działania (czyli jeden ze znaków +,-,*,/), oraz o liczbę b. " +
"\n W przypadku dzielenia program sprawdza poprawność danych, aby nie wystąpiło dzielenie przez zero." +
" Jeśli liczby były poprawne, to wypisuje wynik działania na liczbach a i b";
public void Execute()
{
while (true)
{
try
{
Console.WriteLine("Podaj liczbę a");
var a = int.Parse(Console.ReadLine());
Console.WriteLine("Podaj rodzaj działania: ");
switch (Console.ReadLine())
{
case "+":
Console.WriteLine("Podaj liczbę b");
Console.WriteLine(a + int.Parse(Console.ReadLine()));
break;
case "-":
Console.WriteLine("Podaj liczbę b");
Console.WriteLine(a - int.Parse(Console.ReadLine()));
break;
case "*":
Console.WriteLine("Podaj liczbę b");
Console.WriteLine(a * int.Parse(Console.ReadLine()));
break;
case "/":
Console.WriteLine("Podaj liczbę b");
var b = int.Parse(Console.ReadLine());
if (b == 0)
{
Console.WriteLine("Nie można wykonać dzielenia");
break;
}
else
{
Console.WriteLine(a / b);
break;
}
default:
Console.WriteLine("Nierozpoznane działanie...");
break;
}
}
catch
{
Console.WriteLine("Niepoprawna liczba!");
}
Console.WriteLine("Chcesz Kontynuować obliczenia? (t/n)");
if (Console.ReadLine() == "t") continue;
if (Console.ReadLine() == "n") break;
}
}
}
public class ExerciseSeventeen : IExercise
{
public int Exercisenumber => 17;
public string Name => "Równania Kwadratowe";
public string Description => "Oblicza pierwiastki równania kwadratowego (zespolone też!)";
public void Execute()
{
try
{
double a, b, c;
Console.WriteLine("Wprowadź a: ");
a = double.Parse(Console.ReadLine());
Console.WriteLine("Wprowadź b: ");
b = double.Parse(Console.ReadLine());
Console.WriteLine("Wprowadź c: ");
c = double.Parse(Console.ReadLine());
var delta = Math.Pow(b, 2) - 4 * (a * c);
if (delta > 0)
{
Console.WriteLine("X1 = " + (((-b) - Math.Sqrt(delta)) / (2 * a)).ToString() + "\n X2 = " +
(((-b) + Math.Sqrt(delta)) / (2 * a)).ToString());
}
else if (delta == 0)
{
Console.WriteLine("X = " + (-b / (2 * a)).ToString());
}
else
{
Console.WriteLine("\nPierwiastek Re: " + (-b / (2 * a)).ToString());
Console.WriteLine("\nPierwiastek Ia: " + (Math.Abs((Math.Sqrt(Math.Abs(delta)) / (2 * a))).ToString()));
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
public class ExerciseEighteen : IExercise
{
public int Exercisenumber => 18;
public string Name => "Układ Równań";
public string Description => "Program, który oblicza rozwiązania (jeśli istnieją) układu równań";
double a, b, c, d, e, f;
public void Execute()
{
try
{
Console.WriteLine("Podaj a:");
a = double.Parse(Console.ReadLine());
Console.WriteLine("Podaj b:");
b = double.Parse(Console.ReadLine());
Console.WriteLine("Podaj c:");
c = double.Parse(Console.ReadLine());
Console.WriteLine("Podaj d:");
d = double.Parse(Console.ReadLine());
Console.WriteLine("Podaj e:");
e = double.Parse(Console.ReadLine());
Console.WriteLine("Podaj f:");
f = double.Parse(Console.ReadLine());
var W = (a * e) - (b * d);
var Wx = (c * e) - (b * f);
var Wy = (a * f) - (d * c);
if (W == 0 && Wx == 0 && Wy == 0)
{
Console.WriteLine("Układ jest nieoznaczony (ma nieskończenie wiele rozwiązań) ");
}
else if (W == 0 && Wx != 0 && Wy != 0)
{
Console.WriteLine("Układ jest sprzeczny (nie ma rozwiązań) ");
}
else
{
Console.WriteLine("x = " + Wx / W);
Console.WriteLine("y = " + Wy / W);
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception exception)
{
}
}
}
public class ExerciseNineteen : IExercise
{
public int Exercisenumber => 19;
public string Name => "Ciągi";
public string Description =>
"program, który zamienia podaną przez użytkownika liczbę całkowitą w postaci ciągu znaków na liczbę typu int";
public void Execute()
{
try
{
char[] numbers = new char[11] {'-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
Console.WriteLine("Podaj Liczbę: ");
string ciag = Console.ReadLine();
string newciag = "";
if (numbers.Any(x => x == ciag[0]))
{
foreach (var c in ciag)
{
if (c == '.') break;
else
{
if (numbers.Any(x => x == c))
{
newciag += c;
}
}
}
Console.WriteLine(newciag);
}
else
{
Console.WriteLine("0");
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception e)
{
Console.WriteLine("Niepoprawna liczba");
}
}
}
public class ExerciseTwenty : IExercise
{
public int Exercisenumber => 20;
public string Name => "Liczba pierwsza";
public string Description => "Sprawdza czy podana przez użytkownika liczba jest liczbą pierwszą";
public void Execute()
{
try
{
Console.WriteLine("Wpisz liczbę: ");
var pierwsza = int.Parse(Console.ReadLine());
if (JestPierwsza(pierwsza))
{
Console.WriteLine("Liczba jest pierwsza");
}
else
{
Console.WriteLine("Liczba nie jest pierwsza");
}
Console.WriteLine("Aby zakończyć ćwiczenie, naciśnij dowolny klawisz...");
Console.ReadKey(true);
}
catch (Exception e)
{
Console.WriteLine("Niepoprawna liczba");
}
}
public bool JestPierwsza(int liczba)
{
if (liczba == 1 || liczba == 2) return true;
if (liczba > 1)
{
return Enumerable.Range(1, liczba).Where(x => liczba % x == 0)
.SequenceEqual(new[] {1, liczba}); //Raczej chyba już bardziej się nie da tego zoptymalizować
}
return false;
}
}
public class ExerciseTwentyOne : IExercise
{
public int Exercisenumber => 21;
public string Name => "Ciągi 4 znakowe";
public string Description => "Układa ciągi 4 znakowe z (a, b, c)";
public Random random = new Random();
public void Execute()
{
char[] literki = new char[] {'a', 'b', 'c'};
List<string> ciagi = new List<string>();
for (int i = 0; i < 24; i++)
{
//string temporary =0;
//temporary += literki[random.Next(0, 2)];
//cdn...
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment