Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace KawaiiDesu
  7. {
  8.     class Program
  9.     {
  10.         static List<Action> actions = new List<Action>() { Path1, Path2, Path3, Path4, Path5, Path6, Path7, PassInput, averageStudents, tallestDude, randomNum, EETisCrap, hardOne, hardTwo, hardThree, hardFour };
  11.         static void Main(string[] args)
  12.         {
  13.             while (true)
  14.             {
  15.                 try
  16.                 {
  17.                     var num = (int)vignekit.numInput("\n    Index cesty: ");
  18.                     if (num > actions.Count-1) throw new Exception("       Neplatný index (0-"+(actions.Count-1)+")");
  19.                     actions[num].Invoke();
  20.                 }
  21.                 catch (Exception ex)
  22.                 {
  23.                     Console.ForegroundColor = ConsoleColor.Red;
  24.                     Console.WriteLine(ex.Message);
  25.                     Console.ResetColor();
  26.                 }
  27.             }
  28.         }
  29.         static void Path1()
  30.         {
  31.             Random gen = new Random();
  32.             int over5 = 0, even = 0, total = 0;
  33.             for (var i = 0; i < 10; i++)
  34.             {
  35.                 int value = gen.Next(11);
  36.                 if (value > 5) over5++;
  37.                 if (value % 2 == 0) even++;
  38.                 total += value;
  39.             }
  40.             Console.WriteLine(@"     {0} jich bylo větší než 5.
  41.     {1} jich bylo sudých.
  42.     {2} je jejich součet.", over5, even, total);
  43.         }
  44.         static void Path2()
  45.         {
  46.             Random gen = new Random();
  47.             int counter = 0;
  48.             for (var i = 0; i < 10; i++) {
  49.                 int value = gen.Next(-100, 101);
  50.                 if (value > 10 && value < 50) counter++;
  51.             }
  52.             Console.WriteLine("    {0} čísel je v rozhraní 10-50", counter);
  53.         }
  54.         static void Path3(){
  55.             int counter = 0;
  56.             for (var i = 0; i < 10; i++)
  57.             {
  58.                 float value = vignekit.numInput("      Zadej " + (i + 1) + ". číslo: ");
  59.                 if (value > 10 && value < 50) counter++;
  60.             }
  61.             Console.WriteLine("    {0} čísel je v rozhraní 10-50", counter);
  62.         }
  63.         static void Path4(){
  64.             Random gen = new Random();
  65.             HashSet<int> nums = new HashSet<int>();
  66.             while (nums.Count < 6)
  67.             {
  68.                 nums.Add(gen.Next(1, 50));
  69.             }
  70.             Console.WriteLine("\n    [ {0} ]", string.Join(", ", nums));
  71.         }
  72.         static void Path5()
  73.         {
  74.             Random gen = new Random();
  75.             int attempts = 10, wrong = 0;
  76.             for (var i = 0; i < attempts; i++) {
  77.                 int num1 = gen.Next(11), num2 = gen.Next(11);
  78.                 int answer = (int)vignekit.numInput("    "+num1+" * "+num2+" = ");
  79.                 if (answer != num1 * num2)
  80.                 {
  81.                     wrong++;
  82.                     Console.WriteLine("      Špatně...");
  83.                 }
  84.             }
  85.             Console.WriteLine("    {0} chyb", wrong);
  86.         }
  87.         static void Path6(){
  88.             int dice = new Random().Next(1, 7), attempts = 0;
  89.             while (true)
  90.             {
  91.                 int val = (int)vignekit.numInput("    Co jsem hodil za číslo? ");
  92.                 if (val == dice)
  93.                     break;
  94.                 else
  95.                     attempts++;
  96.                 Console.WriteLine("     Špatně.");
  97.             }
  98.             Console.WriteLine("     Správně! Uhádls to na {0}. pokus", attempts+1);
  99.         }
  100.         static void Path7(){
  101.             Random gen = new Random();
  102.             int illc = (int)vignekit.numInput("  Kolikrát mám hodit kostkou? ");
  103.             int[] calc = new int[6];
  104.             for (var i = 0; i < illc; i++) {
  105.                 calc[gen.Next(0, 6)]++;
  106.             }
  107.            
  108.             for (var i = 0; i < calc.Length; i++) {
  109.  
  110.                 Console.WriteLine("    {0} padlo {1}%", i + 1, (float)calc[i] / illc *100);
  111.             }
  112.         }
  113.        
  114.         //index 7 - 1. priklad
  115.         static void PassInput()
  116.         {
  117.             bool accepted = false;
  118.             int attempts = 0;
  119.             string pass = "hehe";
  120.             while (attempts < 2) {
  121.                 Console.Write("\n    Heslo: ");
  122.                 accepted = Console.ReadLine() == pass;
  123.                 if (accepted) break;
  124.                 Console.WriteLine("Invalid Password."); attempts++;
  125.             }
  126.             if (accepted)
  127.                 Console.WriteLine("Permission granted.");
  128.             else
  129.                 Console.WriteLine("Permission denied.");
  130.         }
  131.         //index 8 - 2. priklad
  132.         static void averageStudents()
  133.         {
  134.             List<int> nums = new List<int>();
  135.             int lastNum = 1;
  136.             while (lastNum != 0) {
  137.                 lastNum = (int)vignekit.numInput("   Zadej počet žáků: ");
  138.                 if (lastNum > 0) nums.Add(lastNum);
  139.             }
  140.             Console.WriteLine("Průměrný počet žáků ve třídě: {0}", (nums.Count == 0) ? "nikdo do školy nechodí." : ((int)nums.Average()).ToString());
  141.         }
  142.         //index 9 - 5. priklad
  143.         static void tallestDude() {
  144.             bool keepGoing = true;
  145.             string recordName = ""; float recordHeight = -1;
  146.             while (keepGoing) {
  147.                 Console.Write("\n\nZadej jméno studenta: ");
  148.                 string name = Console.ReadLine();
  149.                 if (string.IsNullOrWhiteSpace(name)) break;
  150.                 float height = vignekit.numInput("Zadej výšku studenta: ");
  151.  
  152.                 if (height > recordHeight)
  153.                 {
  154.                     recordName = name;
  155.                     recordHeight = height;
  156.                 }
  157.             }
  158.             Console.WriteLine("  Nejvyšší člověk je {0}, který je {1}m vysoký", recordName, recordHeight);
  159.         }
  160.         //index 10 - 7. priklad
  161.         static void randomNum() {
  162.             int dice = new Random().Next(1, 11), attempts = 0;
  163.             while (true)
  164.             {
  165.                 int val = (int)vignekit.numInput("    Co jsem si vymyslel za číslo? ");
  166.                 if (val == dice)
  167.                     break;
  168.                 else
  169.                     attempts++;
  170.                 Console.WriteLine("     Špatně.");
  171.             }
  172.             Console.WriteLine("     Správně! Uhádls to na {0}. pokus", attempts + 1);
  173.         }
  174.         //index 11 - 8. priklad
  175.         static void diceThrower() {
  176.             Random gen = new Random(); int attempts = 0;
  177.             while (true) {
  178.                 attempts++;
  179.                 int newNum = gen.Next(1, 7);
  180.                 if (newNum == 6) break;
  181.             }
  182.             Console.WriteLine(" Padlo mi to po {0} pokusech.", attempts);
  183.         }
  184.         //index 12 - 3. priklad
  185.         static void EETisCrap()
  186.         {
  187.             List<int> nums = new List<int>(); int lastNum = 1, overboard = 0;
  188.            
  189.             while (lastNum != 0) {
  190.                 lastNum = (int)vignekit.numInput("   Zadej částku: ");
  191.                 if (lastNum > 0) nums.Add(lastNum);
  192.                 if (lastNum > 100) overboard++;
  193.             }
  194.  
  195.             Console.WriteLine("Utratil jsi {0}Kč, z toho bylo {1} nákupů nad 100Kč.", nums.Sum(), overboard);
  196.         }
  197.         //index 13 - 1. tezky priklad
  198.         static void hardOne()
  199.         {
  200.             string output = "";
  201.             for (var i = 0; i < 10; i++) {
  202.                 output += (i+1).ToString();
  203.                 Console.WriteLine(output);
  204.             }
  205.         }
  206.         //index 14 - 2. tezky priklad
  207.         static void hardTwo()
  208.         {
  209.             Random gen = new Random();
  210.             for (var i = 0; i < 20; i++) {
  211.                 Console.WriteLine(new String('*', gen.Next(20)));
  212.             }
  213.         }
  214.         //index 15 - 3. tezky priklad
  215.         static void hardThree()
  216.         {
  217.             List<int> primes = new List<int>();
  218.             for (var i = 0; i < 1000; i++) {
  219.                 if (vignekit.isPrime(i)) primes.Add(i);
  220.             }
  221.             Console.WriteLine("\n    Prvočísla v rozhraní 0-1000:\n      [ {0} ] ", string.Join(", ", primes));
  222.         }
  223.         //index 16 - 4. tezky priklad
  224.         static void hardFour()
  225.         {
  226.             for (var i = 0; i < 10; i++)
  227.             {
  228.                 List<int> nums = new List<int>();
  229.                 for (var y = 0; y < 10; y++)
  230.                 {
  231.                     nums.Add((y + 1) * (i+1));
  232.                 }
  233.                 Console.WriteLine("[ {0} ]", string.Join(", ", nums));
  234.             }
  235.         }
  236.     }
  237.     class vignekit
  238.     {
  239.         public static float numInput(string prompt = "Zadej číslo: ", string invalid = "Neplatné číslo.")
  240.         {
  241.             bool isValid = false; float val = 0;    //celkem na tom nezalezi, z loopu by nevylezl
  242.             while (!isValid)
  243.             {
  244.                 Console.Write("\n{0}", prompt);
  245.                 isValid = float.TryParse(Console.ReadLine(), out val);
  246.                 if (!isValid)
  247.                 {
  248.                     Console.WriteLine(invalid);
  249.                 }
  250.             }
  251.             return val;
  252.         }
  253.         public static bool isPrime(float n)
  254.         {
  255.             if (n <= 1) return false;
  256.             for (var i = 2; i < n; i++) {
  257.                 if (n % i == 0) return false;
  258.             }
  259.             return true;
  260.         }
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement