using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static Dictionary> unaryIntExpressions = new Dictionary> { { '!', x => Factorial(x) }, }; static Dictionary> binaryIntExpressions = new Dictionary> { { '+', (x, y) => x + y }, { '-', (x, y) => x - y }, { '*', (x, y) => x * y }, { '/', (x, y) => x / y }, { '%', (x, y) => x % y }, { 'o', (x, y) => Math.Pow(x, 1 / y) }, { 'm', (x, y) => Math.Pow(x, y) }, }; static Dictionary> binaryDoubleExpressions = new Dictionary> { { '+', (x, y) => x + y }, { '-', (x, y) => x - y }, { '*', (x, y) => x * y }, { '/', (x, y) => x / y }, { 'o', (x, y) => Math.Pow(x, 1/y) }, { 'm', (x, y) => Math.Pow(x, y) }, }; static double Factorial(long n) { long x = 1; for (int i = 2; i <= n; ++i) x *= i; return x; } static char DoMenu(string title, params string[] options) { Console.Clear(); PrintCopyright(); Console.WriteLine(); Console.WriteLine(title); Console.WriteLine(); foreach (var option in options) Console.WriteLine(option); Console.WriteLine(); Console.Write(" Vyber hodnotu: "); var c = (char)Console.Read(); Console.ReadLine(); // Vymazáni Bufferu return char.ToLower(c); } static void DoMainMenu() { while (true) { var option = DoMenu("Hlavní menu", " C/c - Celá čisla", " D/d - Desetinná čísla", " K/k - Konec\n"); switch (option) { case 'c': DoIntegerMenu(); break; case 'd': DoDoubleMenu(); break; case 'k': return; } } } static void DoIntegerMenu() { while (true) { var option = DoMenu(" Sub-Menu - Celá čísla", " + - Sčítání", " - - Odčítání", " * - Násobení", " / - Dělení", " % - Zbytek po dělení", " ! - Faktoriál", " O/o - Odmocnina", " M/m - Mocnina", " N/n - Návrat do hlavního menu"); if (option == 'n') return; if (binaryIntExpressions.ContainsKey(option)) DoBinaryOperation(binaryIntExpressions[option]); if (unaryIntExpressions.ContainsKey(option)) DoUnaryOperation(unaryIntExpressions[option]); } } static void DoDoubleMenu() { while (true) { var option = DoMenu(" Sub-Menu - Desetinná čísla", " + - Sčítání", " - - Odčítání", " * - Násobení", " / - Celočíselné dělení", " M/m - Mocnina", " O/o - Odmocnina", " N/n - Návrat do hlavního menu"); if (option == 'n') return; if (binaryDoubleExpressions.ContainsKey(option)) DoBinaryOperation(binaryDoubleExpressions[option]); } } static void DoUnaryOperation(Func func) { Console.Clear(); PrintCopyright(); Console.WriteLine(); Console.Write(" Zadej číslo: "); long x = Convert.ToInt64(Console.ReadLine()); Console.WriteLine("\n Výsledek: " + func(x)); Console.ReadLine(); } static void DoBinaryOperation(Func func) { Console.Clear(); PrintCopyright(); Console.WriteLine(); Console.Write(" Zadej 1. číslo: "); long x = Convert.ToInt64(Console.ReadLine()); Console.Write(" Zadej 2. číslo: "); long y = Convert.ToInt64(Console.ReadLine()); Console.WriteLine("\n Výsledek: " + func(x, y)); Console.ReadLine(); } static void DoBinaryOperation(Func func) { Console.Clear(); PrintCopyright(); Console.WriteLine(); Console.Write(" Zadej 1. číslo: "); double x = Convert.ToDouble(Console.ReadLine()); Console.Write(" Zadej 2. číslo: "); double y = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("\n Výsledek: " + func(x, y)); Console.ReadLine(); } static void PrintCopyright() { Console.WriteLine("\n\n Kalkulačka @ Petr Bayer 2012\n\n"); } static void Main(string[] args) { Console.Title = "Kalkulačka @ Petr Bayer 2012"; DoMainMenu(); Console.WriteLine("Done."); Console.ReadLine(); } } }