Ejemplo n.º 1
0
        // Reads the statement preceding the index location of the NON-UNARY operator
        // SyntaxException is thrown if the character to the IMMEDIATE left is an operator
        private string ReadLeft(string statement, int operatorIndex)
        {
            if (StatementChecker.IsOperator(statement[operatorIndex - 1]))
            {
                throw new SyntaxException();
            }

            int counter = 1;

            for (int index = operatorIndex - 1; index > -1; index--)
            {
                // Checks for beginning of statement, OR the appearance of the next NON-UNARY operator
                // Returns the number BEFORE the operator

                if (index == 0)
                {
                    return(statement.Substring(index, counter));
                }
                else if (StatementChecker.IsArithmeticOperator(statement[index], statement, index))
                {
                    return(statement.Substring(index + 1, counter - 1));
                }

                counter++;
            }

            return(null);
        }
Ejemplo n.º 2
0
 static void Main(string[] args)
 {
     try
     {
         if (!StatementChecker.IsValidArgument(args))
         {
             Console.WriteLine(Constants.INVALID);
         }
         else
         {
             Calculator calculator = new Calculator(args[0]);
             calculator.CalculateStatement();
             calculator.PrintSolution();
         }
     }
     catch (SyntaxException)
     {
         Console.WriteLine(Constants.INVALID);
     }
     catch (OverflowException)
     {
         Console.WriteLine(Constants.OVERFLOW);
     }
     catch (DivideByZeroException)
     {
         Console.WriteLine(Constants.ZERO_DIVISION);
     }
 }
Ejemplo n.º 3
0
        // Reads the statement following index location of the operator
        // Checks for the end of the statement, OR the appearance of the next NON-unary operator
        // Returns the next number AFTER the operator
        private string ReadRight(string statement, int operatorIndex)
        {
            int counter = 1;

            for (int index = operatorIndex + 1; index < statement.Length; index++)
            {
                if (index == statement.Length - 1)
                {
                    return(statement.Substring(operatorIndex + 1, counter));
                }
                else if (StatementChecker.IsArithmeticOperator(statement[index], statement, index))
                {
                    return(statement.Substring(operatorIndex + 1, counter - 1));
                }

                counter++;
            }

            return(null);
        }
Ejemplo n.º 4
0
        // Calculate statement based on specified operations
        // operations = operations to be processed
        private string Calculate(string operations)
        {
            // process statement if specified operators exist
            // if found, read the left and right numbers and calculate
            // recurse until no specified operators are left and return answer
            if (StatementChecker.OperatorCount(statement, operations) != 0)
            {
                for (int x = 0; x < statement.Length - 1; x++)
                {
                    if (operations.Contains(statement[x]) &&
                        !StatementChecker.IsUnaryOperator(statement[x], statement, x))
                    {
                        statement = CalculateLeftandRight(statement, statement[x], x);
                        statement = Calculate(operations);
                        break;
                    }
                }
            }

            return(statement);
        }