Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- delegate double MyFunc(double x);
- delegate double MyMethod(double f);
- class Test
- {
- public static double f1(double x)
- {
- return x;
- }
- public static double f2(double x)
- {
- return x * x;
- }
- public static double f3(double x)
- {
- return (double) Math.Sqrt(x);
- }
- static double divCut(MyFunc f, int t)
- {
- //метод деления отрезка попалам
- double a, b, e;
- a = double.Parse(Console.ReadLine());
- b = double.Parse(Console.ReadLine());
- e = double.Parse(Console.ReadLine());
- double length = b - a;
- double middle = (b - a) / 2;
- switch (t)
- {
- case 1:
- if (f(a) == 0)
- {
- return a;
- }
- else if (f(b) == 0)
- {
- return b;
- }
- else
- {
- while (length / 2 > e)
- {
- length /= 2;
- if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
- {
- a += length;
- }
- else
- {
- b -= length;
- }
- }
- return a + length;
- }
- case 2:
- if (f(a) == 0)
- {
- return a;
- }
- else if (f(b) == 0)
- {
- return b;
- }
- else
- {
- while (length / 2 > e)
- {
- length /= 2;
- if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
- {
- a += length;
- }
- else
- {
- b -= length;
- }
- }
- return a + length;
- }
- case 3:
- if (f(a) == 0)
- {
- return a;
- }
- else if (f(b) == 0)
- {
- return b;
- }
- else
- {
- while (length / 2 > e)
- {
- length /= 2;
- if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
- {
- a += length;
- }
- else
- {
- b -= length;
- }
- }
- return a + length;
- }
- }
- return a + length;
- }
- static double hord()
- {
- return 1;
- }
- static double kas()
- {
- return 1;
- }
- static void Main()
- {
- MyFunc f = new MyFunc(f1);
- int a;
- Console.WriteLine("1 - x, 2 - x^2, 3 - sqrt(x)");
- a = int.Parse(Console.ReadLine());
- switch (a)
- {
- case 1:
- MyMethod g = new MyMethod(f1);
- break;
- case 2:
- g = new MyMethod(f2);
- break;
- case 3:
- g = new MyMethod(f3);
- break;
- }
- double x = 4;
- Console.WriteLine("f({0}) = {1}", x, g(x));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement