Esempio n. 1
0
 /// <summary>
 /// Create a MathNode to hold an operator
 /// </summary>
 /// <param name="input">Must be +, -, /, *</param>
 /// <exception cref="InvalidOperationException">When supplied a string other than the OperatorConstants values in AllowedOperators.IsAnOperator</exception>
 public MathNode(string input)
 {
     if (AllowedOperators.IsAnOperator(input))
     {
         Operator = input;
         NodeType = Enums.NodeType.Operation;
     }
     else
     {
         throw new InvalidOperationException($"{nameof(input)} is not in the list of allowed operators (* / + -)");
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Build an array of MathNodes which either have a fraction, or an operator in them
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static MathNode[] Parse(string[] input)
        {
            int elements = input.Length;

            MathNode[] operations = new MathNode[elements];
            for (int i = 0; i < input.Length; i++)
            {
                //Is it an operator>
                if (AllowedOperators.IsAnOperator(input[i]))
                {
                    operations[i] = new MathNode(input[i]);
                }
                //Try to handle it as a fraction
                else
                {
                    string c = CleanFraction(input[i]);
                    try
                    {
                        if (c.Contains(" "))
                        {
                            //Fractions library only takes care of the 1/2 part from 3 1/2
                            string[] split = c.Split(' ');
                            Fraction f1    = Fraction.FromString(split[0]);
                            Fraction f2    = Fraction.FromString(split[1]);
                            operations[i] = new MathNode(f1.Add(f2));
                        }
                        else
                        {
                            //Whole number or simple fraction
                            Fraction f = Fraction.FromString(c);
                            operations[i] = new MathNode(f);
                        }
                    }
                    catch (Exception e)
                    {
                        //Do some logging with e
                        Console.WriteLine($"Invalid fractional value {input[i]}. " + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace);
                        //Don't throw e or you may lose stack info, rethrow original error, probably a parse error from test values like 'bob'
                        throw;
                    }
                }
            }
            return(operations);
        }