Tvor0zhok

СиАКОД 18 и 19

Apr 4th, 2022 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace MyProgram
  7. {
  8.     abstract public class Function : IComparable<Function> // абстрактный класс-функция
  9.     {
  10.         public double a;
  11.  
  12.         abstract public double f(double x);
  13.         abstract public void Print();
  14.  
  15.         public int CompareTo(Function fun)
  16.         {
  17.             return a.CompareTo(fun.a);
  18.         }
  19.     }
  20.  
  21.     public class Line : Function
  22.     {
  23.         public double b;
  24.  
  25.         public Line(double a, double b)
  26.         {
  27.             this.a = a;
  28.             this.b = b;
  29.         }
  30.  
  31.         public override double f(double x)
  32.         {
  33.             return a * x + b;
  34.         }
  35.  
  36.         public override void Print()
  37.         {
  38.             Console.WriteLine("Информация о графике");
  39.             Console.WriteLine("Тип - линейная функция");
  40.             Console.WriteLine("Вид: {0} * x + {1}", a, b);
  41.         }
  42.     }
  43.  
  44.     public class Kub : Function
  45.     {
  46.         public double b, c;
  47.  
  48.         public Kub(double a, double b, double c)
  49.         {
  50.             this.a = a;
  51.             this.b = b;
  52.             this.c = c;
  53.         }
  54.  
  55.         public override double f(double x)
  56.         {
  57.             return a * x * x + b * x + c;
  58.         }
  59.  
  60.         public override void Print()
  61.         {
  62.             Console.WriteLine("Информация о графике");
  63.             Console.WriteLine("Тип - квадратичная функция");
  64.             Console.WriteLine("Вид: {0} * x * x + {1} * x + {2}", a, b, c);
  65.         }
  66.     }
  67.  
  68.     public class Hyperbola : Function
  69.     {
  70.         public double b;
  71.  
  72.         public Hyperbola(double a, double b)
  73.         {
  74.             this.a = a;
  75.             this.b = b;
  76.         }
  77.  
  78.         public override double f(double x)
  79.         {
  80.             if (x == 0.0)
  81.                 throw new Exception("Функция не определена при нуле (деление на нуль)");
  82.             else
  83.                 return a / x + b;
  84.         }
  85.  
  86.         public override void Print()
  87.         {
  88.             Console.WriteLine("Информация о графике");
  89.             Console.WriteLine("Тип - гиперболическая функция");
  90.             Console.WriteLine("Вид: {0} / x + {1}", a, b);
  91.         }
  92.     }
  93.  
  94.     class Program
  95.     {
  96.         static void Main()
  97.         {
  98.             using (StreamReader file = new StreamReader("C:/Users/Acer/Desktop/СиАКОД/Задание 18/input.txt"))
  99.             {
  100.                 int n = int.Parse(file.ReadLine());
  101.  
  102.                 Function[] functions = new Function[n];
  103.  
  104.                 for (int i = 0; i < n; ++i)
  105.                 {
  106.                     string[] cur = file.ReadLine().Split(' ');
  107.  
  108.                     if (cur[0] == "Line")
  109.                         functions[i] = new Line(double.Parse(cur[1]), double.Parse(cur[2]));
  110.                     else if (cur[0] == "Kub")
  111.                         functions[i] = new Kub(double.Parse(cur[1]), double.Parse(cur[2]), double.Parse(cur[3]));
  112.                     else if (cur[0] == "Hyperbola")
  113.                         functions[i] = new Hyperbola(double.Parse(cur[1]), double.Parse(cur[2]));
  114.                     else
  115.                         throw new Exception("Функция " + cur[0] + " не определена");
  116.                 }
  117.  
  118.                 double x = double.Parse(file.ReadLine());
  119.  
  120.                 // Задание 19
  121.                 Array.Sort(functions);
  122.  
  123.                 for (int i = 0; i < n; ++i)
  124.                 {
  125.                     functions[i].Print();
  126.  
  127.                     Console.WriteLine("Значение в точке x = {0}:", x);
  128.                     Console.WriteLine("f(x) = {0}\n", functions[i].f(x));  
  129.                 }
  130.             }
  131.         }
  132.     };
  133. }
  134.  
  135. /* input.txt
  136. 3
  137. Line 10 10
  138. Kub 1 2 1
  139. Hyperbola 4 0
  140. 4
  141. */
Add Comment
Please, Sign In to add comment