Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- namespace MyProgram
- {
- abstract public class Function : IComparable<Function> // абстрактный класс-функция
- {
- public double a;
- abstract public double f(double x);
- abstract public void Print();
- public int CompareTo(Function fun)
- {
- return a.CompareTo(fun.a);
- }
- }
- public class Line : Function
- {
- public double b;
- public Line(double a, double b)
- {
- this.a = a;
- this.b = b;
- }
- public override double f(double x)
- {
- return a * x + b;
- }
- public override void Print()
- {
- Console.WriteLine("Информация о графике");
- Console.WriteLine("Тип - линейная функция");
- Console.WriteLine("Вид: {0} * x + {1}", a, b);
- }
- }
- public class Kub : Function
- {
- public double b, c;
- public Kub(double a, double b, double c)
- {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public override double f(double x)
- {
- return a * x * x + b * x + c;
- }
- public override void Print()
- {
- Console.WriteLine("Информация о графике");
- Console.WriteLine("Тип - квадратичная функция");
- Console.WriteLine("Вид: {0} * x * x + {1} * x + {2}", a, b, c);
- }
- }
- public class Hyperbola : Function
- {
- public double b;
- public Hyperbola(double a, double b)
- {
- this.a = a;
- this.b = b;
- }
- public override double f(double x)
- {
- if (x == 0.0)
- throw new Exception("Функция не определена при нуле (деление на нуль)");
- else
- return a / x + b;
- }
- public override void Print()
- {
- Console.WriteLine("Информация о графике");
- Console.WriteLine("Тип - гиперболическая функция");
- Console.WriteLine("Вид: {0} / x + {1}", a, b);
- }
- }
- class Program
- {
- static void Main()
- {
- using (StreamReader file = new StreamReader("C:/Users/Acer/Desktop/СиАКОД/Задание 18/input.txt"))
- {
- int n = int.Parse(file.ReadLine());
- Function[] functions = new Function[n];
- for (int i = 0; i < n; ++i)
- {
- string[] cur = file.ReadLine().Split(' ');
- if (cur[0] == "Line")
- functions[i] = new Line(double.Parse(cur[1]), double.Parse(cur[2]));
- else if (cur[0] == "Kub")
- functions[i] = new Kub(double.Parse(cur[1]), double.Parse(cur[2]), double.Parse(cur[3]));
- else if (cur[0] == "Hyperbola")
- functions[i] = new Hyperbola(double.Parse(cur[1]), double.Parse(cur[2]));
- else
- throw new Exception("Функция " + cur[0] + " не определена");
- }
- double x = double.Parse(file.ReadLine());
- // Задание 19
- Array.Sort(functions);
- for (int i = 0; i < n; ++i)
- {
- functions[i].Print();
- Console.WriteLine("Значение в точке x = {0}:", x);
- Console.WriteLine("f(x) = {0}\n", functions[i].f(x));
- }
- }
- }
- };
- }
- /* input.txt
- 3
- Line 10 10
- Kub 1 2 1
- Hyperbola 4 0
- 4
- */
Add Comment
Please, Sign In to add comment