Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SeminarniPrace
- {
- class Program
- {
- static void Main(string[] args)
- {
- ConsoleHandling.PrintHeadline();
- while (true)
- {
- ConsoleHandling.SelectionInput("Vybraný režim: ");
- }
- }
- }
- class ConsoleHandling
- {
- static int a = -1, b = -1;
- public static void ClearCurrentConsoleLine()
- {
- Console.SetCursorPosition(0, Console.CursorTop);
- Console.Write(new string(' ', Console.WindowWidth));
- Console.SetCursorPosition(0, Console.CursorTop);
- }
- public static void PrintHeadline()
- {
- 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("1) úprava nezáporného celého čísla a = " + a);
- }
- if (b != -1)
- {
- Console.SetCursorPosition(0, 1);
- ClearCurrentConsoleLine();
- Console.WriteLine("2) úprava nezáporné celočíselné mocniny n = " + b);
- }
- Console.SetCursorPosition(0, 7);
- }
- public static void SelectionInput(string lineToPrint)
- {
- 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();
- break;
- case 2:
- b = InputNumberValid('b');
- PrintHeadline();
- break;
- case 3:
- Console.WriteLine(Computing.BinSentence(a, b, false));
- break;
- case 4:
- Console.WriteLine(Computing.BinSentence(a, b, true));
- break;
- default:
- Console.WriteLine("Zadána neplatná volba!");
- break;
- }
- }
- 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=1; i<n+1; i++)
- {
- 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)
- {
- if (a != -1 && n != -1)
- {
- char sign;
- if (negative == true) sign = '-';
- else sign = '+';
- string message = String.Format("(x {0} {1})^{2} = ", sign, a, n);
- string message2 = "";
- if (n == 0) message2 = "1";
- else if (a == 0 && n == 1) message2 = "x";
- else if (n == 1) message2 = String.Format("x {0} {1}", sign, a);
- else if (a == 0) message2 = String.Format("x^{0}", n);
- else
- {
- for (int i = 0; i < n + 1; i++)
- {
- if (i == 0) message2 += String.Format("x^{0}", n);
- if (negative == true && i % 2 == 1 && i != 0) message2 += (" - ");
- else if (i != 0) message2 += (" + ");
- if (i != n && i != 0 && (n - i) != 1) message2 += String.Format("{0}x^{1}", (ComNum(n, i) * Math.Pow(a, i)), n - i);
- else if (i != n && i != 0) message2 += String.Format("{0}x", (ComNum(n, i) * Math.Pow(a, i)));
- if (i == n) message2 += Math.Pow(a, n);
- }
- }
- return message + message2 + "\n";
- }
- else if (a == -1) return "Chybí hodnota čísla a. Bez jejího zadání nelze pokračovat!";
- else if (n == -1) return "Chybí hodnota mocniny n. Bez jejího zadání nelze pokračovat!";
- else return "Nejprve je nutné zadat hodnoty pro výpočet!";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment