Advertisement
Blizzardo1

Simple Calculator by Blizzeta

Aug 11th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. /*
  2.     Simple Calculator
  3.     Copyright Blizzardo1 and Blizzeta Internals 2013
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace MathmaticalSolutions
  13. {
  14.     class Program
  15.     {
  16.         private static Dictionary<string, MethodInfo> Commands = new Dictionary<string, MethodInfo>();
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             double number = 0;
  21.             Console.WriteLine("Blizzeta Mathmatical tool Version 0.1a");
  22.             Console.WriteLine("Copyright (c) 2013 Blizzeta Internals. All rights reserved.\n");
  23.             Commands.Add("add", GetCommand("Add"));
  24.             Commands.Add("sub", GetCommand("Subtract"));
  25.             Commands.Add("mul", GetCommand("Multiply"));
  26.             Commands.Add("div", GetCommand("Divide"));
  27.             Commands.Add("clear", GetCommand("Clear"));
  28.            
  29.             double X = 0, Y = 0;
  30.             string lastcom = string.Empty;
  31.  
  32.             for(;;)
  33.             {
  34.                 try
  35.                 {
  36.                     Console.Write("{0}> ", number);
  37.                     string[] commandLine = Console.ReadLine().Split(' ');
  38.                     IEnumerable<string> CommandsArray = (from C in Commands select C.Key);
  39.                     List<object> commandlist = new List<object>();
  40.                    
  41.                     string com = commandLine[0];
  42.  
  43.                     if (commandLine.Length > 1)
  44.                     {
  45.                         X = double.Parse(commandLine[1]);
  46.                         commandlist.Add(X);
  47.  
  48.                         if (commandLine.Length > 2)
  49.                         {
  50.                             Y = double.Parse(commandLine[2]);
  51.                             commandlist.Add(Y);
  52.                            
  53.                         }
  54.                         else
  55.                             commandlist.Add(0);
  56.                     }
  57.                    
  58.                     if(string.IsNullOrEmpty(com))
  59.                     {
  60.                         number = (double)Commands[lastcom].Invoke(null, new object[] { number, X });
  61.                     }
  62.  
  63.                     foreach (string command in CommandsArray)
  64.                     {
  65.                         if (com == command)
  66.                             number = (double)Commands[com].Invoke(null, commandlist.ToArray());
  67.                     }
  68.                     lastcom = !string.IsNullOrEmpty(com) ? com : lastcom;
  69.                 }
  70.                 catch (DivideByZeroException)
  71.                 {
  72.                     Console.WriteLine("Cannot divide by Zero!");
  73.                 }
  74.                 catch (FormatException)
  75.                 {
  76.                     Console.WriteLine("NOT A NUMBER");
  77.                 }
  78.                 catch (Exception ex)
  79.                 {
  80.                     Console.WriteLine(ex);
  81.                 }
  82.             }
  83.         }
  84.  
  85.         static MethodInfo GetCommand(string Command)
  86.         {
  87.             Type Class = Type.GetType("MathmaticalSolutions.Program");
  88.             return Class.GetMethod(Command);
  89.         }
  90.            
  91.         public static double Clear()
  92.         {
  93.             return 0;
  94.         }
  95.  
  96.         public static double Add(double X, double Y)
  97.         {
  98.             return X + Y;
  99.         }
  100.  
  101.         public static double Subtract(double X, double Y)
  102.         {
  103.             return X - Y;
  104.         }
  105.  
  106.         public static double Multiply(double X, double Y)
  107.         {
  108.             return X * Y;
  109.         }
  110.  
  111.         public static double Divide(double X, double Y)
  112.         {
  113.             return X / Y;
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement