示例#1
0
        public void CalculateTest()
        {
            var calculator = new RootExtraction();
            var actual     = calculator.CalculateTwo(65536);

            Assert.AreEqual(256, actual, 0.25);
            Assert.Throws <Exception>(code: () => calculator.CalculateTwo(-4));
        }
        public void TheSquareRootOfZero()
        {
            Number root     = new Number(0);
            int    expected = 0;

            RootExtraction rootExtraction = new RootExtraction(root);
            double         actual         = rootExtraction.Operation();

            Assert.AreEqual(expected, actual);
        }
        public void TheSquareRootOfANegativeRealNumber()
        {
            Number root     = new Number(-9.5);
            double expected = double.NaN;

            RootExtraction rootExtraction = new RootExtraction(root);
            double         actual         = rootExtraction.Operation();

            Assert.AreEqual(expected, actual);
        }
        public void TheSquareRootOfAPositiveRealNumber()
        {
            Number root     = new Number(9.5);
            double expected = 3.082207001484488;

            RootExtraction rootExtraction = new RootExtraction(root);
            double         actual         = rootExtraction.Operation();

            Assert.AreEqual(expected, actual);
        }
        public void TheSquareRootOfAPositiveInteger()
        {
            Number root     = new Number(9);
            int    expected = 3;

            RootExtraction rootExtraction = new RootExtraction(root);
            double         actual         = rootExtraction.Operation();

            Assert.AreEqual(expected, actual);
        }
示例#6
0
        /// <summary>
        /// <para>
        /// To determine the sequence of actions: execution priority is high
        /// </para>
        /// <para>
        /// High priority operations: negation, parentheses, number
        /// </para>
        /// </summary>
        private UniversalOperation ParsingAnExpression_HighPriority()
        {
            UniversalOperation result;

            if (MatchSearch('-'))
            {
                result = new Negation(ParsingAnExpression_LowPriority());
            }
            else if (MatchSearch('√'))
            {
                result = new RootExtraction(ParsingAnExpression_HighPriority());
            }
            else if (MatchSearch('S') && MatchSearch('q') && MatchSearch('r'))  //Workaround
            {
                result = new Exponentiation(ParsingAnExpression_HighPriority(), new Number(2));
            }
            else if (MatchSearch('('))
            {
                result = ParsingAnExpression_LowPriority();

                if (!MatchSearch(')'))
                {
                    System.Console.WriteLine("Missing ')'");
                }
            }
            else
            {
                //Parsing numbers
                double val           = 0.0;
                int    startPosition = pos;

                //Find out the size of the number to parse
                while (pos < currentExpression.Length && (char.IsDigit(currentExpression[pos]) || currentExpression[pos] == ','))
                {
                    pos++;
                }

                //Attempt to parse a number
                try
                {
                    val = double.Parse(currentExpression.Substring(startPosition, pos - startPosition));
                }
                catch (System.Exception e)
                {
                    System.Console.WriteLine("The number is not parsed...");
                    System.Console.WriteLine(e);
                }

                result = new Number(val);
            }

            return(result);
        }