Exemplo n.º 1
0
 void CreateOperators()
 {
     // Only one of each operation Token needs to be created
     opAdd = new Add(workStack);
     opSubtract = new Subtract(workStack);
     opMultiply = new Multiply(workStack);
     opDivide = new Divide(workStack);
     opPower = new Power(workStack);
     opBracket = new Bracket();
     opUnaryMinus = new UnaryMinus(workStack);
     opUnaryPlus = new UnaryPlus();
     opSqrt = new Sqrt(workStack);
     opSin = new Sin(workStack);
     opCos = new Cos(workStack);
     opTan = new Tan(workStack);
     opLog = new Log(workStack);
     opAsin = new Asin(workStack);
     opAcos = new Acos(workStack);
     opAtan = new Atan(workStack);
     functions = new Dictionary<string, Function>
     {
         {"sqr", opSqrt },
         {"sin", opSin },
         {"cos", opCos },
         {"tan", opTan },
         {"log", opLog },
         {"asin", opAsin },
         {"acos", opAcos },
         {"atan", opAtan }
     };
     binaryOperators = new Dictionary<char, BinaryOperator>
     {
         {'+', opAdd },
         {'-', opSubtract },
         {'*', opMultiply },
         {'/', opDivide },
         {'^',opPower }
     };
 }
Exemplo n.º 2
0
        //THE MOTHER FUNCTION
        public int handledIt(string input)
        {
            //declare variable to hold result from operation
            int result = 0;

            //set stack_record so that I can access the last user input
            stack_record.lastQ = input;

            //create new expression class to handle parsing user input
            Expression myExp = new Expression();

            //allow the container to be set from the parseExpression method
            Container answer = myExp.ParseExpression(input);

            //search the equation for a constant and parse it out to do the math on.
            string ContString = "abcdefghijklmnopqrstuvwxyz";

            foreach (var letter in input)
            {
                if (ContString.IndexOf(letter) == 0)
                {
                    Console.WriteLine("No Constant Defined");
                }
                else
                {
                }
            }


            //shorthand the variables from Container class
            int    lhs      = answer.LHS;
            int    rhs      = answer.RHS;
            char   op       = answer.OP;
            string constant = answer.CONS;

            //run the operator logic
            if (op == '+')
            {
                Add add_me = new Add();
                result = add_me.addIt(lhs, rhs);
            }
            else if (op == '-')
            {
                Subtract subtract_me = new Subtract();
                result = subtract_me.subtractIt(lhs, rhs);
            }
            else if (op == '*')
            {
                Multiply multiply_me = new Multiply();
                result = multiply_me.multiplyIt(lhs, rhs);
            }
            else if (op == '/')
            {
                Divide divide_me = new Divide();
                result = divide_me.divideIt(lhs, rhs);
            }
            else if (op == '%')
            {
                Modulo modulo_me = new Modulo();
                result = modulo_me.moduloIt(lhs, rhs);
            }
            else if (op == '=')
            {
                conts.Add(constant, rhs);
                result = rhs;
            }
            else
            {
                throw new InvalidOperationException("no valid operator found");
            }

            //set stack_record result for later access
            stack_record.last = result;
            return(result);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            int flag = 1;
            int a =0;
            int b = 0;
            int res = 0;
            int i = 0;

            int op;
            char cont;
            string[] operations = new string[] { "Addition", "Subtraction", "Multipliaction", "Division", "Exit" };
            while(flag == 1)
            {
                Console.Clear();
                Console.Write("Please enter the operation to be performed :\n");
                //Console.Write("\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n5) exit\n\n");
                i = 1;
                foreach (string option in operations)
                {
                    Console.Write("\n" + i + ") " + option + "\n");
                    i++;
                }
                op = Convert.ToInt32(Console.ReadLine());

                if(op < 5)
                {
                    Console.Write("\n************************************************************************\n");

                    Console.Write("Please enter 2 integers \n");
                    a = Convert.ToInt32(Console.ReadLine());
                    b = Convert.ToInt32(Console.ReadLine());

                }

                switch(op)
                {

                    case 1:
                        Console.Write("\n************************************************************************\n");
                        Console.Write("Operation to be performed is {0}\n", operations[0]);
                        Add op1 = new Add();
                        res = op1.addition(a, b);
                        break;
                    case 2:
                        Console.Write("\n************************************************************************\n");
                        Console.Write("Operation to be performed is {0}\n", operations[1]);
                        Sub op2 = new Sub();
                        res = op2.substraction(a, b);
                        break;
                    case 3:
                        Console.Write("\n************************************************************************\n");
                        Console.Write("Operation to be performed is {0}\n", operations[2]);
                        multiply op3 = new multiply();
                        res = op3.multiplication(a, b);
                        break;
                    case 4:
                        Console.Write("\n************************************************************************\n");
                        Console.Write("Operation to be performed is {0}\n", operations[3]);
                        if (b == 0)
                        {
                            Console.Write("Division by zero error !!! \n\nPlease enter a non zero value for b \n");

                            b = Convert.ToInt32(Console.ReadLine());
                        }
                        divide op4 = new divide();
                        res = op4.division(a, b);
                        break;
                    case 5:
                        Console.Write("\n************************************************************************\n");
                        Console.Write("Operation to be performed is {0}\n", operations[4]);
                        Environment.Exit(0);
                        break;
                    default :
                        Console.Write("Invalid operation\n");
                        break;
                }
                Console.Write("\n************************************************************************\n");
                Console.Write("\nResult of the operation is : {0}", res);
                Console.Write("\n************************************************************************\n");
                Console.Write("\n\nDo you want to continue : Y or N\n");
                cont = Convert.ToChar(Console.ReadLine());

                if (cont == 'N' || cont == 'n')
                    flag = 0;

            }
        }