Beispiel #1
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);
        }
Beispiel #2
0
        // evaluator

        private void btnEq_Click(object sender, EventArgs e)
        {
            Expression expression;
            Double     sndArgument = new Double();


            if (textBoxRes.Text.Equals("x") || textBoxRes.Text.Equals("y") || textBoxRes.Text.Equals("z"))
            {
                using (var form = new CalculatorValueForVariables())
                {
                    var result = form.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        sndArgument = form.varValue;
                    }
                }
            }
            else
            {
                sndArgument = Double.Parse(textBoxRes.Text);
            }

            switch (calcOperator)
            {
            case "sqrt":
                if (!(resultValue < 0))
                {
                    expression      = new Sqrt(resultValue);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't calculate sqrt of a neg num!";
                }
                break;

            case "neg":
                expression      = new Opposite(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "cos":
                expression      = new Cosinus(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "sin":
                expression      = new Sinus(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "exp":
                expression      = new Exponentation(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "abs":
                expression      = new Abs(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "log":
                if (resultValue > 0)
                {
                    expression      = new Log(resultValue);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't calculate log of a neg num!";
                }
                break;

            case "+":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Addition(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "-":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Subtraction(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "*":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Multiplication(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "/":
                labelCurrentExpr.Text += sndArgument;
                if (!sndArgument.Equals(0))
                {
                    expression      = new Division(resultValue, sndArgument);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't divide by 0";
                }
                break;

            case "^":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Power(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "%":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Modulo(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;
            }
            resultValue           = 0;
            labelCurrentExpr.Text = "";
        }
Beispiel #3
0
 public void SetUp()
 {
     _modulo = new Modulo();
 }