Beispiel #1
0
        public void CalculateCorrectExpressionTest(string currentExpression, double expected)
        {
            var expression = Initialize(currentExpression);
            var result     = PostfixCalculator.Calculate(expression);

            Assert.IsTrue(CompareDouble(expected, result));
        }
        public void Test()
        {
            // 5 + 6 * 7 - 1
            var tokens = new[] { "5", "6", "7", "*", "+", "1", "-" };

            Assert.AreEqual(46, PostfixCalculator.Calculate(tokens));
        }
        public static string Solve(string question)
        {
            var postfixExpression = PostfixBuilder.BuildPostfixExpression(question);
            var answer            = PostfixCalculator.Calculate(postfixExpression).ToString();

            return(answer);
        }
        /// Simple enough that its easy
        #endregion Stack Linked List Static Cases Functions


        #region Postfix Cal functions
        private static int CalculatePostfix()
        {
            var expr    = "52+";
            var postfix = new PostfixCalculator();
            var output  = postfix.CalculatePostfix(expr);

            return(output);
        }
        public CalculationProcessTests()
        {
            var pluginReader   = new OperationPluginReader();
            var operationsList = pluginReader.ReadPluginsFrom(Environment.CurrentDirectory + "\\Plugins");
            var recognizer     = new BaseRecognizer(operationsList);

            calculator = new PostfixCalculator(recognizer);
        }
        static void Main(string[] args)
        {
            PostfixCalculator calculator = new PostfixCalculator();
            string            command    = Console.ReadLine();

            string[] values = command.Split(' ');
            Console.WriteLine(calculator.Calculate(values));
            Console.ReadLine();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singly Linked List");
            SinglyLinkedList myList1 = new SinglyLinkedList();

            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);

            myList1.AddToFront(n1);
            myList1.AddToEnd(n2);
            myList1.AddToFront(n3);
            myList1.AddToEnd(n4);
            myList1.Print();
            myList1.RemoveNode(myList1.Head, n2.Value);
            myList1.Print();
            myList1.RemoveFirst();
            myList1.Print();
            myList1.RemoveLast();
            myList1.Print();
            myList1.RemoveLast();
            myList1.Print();

            Console.WriteLine("Doubly Linked List");
            DoublyLinkedList myList2 = new DoublyLinkedList();


            myList2.AddToFront(n1);
            myList2.AddToEnd(n2);
            myList2.AddToFront(n3);
            myList2.AddToEnd(n4);
            myList2.Print();
            myList2.RemoveNode(myList2.Head, n2.Value);
            myList2.Print();
            myList2.RemoveFirst();
            myList2.Print();
            myList2.RemoveLast();
            myList2.Print();
            myList2.RemoveLast();
            myList2.Print();
            myList2.RemoveFirst();
            myList2.Print();


            //Post Fix Calculator

            string[] expression = new string[] {
                "5", "6", "7", "*", "+", "1", "-"
            };

            Console.WriteLine("Postfix calculator");
            PostfixCalculator.Print(expression);
            Console.WriteLine("Result: " + PostfixCalculator.Calculate(expression));
        }
        static void Main()
        {
            // var pluginReader = new OperationPluginReader();
            List <IOperation> operations;

            try
            {
                operations = GetOperations();
                // operations = pluginReader.ReadPluginsFrom(Environment.CurrentDirectory + "\\Plugins");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("Ошибка! Отсутсвует директория Plugins");
                Console.WriteLine("Нажмите любую клавишу, чтобы выйти...");
                Console.ReadKey();
                return;
            }
            var recognizer = new BaseRecognizer();
            var calculator = new PostfixCalculator(recognizer.CraeteOperations(operations));

            ShowAvailableOperations(calculator.GetAvailableOperations());
            var reader = new ConsoleInputReader();

            while (true)
            {
                try
                {
                    var result = calculator.Calculate(reader.GetExpression());
                    Console.WriteLine($"Результат: {result}");
                }
                catch (UnrecognizedOperationException e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine("Нажмите Enter, чтобы продолжить или Esc, чтобы выйти");
                if (Console.ReadKey().Key == ConsoleKey.Escape)
                {
                    return;
                }
            }
        }
Beispiel #9
0
 public void SetUp()
 {
     _calculator = new PostfixCalculator();
 }
Beispiel #10
0
        public static void Main(string[] args)
        {
            System.Console.WriteLine("LinkList Sample");

            SinglyLinkedList <int> primeNumbers = new SinglyLinkedList <int>();

            // first we'll add the middle element. Then try the AddToFront with 3 and then try the AddToBack with 7
            // generating the final sequence as 3, 5, 7

            primeNumbers.AddToFront(5);
            primeNumbers.AddToFront(3);
            primeNumbers.AddToBack(7);
            primeNumbers.AddToBack(9);
            primeNumbers.AddToBack(8);
            primeNumbers.AddToBack(7);
            primeNumbers.Remove(8);
            primeNumbers.RemoveLast();

            PrintHelpers.PrintFromNode(primeNumbers.Head);


            System.Console.WriteLine("Stack Sample: ");

            PostfixCalculator postfixCalculator = new PostfixCalculator();
            int result = postfixCalculator.calculate("5 6 7 * + 1 -");

            System.Console.WriteLine($"The postfix expression result is: {result}");


            System.Console.WriteLine("Binary Search Tree Sample:");

            BinaryTree <int> binaryTree = new BinaryTree <int>();

            binaryTree.Add(3);
            binaryTree.Add(7);
            binaryTree.Add(5);
            binaryTree.Add(4);
            binaryTree.Add(6);
            binaryTree.Add(8);
            binaryTree.Add(11);
            binaryTree.Add(1);

            System.Console.WriteLine($"Contains 5: {binaryTree.Contains(5)}");
            System.Console.WriteLine($"Contains 6: {binaryTree.Contains(6)}");
            System.Console.WriteLine($"Contains 1: {binaryTree.Contains(1)}");

            System.Console.WriteLine($"Remove 9 op result: {binaryTree.Remove(9)}");
            System.Console.WriteLine($"Remove 7 op result: {binaryTree.Remove(7)}");

            System.Console.WriteLine("Traversal Outputs:");

            Traversals <int> traversals = new Traversals <int>();

            IReadOnlyCollection <int> preOrder  = traversals.PreorderTraversal(binaryTree);
            IReadOnlyCollection <int> inOrder   = traversals.InorderTraversal(binaryTree);
            IReadOnlyCollection <int> postOrder = traversals.PostorderTraversal(binaryTree);

            System.Console.WriteLine($"Postorder: {string.Join(", ", preOrder)}");
            System.Console.WriteLine($"Inorder: {string.Join(", ", inOrder)}");
            System.Console.WriteLine($"Postorder: {string.Join(", ", postOrder)}");
        }
 public void Initialize()
 {
     calculator = new PostfixCalculator(new MyStackOnArray());
 }
Beispiel #12
0
        public void CalculationProcessTestsSetUp()
        {
            creator = new PostfixCalculatorCreator(new BaseRecognizer(), WayToGetOperation.Internal);

            calculator = creator.CreateCalculator();
        }
Beispiel #13
0
 public Expression()
 {
     parser            = new ExpressionParser();
     postfixConverter  = new InfixToPostfixConverter();
     postfixCalculator = new PostfixCalculator();
 }
Beispiel #14
0
        public double Calculate(string expression)
        {
            PostfixCalculator postfixCalculator = new PostfixCalculator();

            return(postfixCalculator.Calculate(expression));
        }
        static void Main(string[] args)
        {
            string[] strings =
            {
                "(){}",
                ")"
            };

            foreach (var s in strings)
            {
                string results = BracketDelimiter.IsBalanced(s) ? "succes" : "niet oké";
                Debug.WriteLine(results);
            }

            var resultTot = PostfixCalculator.Calculate("12 10-2+25*10/");

            Console.WriteLine(resultTot);

            //BracketDelimiter.IsBalanced("(){}");

            //BracketDelimiter.IsBalanced("()");
            //BracketDelimiter.IsBalanced("((");

            //BracketDelimiter.IsBalanced("{()}");
            //BracketDelimiter.IsBalanced("{()");
            //BracketDelimiter.IsBalanced("''");

            ////var resultS =  BracketDelimiter.IsBalanced("<?<??>?>");
            //var resultSS = BracketDelimiter.IsBalanced("for(int i = 0; i< 100; i++){}");

            //Console.WriteLine(resultSS);
            Console.ReadLine();
            return;


            OddNumberPrinter.ExecuteEven(2);
            OddNumberPrinter.ExecuteOdd(3);



            var result = InterestCalculator.Calculate(2, 1, 5000);

            List <string> items = new List <string> {
                "a", "b", "c", "d", "e", "f", "g"
            };

            Permutation.Permutations(items);
            Console.WriteLine(Permutation.GetPermutationCount);

            //Printer.PrintAsc();
            //Console.WriteLine(NaturalCounter.Execute(5));
            //DigitSeperator.Execute(255);
            //Console.WriteLine(DigitCounter.Execute(12345));
            //OddNumberPrinter.Execute(20);
            ItterativeIndex ittIdIndex = new ItterativeIndex();

            int value = ittIdIndex.IndexOf(new int[] { 1, 2, 3, 4, 5 }, 5, 0);


            IsPrime.Execute(37, 37 / 2);
            IsPalindrome.Execute("ABBA");
            Console.WriteLine(
                FactorialCalculator.Execute(8));
            //ForLoopProblem.Problem();

            char startPeg   = 'A';  // start tower in output
            char endPeg     = 'C';  // end tower in output
            char tempPeg    = 'B';  // temporary tower in output
            int  totalDisks = 1200; // number of disks

            //TowersOfHanoi.Solve(totalDisks, startPeg, endPeg, tempPeg);


            int[,] random = new int[, ]
            {
                { 200, 400 },
                { 2000, 4176 },
                { 20000, 40000 },
                { 50000, 50000 }
            };

            //var c = _2DArraySum.GetHighestSum(random);

            var getHighestSummedArray = new _2DArraySumAsClass <int[, ]>();

            Console.WriteLine("Highest value" + getHighestSummedArray.GetHighestSum(random));


            //Create linked list
            LinkedListExercise linkedListExercise = new LinkedListExercise();

            //Add data
            linkedListExercise.Employees.AddLast(new Employee("Bob", 5));
            linkedListExercise.Employees.AddLast(new Employee("Alice", 5000));

            //ShallowCopy with IClonable
            var shallowCopy = linkedListExercise.ShallowCopy();
            //Shallow copy with Collection<T>() ctor
            var shallowCopyCollection = linkedListExercise.ShallowCopyCollection();
            //Deep copy
            var deepCopy = linkedListExercise.DeepCopy();


            //var bucketSort = new BucketSort();
            //bucketSort.Execute(new[] { 8, 2, 122, 1, 99, 3, 4, 2 });

            //BubbleSort bubbleSort = new BubbleSort();
            //var res = bubbleSort.Execute(new int[]{8,2, 122, 1});

            //var x = new Solution3();
            //x.Slow(100);
            //x.Fast(100);

            //var binarySearch = new BinarySearch();
            //var array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            //int pos = binarySearch.Execute(array, 10);

            //var input = "8,1,6;3,5,7;4,9,2";
            //var array = (from x in input.Split(',', ';') select int.Parse(x)).ToArray();
            //var magic = new MagicSquare();
            //magic.Execute(3);

            //magic.ExecuteCorrect(3);
            //MagicSquare.RunMagicSquare(3);


            //Binary Search recurisve.
            //int[] testArray = { 1,2,3,4,5,6,7,8,9,10,11,12};
            //var length = testArray.Length;
            //var searchFor = 10;

            //Console.WriteLine(BinarySearch.RecursiveV2(searchFor, testArray, 0, length));
            //Console.WriteLine($"Self made {BinarySearch.BinarySearchRecursiveSelf(testArray, 0, length, searchFor)}");


            //Console.WriteLine(
            //    ADS.Core.Lesson_2.Converter.DecimalToBin(10));

            //Console.WriteLine("Bin 2 dec " + ADS.Core.Lesson_2.Converter.Bin2dec(1100));
            //Console.WriteLine(ADS.Core.Lesson_2.Converter.BinToDecimal("10101"));


            //Console.WriteLine(Palindrome.IsPalindrome("racecar"));

            // Console.WriteLine(ReverseString.ExecuteV1("ban"));
            // Console.WriteLine(ReverseString.Execute("ban"));

            // ArrayCombiner arrayCombiner = new ArrayCombiner();
            //var x = arrayCombiner.Execute(new int[] { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 },
            //     new int[] { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });

            //ArrayListCombiner arrayListCombiner = new ArrayListCombiner();

            // var y = arrayListCombiner.Merge(new List<int> { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 },
            //    new List<int> { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 });


            // Create first polynomial
            var polynomial1 = new Polynomial();

            polynomial1.Add(new Term(1, 2));
            polynomial1.Add(new Term(2, 2));
            polynomial1.Add(new Term(3, 0));


            // Create second polynomial
            var polynomial2 = new Polynomial();

            polynomial2.Add(new Term(-1, 3));
            polynomial2.Add(new Term(1, 1));
            polynomial2.Add(new Term(1, 2));

            Stopwatch x = new Stopwatch();

            x.Start();
            Polynomial polyX        = polynomial1.SumWorking(polynomial2);
            var        termsWorking = polyX.GetAllTerms();

            x.Stop();
            Console.WriteLine(x.ElapsedTicks + "TICKS");
            x.Reset();

            x.Start();

            Polynomial polyOptimized = polynomial1.OptimizedAlgorithm(polynomial2);
            var        optimzedTerms = polyX.GetAllTerms();

            x.Stop();
            Console.WriteLine(x.ElapsedTicks + "TICKS");
            x.Reset();

            // Determine the sum
            Polynomial polynomialSum = polynomial1.Sum(polynomial2);
            var        terms         = polynomialSum.GetAllTerms();

            for (int i = 0; i < polynomialSum.GetAllTerms().Count; i++)
            {
                Console.Write($"{terms[i].toString()}\t");
            }

            //TowersOfHanoi.Solve(64);
            //TowersOfHanoi.TowerHanoi(3);
            //-> coeff = 1 - X^ > ex3
            //2X^3
            //-5^0

            //var c = BinarySearch.binarySearchFloris(new int[] { 1,2,3,4,5,6,7,8,9,10 }, 2);

            Hangman hangman = new Hangman();

            hangman.ChooseWord();

            bool val = true;

            while (val)
            {
                string input = Console.ReadLine();
                hangman.GuessWord(input);

                if (input == "exit")
                {
                    val = false;
                }
            }

            int[] arr1 = new int[] { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
            int[] arr2 = new int[] { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 };

            int[] execute = ArrayCombiner.combineArray(arr2, arr1);

            //var intList = new ArrayList();
            //intList[1] as int? = 2;

            var res12 = ArrayListCombiner.Merge(new ArrayList()
            {
                1, 2, 3, 4
            }, new ArrayList()
            {
                1, 2, 3, 4
            });

            int max = MaxRec.Execute(new int[] { 1, 2, 3, 4, 1204 });


            Console.WriteLine();

            //Console.WriteLine(Converter.BinToDecimal("1011"));
            Console.ReadLine();
        }
 public string T01_CanCalculateEmpty(string postfixExpression)
 => PostfixCalculator.Calculate(postfixExpression);
Beispiel #17
0
        public void NotAPostfixFormTest(string currentExpression)
        {
            var expression = Initialize(currentExpression);

            Assert.Throws <ArgumentException>(() => PostfixCalculator.Calculate(expression));
        }
Beispiel #18
0
        public void DivisionByZeroTest(string currentExpression)
        {
            var expression = Initialize(currentExpression);

            Assert.Throws <DivideByZeroException>(() => PostfixCalculator.Calculate(expression));
        }
 public PosfixCalculatorService(IRecognizer recognizer)
 {
     this.recognizer = recognizer;
     creator         = new PostfixCalculatorCreator(recognizer, WayToGetOperation.Internal);
     calculator      = creator.CreateCalculator();
 }