Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SeminarniPrace
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a=-1, n=-1;
- ConsoleHandling.PrintHeadline();
- while (true)
- {
- (a, n) = ConsoleHandling.SelectionInput("Vybraný režim: ", a, n);
- }
- }
- }
- class ConsoleHandling
- {
- public static void ClearCurrentConsoleLine()
- {
- int currentLineCursor = Console.CursorTop;
- Console.SetCursorPosition(0, Console.CursorTop);
- Console.Write(new string(' ', Console.WindowWidth));
- Console.SetCursorPosition(0, currentLineCursor);
- }
- public static void PrintHeadline(int a = -1, int n = -1)
- {
- Console.Clear();
- Console.WriteLine("1) zadání nezáporného celého čísla a");
- Console.WriteLine("2) zadání nezáporné celočíselné mocniny n");
- Console.WriteLine("3) výpis binomické věty(x + a)^n");
- Console.WriteLine("4) výpis binomické věty (x -a)^n");
- Console.WriteLine("------------------------------------------------------");
- Console.WriteLine("0) ukončení programu");
- Console.WriteLine();
- if (a != -1)
- {
- Console.SetCursorPosition(0, 0);
- ClearCurrentConsoleLine();
- Console.WriteLine("2) úprava nezáporného celého čísla a = " + a);
- }
- if (n != -1)
- {
- Console.SetCursorPosition(0, 1);
- ClearCurrentConsoleLine();
- Console.WriteLine("2) úprava nezáporné celočíselné mocniny n = " + n);
- }
- Console.SetCursorPosition(0, 7);
- }
- public static (int,int) SelectionInput(string lineToPrint, int a, int b)
- {
- int caseIn = 5;
- Console.Write(lineToPrint);
- try
- {
- caseIn = int.Parse(Console.ReadLine());
- }
- catch (Exception ex)
- {
- if (ex is System.FormatException) { }
- else if (ex is System.OverflowException) { }
- else throw;
- }
- switch (caseIn)
- {
- case 0:
- Console.Clear();
- Console.WriteLine("Ukončuji program...");
- System.Environment.Exit(1);
- break;
- case 1:
- a = InputNumberValid('a');
- PrintHeadline(a,b);
- break;
- case 2:
- b = InputNumberValid('b');
- PrintHeadline(a,b);
- break;
- case 3:
- Console.WriteLine("Case 3");
- Console.WriteLine(Computing.BinSentence(a, b, false));
- break;
- case 4:
- Console.WriteLine("Case 4");
- Console.WriteLine(Computing.BinSentence(a, b, true));
- break;
- default:
- Console.WriteLine("Zadána neplatná volba!");
- break;
- }
- return (a, b);
- }
- static int InputNumberValid(char selectArg)
- {
- int userInput = -1;
- while (userInput < 0)
- {
- Console.Write("Zadejte hodnotu čísla " + selectArg + ": ");
- try
- {
- userInput = int.Parse(Console.ReadLine());
- if (userInput < 0) Console.WriteLine("Číslo nesmí být záporné, zkus to znovu!");
- }
- catch (Exception ex)
- {
- if (ex is System.FormatException) Console.WriteLine("Číslo nesmí obsahovat nečíselné znaky, zkus to znova!");
- else if (ex is System.OverflowException) Console.WriteLine("Zadáno neplatné číslo!");
- else throw;
- }
- }
- return userInput;
- }
- }
- class Computing
- {
- static int Factorial(int n)
- {
- int fn = 1;
- for(int i=2; i<n; i++)
- {
- fn = fn * i;
- }
- return fn;
- }
- static int ComNum(int n, int k)
- {
- int nk = Factorial(n) / (Factorial(n - k) * Factorial(k));
- return nk;
- }
- public static string BinSentence(int a, int n, bool negative)
- {
- char sign;
- string sentence = "";
- if (negative == true) sign = '-';
- else sign = '+';
- FormattableString message = $"( x {sign} {a} ) ^ {n} = ";
- FormattableString message2 = $"";
- if (n == 0)
- {
- return message.ToString() + "1";
- }
- for (int i=0; i<n; i++)
- {
- message2 = $"{ComNum(n,i)*Math.Pow(a, i)}x ^ {(n- i)} ";
- //FormattableString message2 = $"x ^ {n}";
- //sentence += comNum(n,i) n-i Math.Pow(a, i);
- }
- return message2.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement