Blizzardo1 icon

Simple Calculator by Blizzeta

Blizzardo1 | PRO | 08/11/13 08:54:20 PM UTC | 0 ⭐ | 774 👁️ | Never ⏰ | []
C# |

3.64 KB

|

None

|

0 👍

/

0 👎

/*
    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<string, MethodInfo> Commands = new Dictionary<string, MethodInfo>();
 
        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<string> CommandsArray = (from C in Commands select C.Key);
                    List<object> commandlist = new List<object>();
                    
                    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;
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎