/* Simple Calculator Copyright Blizzardo1 and Blizzeta Internals 2013 */ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MathmaticalSolutions { class Program { private static Dictionary Commands = new Dictionary(); static void Main(string[] args) { double number = 0; Console.WriteLine("Blizzeta Mathmatical tool Version 0.1a"); Console.WriteLine("Copyright (c) 2013 Blizzeta Internals. All rights reserved.\n"); Commands.Add("add", GetCommand("Add")); Commands.Add("sub", GetCommand("Subtract")); Commands.Add("mul", GetCommand("Multiply")); Commands.Add("div", GetCommand("Divide")); Commands.Add("clear", GetCommand("Clear")); double X = 0, Y = 0; string lastcom = string.Empty; for(;;) { try { Console.Write("{0}> ", number); string[] commandLine = Console.ReadLine().Split(' '); IEnumerable CommandsArray = (from C in Commands select C.Key); List commandlist = new List(); string com = commandLine[0]; if (commandLine.Length > 1) { X = double.Parse(commandLine[1]); commandlist.Add(X); if (commandLine.Length > 2) { Y = double.Parse(commandLine[2]); commandlist.Add(Y); } else commandlist.Add(0); } if(string.IsNullOrEmpty(com)) { number = (double)Commands[lastcom].Invoke(null, new object[] { number, X }); } foreach (string command in CommandsArray) { if (com == command) number = (double)Commands[com].Invoke(null, commandlist.ToArray()); } lastcom = !string.IsNullOrEmpty(com) ? com : lastcom; } catch (DivideByZeroException) { Console.WriteLine("Cannot divide by Zero!"); } catch (FormatException) { Console.WriteLine("NOT A NUMBER"); } catch (Exception ex) { Console.WriteLine(ex); } } } static MethodInfo GetCommand(string Command) { Type Class = Type.GetType("MathmaticalSolutions.Program"); return Class.GetMethod(Command); } public static double Clear() { return 0; } public static double Add(double X, double Y) { return X + Y; } public static double Subtract(double X, double Y) { return X - Y; } public static double Multiply(double X, double Y) { return X * Y; } public static double Divide(double X, double Y) { return X / Y; } } }