Exemplo n.º 1
0
        public Fraction DivideFractions()
        {
            fraction  = userInterface.getFraction();
            fraction2 = userInterface.getFraction();
            result.Allocate(fraction.Divide(fraction2));

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs through an array of MathNodes executing the supplied operator only.  Not perfect... i think multiplication and division are same priority
        /// </summary>
        /// <param name="input"></param>
        /// <param name="op"></param>
        /// <returns></returns>
        internal static MathNode[] ProcessOperator(MathNode[] input, string op)
        {
            int elements = input.Length;
            var ret      = new List <MathNode>();

            for (var i = 0; i < input.Length; i++)
            {
                if (input[i].NodeType == Enums.NodeType.Operation && input[i].Operator == op)
                {
                    Fraction?temp = null;
                    if (OperatorHasLeftAndRightMovesAvailable(i, input.Length))
                    {
                        //Make sure an operator is surrounded by numbers only
                        if (input[i - 1].NodeType != Enums.NodeType.Fraction || input[i + 1].NodeType != Enums.NodeType.Fraction)
                        {
                            throw new InvalidOperationException($"Tried to process an operator not surrounded by 2 numbers. i {i}, node types: {input[i - 1].NodeType}, {input[i].NodeType}, {input[i + 1].NodeType}");
                        }
                        Fraction last = (Fraction)input[i - 1].Value;
                        Fraction next = (Fraction)input[i + 1].Value;
                        if (last == null || next == null)
                        {
                            throw new InvalidOperationException($"Tried to process a null number node. i {i}, last: {last}, next: {next}");
                        }
                        switch (op)
                        {
                        case OperatorConsts.DIVIDE:
                            //Need to test for divide by 0,
                            if (next == 0)
                            {
                                throw new InvalidOperationException($"About to divide by 0 {last}/{next}");
                            }
                            else
                            {
                                temp = last.Divide(next);
                                break;
                            }

                        case OperatorConsts.MULTIPLY:
                            temp = last.Multiply(next);
                            break;

                        case OperatorConsts.PLUS:
                            temp = last.Add(next);
                            break;

                        case OperatorConsts.MINUS:
                            temp = last.Subtract(next);
                            break;
                        }
                        //TODO: there is a better way to do this, can't think of it at the moment.  We already added the previous node to the ret list.  Yank it out.
                        //Add this number.  And then, skip i ahead 1 because that way we skip over the processing of the next numeric node which will be a no-op, and
                        //add an entry in the ret list
                        ret.RemoveAt(ret.FindLastIndex(x => x.NodeType == Enums.NodeType.Fraction && x.Value == last));
                        ret.Add(new MathNode((Fraction)temp));
                        i++;
                    }
                    else
                    {
                        //Would use some normal logging...
                        Debug.WriteLine($"On index {i}, input is {input.Length} long, node type {input[i].NodeType}, operator? {input[i].Operator}, number? {input[i].Value}");
                        throw new InvalidOperationException($"Operator with no available next or previous fraction node. On index {i}, input is {input.Length}");
                    }
                }
                else
                {
                    ret.Add(input[i]);
                }
            }

            return(ret.ToArray());
        }