Пример #1
0
        static void Main(string[] args)
        {
            var parser     = new InputParser();
            var checker    = new InputChecker();
            var converter  = new RpnConverter(checker);
            var counter    = new RpnCounter();
            var calculator = new RpnCalculator(parser, converter, counter);

            Console.WriteLine("CONSOLE CALCULATOR");
            Console.WriteLine("You can enter numbers and symbols + - * / ^ ( ).");
            Console.WriteLine("Press enter key to calculate.");
            Console.WriteLine("Enter 'q' key to exit.");

            while (true)
            {
                Console.Write("Enter expression > ");

                string input = Console.ReadLine();
                if ("q".Equals(input, StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                try
                {
                    double result = calculator.Calculate(input);
                    Console.WriteLine($"Result is {result}");
                }
                catch (Exception)
                {
                    Console.WriteLine("Incorrect input");
                }
            }
        }
 public WorkItem(ManualResetEvent sync, Int32 from, Int32 to, RpnCalculator calculator, ExampleCallback callback)
 {
     m_Sync = sync;
     m_From = from;
     m_To = to;
     m_Calculator = calculator;
     m_Callback = callback;
 }
 public WorkItem(ManualResetEvent sync, Int32 from, Int32 to, RpnCalculator calculator, double[] results, Int32 index)
 {
     m_Sync = sync;
     m_From = from;
     m_To = to;
     m_Calculator = calculator;
     m_Results = results;
     m_Index = index;
 }
Пример #4
0
        public void Should_Not_Compute_When_Input_Is_1_Divided_By_0()
        {
            //Arrange
            var rpnCalculator = new RpnCalculator();

            //Act
            //Assert
            Assert.Throws <ArgumentException>(() => rpnCalculator.Compute("1 0 /"));
        }
Пример #5
0
        public void Should_Return_10_When_Input_Is_10()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 10;

            //Act
            var actual = rpnCalculator.Compute("10");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #6
0
        public void Should_Add_When_Input_Is_1_Plus_0()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 1;

            //Act
            var actual = rpnCalculator.Compute("1 0 +");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #7
0
        public void Should_Combine_Nested_Computations()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 14;

            //Act
            var actual = rpnCalculator.Compute("5 1 2 + 4 * + 3 -");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #8
0
        public void Should_Substract_And_Divide_When_5_Minus_1_Divided_By_4()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 1;

            //Act
            var actual = rpnCalculator.Compute("5 1 - 4 /");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #9
0
        public void Should_Add_And_Multiply_When_Input_Is_4_Plus_1_Multiply_2()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 10;

            //Act
            var actual = rpnCalculator.Compute("2 4 1 + *");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #10
0
        public void Should_Divide_When_Input_Is_9_Divided_By_3()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 3;

            //Act
            var actual = rpnCalculator.Compute("9 3 /");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #11
0
        public void Should_Multiply_When_Input_Is_2_Multiply_4()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 8;

            //Act
            var actual = rpnCalculator.Compute("2 4 *");

            //Assert
            Assert.AreEqual(expected, actual);
        }
Пример #12
0
        public void Should_Substract_When_Input_Is_6_Minus_1()
        {
            //Arrange
            var       rpnCalculator = new RpnCalculator();
            const int expected      = 5;

            //Act
            var actual = rpnCalculator.Compute("6 1 -");

            //Assert
            Assert.AreEqual(expected, actual);
        }
        static void Main(string[] args)
        {
            do
            {
                Console.Write(">> ");
                string input = Console.ReadLine()?.Trim();

                if (string.IsNullOrEmpty(input))
                {
                    break;
                }

                try
                {
                    Console.WriteLine(" = " + RpnCalculator.Calculate(input));
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                }
            } while (true);
        }
Пример #14
0
 public RpnCalculatorTests()
 {
     // This constructor is called before every test is executed
     calc = new RpnCalculator();
 }
Пример #15
0
        private void CalculateSeries()
        {
            ValueExtractor ve;
            RrdDb rrd;
            string[] varList;
            long finalEndTime		= 0;
            bool changingEndTime	= false;

            long startTime 			= graphDef.StartTime;
            long endTime			= graphDef.EndTime;
            changingEndTime			= (endTime == 0);

            int numDefs				= graphDef.NumDefs;

            Cdef[] cdefList			= graphDef.Cdefs;
            int numCdefs			= cdefList.Length;

            Pdef[] pdefList			= graphDef.Pdefs;
            int numPdefs			= pdefList.Length;

            // Set up the array with all datasources (both Def and Cdef)
            sources 				= new Source[ numDefs + numCdefs + numPdefs ];
            sourceIndex 			= new Hashtable( numDefs + numCdefs + numPdefs );
            int tblPos				= 0;
            int vePos				= 0;

            ValueExtractor[] veList		= new ValueExtractor[ graphDef.FetchSources.Count ];
            ICollection fetchSources	= graphDef.FetchSources.Values;

            foreach (FetchSource fs in fetchSources)
            {
                // Get the rrdDb
                rrd		= rrdGraph.GetRrd( fs.RrdFile );

                // If the endtime is 0, use the last time a database was updated
                if (  changingEndTime )
                {
                    endTime = rrd.LastUpdateTime;
                    endTime -= (endTime % rrd.Header.Step);

                        if ( endTime > finalEndTime)
                            finalEndTime = endTime;
                }

                // Fetch all required datasources
                ve 		= fs.Fetch( rrd, startTime,  endTime );
                varList = ve.Names;

                // BUGFIX: Release the rrdDb
                rrdGraph.ReleaseRrd(rrd);

                for (int i= 0; i < varList.Length; i++)
                {
                    sources[tblPos]	= new Def(varList[i], numPoints);
                    sourceIndex[varList[i]] = tblPos++ ;
                }

                veList[ vePos++ ] = ve;
            }

            // Add all Pdefs to the source table
            for ( int i = 0; i < pdefList.Length; i++ )
            {
                pdefList[i].Prepare( numPoints );

                sources[tblPos] = pdefList[i];
                sourceIndex[pdefList[i].Name] = tblPos++ ;
            }

            // Add all Cdefs to the source table
            // Reparse all RPN datasources to use indices of the correct variables
            for ( int i = 0; i < cdefList.Length; i++ )
            {
                cdefList[i].Prepare( sourceIndex, numPoints );

                sources[tblPos]	= cdefList[i];
                sourceIndex[cdefList[i].Name] =  tblPos++ ;
            }

            // Fill the array for all datasources
            timestamps 				= new long[numPoints];

            if ( changingEndTime )
            {
                endTime				= finalEndTime;
                calculatedEndTime	= endTime;
            }

            // RPN calculator for the Cdefs
            RpnCalculator rpnCalc 	= new RpnCalculator( sources, (endTime - startTime)/ (double) numPoints );

            for (int i = 0; i < numPoints; i++)
            {
                long t 	= (long) (startTime + i * ((endTime - startTime) / (double)(numPoints - 1)));
                int pos = 0;

                // Get all fetched datasources
                for (int j = 0; j < veList.Length; j++)
                    pos = veList[j].Extract( t, sources, i, pos );

                // Get all custom datasources
                for (int j = pos; j < pos + numPdefs; j++)
                    ((Pdef) sources[j]).Set(i,t);
                pos += numPdefs;

                // Get all combined datasources
                for (int j = pos; j < sources.Length; j++)
                    sources[j].Set(i, t, rpnCalc.Evaluate( (Cdef) sources[j], i, t ) );

                timestamps[i] = t;
            }

            // Clean up the fetched datasources forcibly
            veList = null;
        }
Пример #16
0
        public void ExceptionIsThrownWhenTokenSequenceIsCorrupted(string value)
        {
            Action action = () => RpnCalculator.Run(value);

            action.Should().Throw <Exception>();
        }
Пример #17
0
 public void ValueIsCalculatedRight(string value, double expected) => RpnCalculator.Run(value).Should().Be(expected);
Пример #18
0
 public ApiResponse <double> Execute(RpnRequest request) => ApiResponse.Create(RpnCalculator.Run(request.Expression));
Пример #19
0
 public ControlFunctions()
 {
     calc = new RpnCalculator();
 }