Advertisement
zdenekpetrzd

Untitled

May 6th, 2021
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.35 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SeminarniPrace
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int a=-1, n=-1;
  10.             ConsoleHandling.PrintHeadline();
  11.             while (true)
  12.             {
  13.                 (a, n) = ConsoleHandling.SelectionInput("Vybraný režim: ", a, n);
  14.             }
  15.  
  16.         }
  17.     }
  18.  
  19.     class ConsoleHandling
  20.     {
  21.         public static void ClearCurrentConsoleLine()
  22.         {
  23.             int currentLineCursor = Console.CursorTop;
  24.             Console.SetCursorPosition(0, Console.CursorTop);
  25.             Console.Write(new string(' ', Console.WindowWidth));
  26.             Console.SetCursorPosition(0, currentLineCursor);
  27.         }
  28.         public static void PrintHeadline(int a = -1, int n = -1)
  29.         {
  30.             Console.Clear();
  31.             Console.WriteLine("1) zadání nezáporného celého čísla a");
  32.             Console.WriteLine("2) zadání nezáporné celočíselné mocniny n");
  33.             Console.WriteLine("3) výpis binomické věty(x + a)^n");
  34.             Console.WriteLine("4) výpis binomické věty (x -a)^n");
  35.             Console.WriteLine("------------------------------------------------------");
  36.             Console.WriteLine("0) ukončení programu");
  37.             Console.WriteLine();
  38.  
  39.             if (a != -1)
  40.             {
  41.                 Console.SetCursorPosition(0, 0);
  42.                 ClearCurrentConsoleLine();
  43.                 Console.WriteLine("2) úprava nezáporného celého čísla a = " + a);
  44.             }
  45.             if (n != -1)
  46.             {
  47.                 Console.SetCursorPosition(0, 1);
  48.                 ClearCurrentConsoleLine();
  49.                 Console.WriteLine("2) úprava nezáporné celočíselné mocniny n = " + n);
  50.             }
  51.             Console.SetCursorPosition(0, 7);
  52.         }
  53.  
  54.         public static (int,int) SelectionInput(string lineToPrint, int a, int b)
  55.         {
  56.             int caseIn = 5;
  57.             Console.Write(lineToPrint);
  58.             try
  59.             {
  60.                 caseIn = int.Parse(Console.ReadLine());
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 if (ex is System.FormatException) { }
  65.                 else if (ex is System.OverflowException) { }
  66.                 else throw;
  67.             }
  68.            
  69.             switch (caseIn)
  70.             {
  71.                 case 0:
  72.                     Console.Clear();
  73.                     Console.WriteLine("Ukončuji program...");
  74.                     System.Environment.Exit(1);
  75.                     break;
  76.                 case 1:
  77.                     a = InputNumberValid('a');
  78.                     PrintHeadline(a,b);
  79.                     break;
  80.                 case 2:
  81.                     b = InputNumberValid('b');
  82.                     PrintHeadline(a,b);
  83.                     break;
  84.                 case 3:
  85.                     Console.WriteLine("Case 3");
  86.                     Console.WriteLine(Computing.BinSentence(a, b, false));
  87.                     break;
  88.                 case 4:
  89.                     Console.WriteLine("Case 4");
  90.                     Console.WriteLine(Computing.BinSentence(a, b, true));
  91.                     break;
  92.                 default:
  93.                     Console.WriteLine("Zadána neplatná volba!");
  94.                     break;
  95.             }
  96.            
  97.             return (a, b);
  98.         }
  99.         static int InputNumberValid(char selectArg)
  100.         {
  101.             int userInput = -1;
  102.             while (userInput < 0)
  103.             {
  104.                 Console.Write("Zadejte hodnotu čísla " + selectArg + ": ");
  105.                 try
  106.                 {
  107.                     userInput = int.Parse(Console.ReadLine());
  108.                     if (userInput < 0) Console.WriteLine("Číslo nesmí být záporné, zkus to znovu!");
  109.                 }
  110.                 catch (Exception ex)
  111.                 {
  112.                     if (ex is System.FormatException) Console.WriteLine("Číslo nesmí obsahovat nečíselné znaky, zkus to znova!");
  113.                     else if (ex is System.OverflowException) Console.WriteLine("Zadáno neplatné číslo!");
  114.                     else throw;
  115.                 }
  116.             }
  117.             return userInput;
  118.         }
  119.     }
  120.  
  121.     class Computing
  122.     {
  123.         static int Factorial(int n)
  124.         {
  125.             int fn = 1;
  126.             for(int i=2; i<n; i++)
  127.             {
  128.                 fn = fn * i;
  129.             }
  130.             return fn;
  131.         }  
  132.         static int ComNum(int n, int k)
  133.         {
  134.             int nk = Factorial(n) / (Factorial(n - k) * Factorial(k));
  135.             return nk;
  136.         }
  137.         public static string BinSentence(int a, int n, bool negative)
  138.         {
  139.             char sign;
  140.             string sentence = "";
  141.             if (negative == true) sign = '-';
  142.             else sign = '+';
  143.  
  144.             FormattableString message = $"( x {sign} {a} ) ^ {n} = ";
  145.             FormattableString message2 = $"";
  146.  
  147.             if (n == 0)
  148.             {
  149.                 return message.ToString() + "1";
  150.             }
  151.  
  152.             for (int i=0; i<n; i++)
  153.             {
  154.  
  155.                 message2 = $"{ComNum(n,i)*Math.Pow(a, i)}x ^ {(n- i)} ";
  156.  
  157.  
  158.                 //FormattableString message2 = $"x ^ {n}";
  159.                 //sentence += comNum(n,i)  n-i Math.Pow(a, i);
  160.             }
  161.             return message2.ToString();
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement