Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace KolokwiumPodstawyProgramowania
  8. {
  9.     class Funkcje
  10.     {
  11.         public static uint WybierzNumerZadania()
  12.         {
  13.             uint n = 0;
  14.             bool poprawny = false;
  15.  
  16.             while (poprawny == false)
  17.             {
  18.                 Console.WriteLine("Wybierz numer zadania (1-14).");
  19.                 Console.WriteLine("Jesli koniec to wpisz 15.");
  20.                 if (UInt32.TryParse(Console.ReadLine(), out n))
  21.                 {
  22.                     if(n>0 && n<16)
  23.                     {
  24.                         poprawny = true;
  25.                     }
  26.                     else
  27.                     {
  28.                         Console.Clear();
  29.                         Console.WriteLine("Podales zla liczbe.");
  30.                     }
  31.                 }
  32.                 else
  33.                 {
  34.                     Console.Clear();
  35.                     Console.WriteLine("Podales zla liczbe.");
  36.                 }
  37.             }
  38.             return n;
  39.         }
  40.  
  41.         public static uint WprowadzLiczbeCalkowita()
  42.         {
  43.             uint n = 0;
  44.             bool poprawny = false;
  45.             int czyPierwszePrzejscie = 0;
  46.             Console.WriteLine("Podaj liczbe calkowita.");
  47.             while (poprawny == false)
  48.             {
  49.                 if (UInt32.TryParse(Console.ReadLine(), out n))
  50.                 {
  51.                     poprawny = true;
  52.                 } else
  53.                 {
  54.                     if (czyPierwszePrzejscie == 0) Console.WriteLine("");
  55.                     ClearLine();
  56.                     ClearLine();
  57.                     Console.WriteLine("Podales zla liczbe.");
  58.                 }
  59.                 if (czyPierwszePrzejscie == 0) czyPierwszePrzejscie = 1;
  60.             }
  61.             Console.Clear();
  62.             return n;
  63.         }
  64.  
  65.         public static int WprowadzLiczbeNaturalna()
  66.         {
  67.             int n = 0;
  68.             bool poprawny = false;
  69.             int czyPierwszePrzejscie = 0;
  70.             Console.WriteLine("Podaj liczbe naturalna.");
  71.             while (poprawny == false)
  72.             {
  73.                 if (Int32.TryParse(Console.ReadLine(), out n))
  74.                 {
  75.                     poprawny = true;
  76.                 }
  77.                 else
  78.                 {
  79.                     if (czyPierwszePrzejscie == 0) Console.WriteLine("");
  80.                     ClearLine();
  81.                     ClearLine();
  82.                     Console.WriteLine("Podales zla liczbe.");
  83.                 }
  84.                 if (czyPierwszePrzejscie == 0) czyPierwszePrzejscie = 1;
  85.             }
  86.             Console.Clear();
  87.             return n;
  88.         }
  89.  
  90.         public static string WprowadzSlowo()
  91.         {
  92.             string str = "";
  93.             bool poprawny = false;
  94.             int czyPierwszePrzejscie = 0;
  95.             while (poprawny == false)
  96.             {
  97.                 str = Console.ReadLine();
  98.                 if (czyPierwszePrzejscie == 0) Console.WriteLine("");
  99.                 poprawny = true;
  100.                 for(int i = 0; i < str.Length; i++)
  101.                 {
  102.                     if ((str[i] < 65 || str[i] > 90) && (str[i] < 97 || str[i] > 122))
  103.                     {
  104.                         poprawny = false;
  105.                         ClearLine();
  106.                         ClearLine();
  107.                         Console.WriteLine("To nie jest slowo.");
  108.                         break;
  109.                     }
  110.                 }
  111.                 if (str.Length == 0)
  112.                 {
  113.                     poprawny = false;
  114.                     ClearLine();
  115.                     ClearLine();
  116.                     Console.WriteLine("To nie jest slowo.");
  117.                 }
  118.                 if (czyPierwszePrzejscie == 0) czyPierwszePrzejscie = 1;
  119.             }
  120.             Console.Clear();
  121.             return str;
  122.         }
  123.  
  124.         public static void ClearLine()
  125.         {
  126.             Console.SetCursorPosition(0, Console.CursorTop - 1);
  127.             Console.Write(new string(' ', Console.WindowWidth));
  128.             Console.SetCursorPosition(0, Console.CursorTop - 1);
  129.         }
  130.  
  131.         public static void Zadanie1()
  132.         {
  133.             Console.WriteLine("Podaj imie.");
  134.             string imie = WprowadzSlowo();
  135.             imie = imie.ToLower();
  136.             imie = char.ToUpper(imie[0]) + imie.Substring(1);
  137.             Console.WriteLine(imie);
  138.  
  139.             char ostatniChar = imie[imie.Length - 1];
  140.             if (ostatniChar == 97)
  141.             {
  142.                 Console.WriteLine("Wprowadzone imie jest zenskie.");
  143.             }
  144.             else
  145.             {
  146.                 Console.WriteLine("Wprowadzone imie jest meskie.");
  147.             }
  148.         }
  149.  
  150.         public static void Zadanie2()
  151.         {
  152.             Console.WriteLine("Podaj wielkosc tablicy.");
  153.             uint n = WprowadzLiczbeCalkowita();
  154.  
  155.             int[] arr = new int[n];
  156.             Random rand = new Random();
  157.  
  158.             for (int i = 0; i < arr.Length; i++)
  159.             {
  160.                 arr[i] = rand.Next(0, 100);
  161.                 Console.Write(arr[i] + " ");
  162.             }
  163.  
  164.             Console.WriteLine("\nNajwieksza wartosc z tablicy: " + arr.Max());
  165.             Console.WriteLine("Najmniejsza wartość z tablicy: " + arr.Min());
  166.         }
  167.  
  168.         public static void Zadanie3()
  169.         {
  170.             int a, b;
  171.             bool wynik = false;
  172.             Console.WriteLine("Podaj pierwsza liczbe.");
  173.             a = WprowadzLiczbeNaturalna();
  174.             Console.WriteLine("Podaj druga liczbe.");
  175.             b = WprowadzLiczbeNaturalna();
  176.  
  177.             int i = 0;
  178.             int j = b.ToString().Length - 1;
  179.  
  180.             if (a.ToString().Length == b.ToString().Length)
  181.             {
  182.                 wynik = true;
  183.                 do
  184.                 {
  185.                     if (a.ToString()[i] != b.ToString()[j])
  186.                     {
  187.                         wynik = false;
  188.                         break;
  189.                     }
  190.                     i++;
  191.                     j--;
  192.                 } while (i < a.ToString().Length);
  193.             }
  194.             if (wynik)
  195.             {
  196.                 Console.WriteLine("To sa liczby lustrzane.");
  197.             }
  198.             else
  199.             {
  200.                 Console.WriteLine("To nie sa liczby lustrzane.");
  201.             }
  202.         }
  203.  
  204.         public static void Zadanie4()
  205.         {
  206.             Console.WriteLine("Podaj wielkosc tablicy.");
  207.             uint n = WprowadzLiczbeCalkowita();
  208.  
  209.             int[] arr = new int[n];
  210.             Random rnd = new Random();
  211.  
  212.             int los;
  213.             for (int i = 0; i < arr.Length; i++)
  214.             {
  215.                 los = rnd.Next(0, 100);
  216.                 if (arr.Contains(los) == false)
  217.                 {
  218.                     arr[i] = los;
  219.                 }
  220.                 else
  221.                 {
  222.                     i--;
  223.                 }
  224.             }
  225.  
  226.             for (int i = 0; i < arr.Length; i++)
  227.             {
  228.                 Console.Write(arr[i] + " ");
  229.             }
  230.         }
  231.  
  232.         public static void Zadanie5()
  233.         {
  234.             Console.WriteLine("Podaj liczbe.");
  235.             uint a, b;
  236.             a = WprowadzLiczbeCalkowita();
  237.             b = 0;
  238.  
  239.             for (int i = 1; i <= a; i++)
  240.             {
  241.  
  242.                 while (b < i)
  243.                 {
  244.                     Console.Write("$");
  245.                     b++;
  246.                 }
  247.                 b = 0;
  248.                 Console.Write(" ");
  249.             }
  250.         }
  251.  
  252.         public static void Zadanie6()
  253.         {
  254.             Random rand = new Random();
  255.             Console.WriteLine("Podaj wielkosc tablicy.");
  256.             uint n = WprowadzLiczbeCalkowita();
  257.             int[] arr = new int[n];
  258.  
  259.             int sumaP = 0;
  260.             int sumaN = 0;
  261.             for(int i = 0; i < arr.Length; i++)
  262.             {
  263.                 arr[i] = rand.Next(0, 100);
  264.                 if (arr[i] % 2 == 0)
  265.                 {
  266.                     sumaP += arr[i];
  267.                 }
  268.                 else
  269.                 {
  270.                     sumaN += arr[i];
  271.                 }
  272.                 Console.Write(arr[i] + " ");
  273.             }
  274.             Console.WriteLine("");
  275.             Console.WriteLine("Suma parzystych to: " + sumaP);
  276.             Console.WriteLine("Suma nieparzystych to: " + sumaN);
  277.         }
  278.  
  279.         public static void Zadanie7()
  280.         {
  281.             // bez kontroli bledu podczas wprowadzania
  282.             Console.WriteLine("Podaj PESEL.");
  283.             string pesel = Console.ReadLine();
  284.             string liczbaKontrolna = "13791379131";
  285.             int liczba;
  286.             int suma = 0;
  287.  
  288.             if (pesel.Length == 11)
  289.             {
  290.                 for (int i = 10; i >= 0; i--)
  291.                 {
  292.                     liczba = Convert.ToInt32(pesel[i]) * Convert.ToInt32(liczbaKontrolna[i]);
  293.                     suma += liczba;
  294.                 }
  295.  
  296.                 if (suma % 10 == 0)
  297.                 {
  298.                     Console.WriteLine("To jest PESEL.");
  299.                 }
  300.                 else
  301.                 {
  302.                     Console.WriteLine("To nie jest PESEL.");
  303.                 }
  304.             }
  305.             else
  306.             {
  307.                 Console.WriteLine("To nie jest PESEL.");
  308.             }
  309.         }
  310.  
  311.         public static void Zadanie8()
  312.         {
  313.             Console.WriteLine("Wprowadz liczbe.");
  314.             uint n = WprowadzLiczbeCalkowita();
  315.             uint wynik = 0;
  316.             uint temp = n;
  317.  
  318.             while (n!=0)
  319.             {
  320.                 wynik += n % 10;
  321.                 n /= 10;
  322.             }
  323.             Console.WriteLine("Suma calkowita liczby " + temp + " to: " + wynik);
  324.         }
  325.  
  326.         public static void Zadanie9()
  327.         {
  328.             Console.WriteLine("Podaj wielkosc tablicy.");
  329.             Random rand = new Random();
  330.             uint n = WprowadzLiczbeCalkowita();
  331.             int[] arr = new int[n];
  332.             for (int i = 0; i < arr.Length; i++)
  333.             {
  334.                 arr[i] = rand.Next(0, 2);
  335.                 Console.Write(arr[i] + " ");
  336.             }
  337.             Console.WriteLine("");
  338.             var grupy = arr.GroupBy(v => v);
  339.             int najwiecej = 0;
  340.             foreach (var grupa in grupy)
  341.             {
  342.                 if (grupa.Count() > najwiecej)
  343.                 {
  344.                     najwiecej = grupa.Count();
  345.                 }
  346.             }
  347.             Console.Write("Wartosc/wartosci:");
  348.             foreach (var grupa in grupy)
  349.             {
  350.                 if (grupa.Count() == najwiecej)
  351.                 {
  352.                     Console.Write(" " + grupa.Key);
  353.                 }
  354.             }
  355.             Console.Write(" wystepuja najwiecej razy t.j.: " + najwiecej);
  356.         }
  357.  
  358.         public static void Zadanie10()
  359.         {
  360.             //Gotowe rozwiazanie jest w necie wystarczy przekopiowac
  361.         }
  362.  
  363.         public static void Zadanie11()
  364.         {
  365.             Console.WriteLine("Podaj liczbe.");
  366.             uint n = WprowadzLiczbeCalkowita();
  367.             uint suma = 0;
  368.  
  369.             for (uint i = 2; i < n/2; i++)
  370.             {
  371.                 if (n % i == 0)
  372.                 {
  373.                     suma += i;
  374.                     n /= i;
  375.                     i = 1;
  376.                 }
  377.             }
  378.             suma += n;
  379.  
  380.             Console.WriteLine("Suma czynnikow pierwszych to: " + suma);
  381.         }
  382.  
  383.         public static void Zadanie12()
  384.         {
  385.             Console.WriteLine("Podaj wzor.");
  386.             string wzor = WprowadzSlowo();
  387.             wzor = wzor.ToUpper();
  388.             Console.WriteLine("Podaj przesuniecie.");
  389.             uint k = WprowadzLiczbeCalkowita();
  390.             k = k % 26;
  391.             //char temp;
  392.  
  393.             for(int i = 0; i < wzor.Length; i++)
  394.             {
  395.                 //niedokonczone
  396.             }
  397.         }
  398.  
  399.         public static void Zadanie13()
  400.         {
  401.             //nie rozumiem co tutaj trzeba zrobic
  402.         }
  403.  
  404.         public static void Zadanie14()
  405.         {
  406.             //nie zdazylem
  407.         }
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement