//Method to simplify calling the parser from main
        public static void StringParse(string s)
        {
            var        node = new PolynomialParser(new PolynomialToSplit(new StringReader(s)));
            Polynomial res  = node.CheckExpression().Calculate();

            Console.WriteLine();
            Console.WriteLine("Result: ");
            int counter = 0;

            for (int i = 0; i < 101; i++)
            {
                if (res.pol_array[i] != 0)
                {
                    if (i == 0)
                    {
                        if (counter == 0)
                        {
                            Console.Write(res.pol_array[i]);
                            counter++;
                        }
                        else
                        {
                            Console.Write(res.pol_array[i]);
                        }
                    }
                    else
                    {
                        //Coef = 1
                        if (res.pol_array[i] == 1)
                        {
                            if (counter == 0)
                            {
                                if (i == 1)
                                {
                                    Console.Write("x");
                                    counter++;
                                }
                                else
                                {
                                    Console.Write($"x^{i}");
                                    counter++;
                                }
                            }
                            else
                            {
                                if (i == 1)
                                {
                                    Console.Write("+x");
                                }
                                else
                                {
                                    Console.Write($"+x^{i}");
                                }
                            }
                        }
                        //Coef = -1
                        else if (res.pol_array[i] == -1)
                        {
                            if (i == 1)
                            {
                                Console.Write("-x");
                            }
                            else
                            {
                                Console.Write($"-x^{i}");
                            }
                        }
                        else
                        {
                            //Coef > 0
                            if (res.pol_array[i] > 0)
                            {
                                if (counter == 0)
                                {
                                    if (i == 1)
                                    {
                                        Console.Write($"{res.pol_array[i]}x");
                                    }
                                    else
                                    {
                                        Console.Write($"{res.pol_array[i]}x^{i}");
                                    }
                                    counter++;
                                }
                                else
                                {
                                    if (i == 1)
                                    {
                                        Console.Write($"+{res.pol_array[i]}x");
                                    }
                                    else
                                    {
                                        Console.Write($"+{res.pol_array[i]}x^{i}");
                                    }
                                }
                            }
                            //Coef < 0
                            else
                            {
                                if (i == 1)
                                {
                                    Console.Write($"{res.pol_array[i]}x");
                                }
                                else
                                {
                                    Console.Write($"{res.pol_array[i]}x^{i}");
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        private static bool MainMenu()
        {
            Console.Clear();
            Console.WriteLine("Choose an option:");
            Console.WriteLine("1) Algebraic expression");
            Console.WriteLine("2) Boolean expression");
            Console.WriteLine("3) Matrices");
            Console.WriteLine("4) Polynomials");
            Console.WriteLine("5) Exit");
            Console.Write("\r\nSelect an option: ");

            switch (Console.ReadLine())
            {
            case "1":
                Console.WriteLine("Enter the expression like in the example. Trigonometric functions, constants 'pi' and 'e', variables are allowed.");
                Console.WriteLine("Example: (1.5 + sin(2.0 + cos(3.5)))*3");
                Console.WriteLine();
                string s = Console.ReadLine();
                MathParser.StringParse(s);
                Console.WriteLine();
                Console.WriteLine("If you want to continue, press enter. Press 0 to exit.");
                string exit = Console.ReadLine();
                if (exit == "0")
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "2":
                Console.WriteLine("Enter the expression like in the example. Constants '1' and '0' are allowed.");
                Console.WriteLine("Example: (0 \\/ ((1))) /\\ 1");
                Console.WriteLine();
                string s1 = Console.ReadLine();
                BooleanParser.StringParse(s1);
                Console.WriteLine();
                Console.WriteLine("If you want to continue, press enter. Press 0 to exit.");
                string exit1 = Console.ReadLine();
                if (exit1 == "0")
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "3":
                Console.WriteLine("Enter the expression like in the example. Inversion (inv()) is allowed.");
                Console.WriteLine("Example: [ 1 2 ; 3 4 ] + ([ 5 6 ; 7 8] + [0 0 ; 0 0])");
                Console.WriteLine();
                string s2 = Console.ReadLine();
                MatrParser.StringParse(s2);
                Console.WriteLine();
                Console.WriteLine("If you want to continue, press enter. Press 0 to exit.");
                string exit2 = Console.ReadLine();
                if (exit2 == "0")
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "4":
                Console.WriteLine("Enter the expression like in the example. Only the powers < 100 allowed.");
                Console.WriteLine("Example: x^100 + (5 + x^1) * x^3");
                Console.WriteLine();
                string s3 = Console.ReadLine();
                PolynomialParser.StringParse(s3);
                Console.WriteLine();
                Console.WriteLine("If you want to continue, press enter. Press 0 to exit.");
                string exit3 = Console.ReadLine();
                if (exit3 == "0")
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            default:
                return(false);
            }
        }