public double FindSolution(Equation eq, double eps)
        {
            Console.WriteLine("Binsearch:\n");
            Console.WriteLine($"Eps: {eps}");
            double a = eq.start, b = eq.end;

            Console.WriteLine($"Start: {a}\nEnd: {b}\n");

            double x      = (a + b) / 2;
            double fvalue = eq.function(x);

            Console.WriteLine("Initial approximation: " + String.Format("{0:f6}", x) +
                              " Function value: " + String.Format("{0:f6}", fvalue));
            int i = 1;

            while (Math.Abs(fvalue) - eps > 0)
            {
                if (fvalue > 0)
                {
                    b = x;
                }
                else
                {
                    a = x;
                }
                x = (a + b) / 2;
                // fvalue == eq.function(x) - це зроблено для уникання багатьох перераховувань функції
                fvalue = eq.function(x);
                Console.WriteLine($"Iteration {i}: " + String.Format("{0:f6}", x) +
                                  " Function value: " + String.Format("{0:f6}", fvalue));
                i++;
            }
            Console.WriteLine("Result: " + String.Format("{0:f6}", x) +
                              " Function value: " + String.Format("{0:f6}", fvalue));
            return(x);
        }
예제 #2
0
        static void Main(string[] args)
        {
            double answer;
            string yayornay = "";

            do
            {
                Console.WriteLine("Please enter a left value");
                double lh = Convert.ToDouble(Console.ReadLine());


                Console.WriteLine("Please enter a right value");
                double rh = Convert.ToDouble(Console.ReadLine());

                Console.WriteLine("Which calculation would you like to perform?(Add, Subtract, Multiply, Power)");
                string userInput = Console.ReadLine();

                //double left= eq1.Left;
                //double right = eq1.Right;

                List <Equation> equations = new List <Equation>();
                Equation        eq1       = new Equation(lh, rh);

                if (userInput == "add")
                {
                    answer = eq1.Add();
                }
                else if (userInput == "subtract")
                {
                    answer = eq1.Subtract();
                }
                else if (userInput == "multiply")
                {
                    answer = eq1.Multiply();
                }
                else if (userInput == "power")
                {
                    Console.WriteLine("What would you like to raise the power to?");
                    int toThePowerOf = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine("Left or Right");
                    string leftOrRight = Console.ReadLine().ToLower();

                    if (leftOrRight == "left")
                    {
                        answer = eq1.LeftToThePower(toThePowerOf);
                        //double power = Equations.LeftToThePower();
                    }
                    else if (leftOrRight == "right")
                    {
                        answer = eq1.RightToThePower(toThePowerOf);
                        //Console.WriteLine(Equation.RightToThePower());
                    }
                }
                else
                {
                    Console.WriteLine("You suck");
                }
                equations.Add($"{lh}{rh}+{answer}");



                Console.WriteLine("would you like to perform again?(Yes or No)");
                yayornay = Console.ReadLine().ToLower();

                Console.ReadKey();
            } while (yayornay == "yes" || yayornay == "y");
        }