Пример #1
0
        public static Equation[] ParseInput(string[] args)
        {
            Equation leftSide = new Equation();

            var currentSide     = new Equation();
            var equals          = false;
            var brackets        = false; // check if brackets is open
            var bracketEquation = new Equation();
            var bracketModifier = "1";
            var bracketOperator = '*';

            for (int i = 0; i < args.Length; i++)
            {
                if (equals)
                {
                    leftSide    = currentSide;
                    currentSide = new Equation(); // switches side of the equation (current side is now right side)
                    equals      = false;          // will no longer switch on loop
                }
                bool wasNumber = false;           // check if argument is a number
                foreach (var c in args[i])
                {
                    if (c == '=')
                    {
                        equals = true;
                    }
                    else if (IsNumber(c) && !wasNumber)
                    {
                        wasNumber = true;
                    }
                    else if (IsOperator(c) && args[i].Length == 1)
                    {
                        // is a single operator and not a modifier operator ( - as opposed to -4X )
                        wasNumber = false;
                        var operation = c;

                        //Check to see if multiple operators existing concurrently
                        if (i < args.Length - 1 && args[i + 1].Length == 1 && IsUnaryOperator(args[i][0]) && IsUnaryOperator(args[i + 1][0]))
                        {
                            operation = args[i][0] == args[i + 1][0] ? '+' : '-';
                            i++;
                            if (i + 1 < args.Length - 1 && args[i + 1].Length == 1 && IsUnaryOperator(args[i + 1][0]))
                            {
                                operation = operation == args[i + 1][0] ? '+' : '-';
                                i++;
                            }
                        }
                        if (brackets)
                        {
                            bracketEquation.Add(operation);
                        }
                        else
                        {
                            currentSide.Add(operation);
                        }
                    }
                    else if (c == '(')
                    {
                        wasNumber       = false;
                        brackets        = true;
                        bracketEquation = new Equation(); //renew variable incase of multiple brackets
                    }
                    else if (c == ')')                    // close bracket
                    {
                        if (i + 1 < args.Length && IsMultiDivide(args[i + 1][0]))
                        {
                            bracketModifier = args[i + 2];
                            bracketOperator = args[i + 1][0];
                            i += 2; // skip the next two arguments as you add them to the bracket
                        }
                        wasNumber = false;
                        brackets  = false;
                        currentSide.Add(bracketEquation, bracketModifier, bracketOperator);
                    }
                }
                if (wasNumber)
                {
                    if (brackets)
                    {
                        bracketEquation.Add(args[i]);
                    }
                    else if (i < args.Length - 1 && args[i + 1][0] == '(')   // next character is an open bracket
                    {
                        bracketModifier = args[i];
                    }
                    else
                    {
                        currentSide.Add(args[i]);
                    }
                }
            }
            return(new [] { leftSide, currentSide });
        }
Пример #2
0
 // Calculator Constructor - Create a new equation object by passing in an array of arguments
 public Calculator(string[] args)
 {
     equation = new Equation(args);
 }
Пример #3
0
 // Merge two equations together
 public void Merge(Equation equation)
 {
     _a -= equation._a;
     _b -= equation._b;
 }