Построение графика функции и поиск корней. Кубическое уравнение - C#

Узнай цену своей работы

Формулировка задачи:

Задали курсач,нажно написать математическую программу "Построение графика функции и поиск корней. Кубическое уравнение" помогите плиз,заранее спасибо)

Решение задачи: «Построение графика функции и поиск корней. Кубическое уравнение»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CubicEquation
  8. {
  9.     public class Complex : IComparable<Complex>, IFormattable
  10.     {
  11.         public double Real { get; set; }
  12.         public double Imaginary { get; set; }
  13.  
  14.         public Complex()
  15.         {
  16.  
  17.         }
  18.  
  19.         public Complex(double real)
  20.         {
  21.             Real = real;
  22.             Imaginary = 0;
  23.         }
  24.  
  25.         public Complex(double real, double imaginary)
  26.         {
  27.             Real = real;
  28.             Imaginary = imaginary;
  29.         }
  30.  
  31.         public static Complex operator +(Complex a, Complex b)
  32.         {
  33.             return new Complex()
  34.             {
  35.                 Imaginary = a.Imaginary + b.Imaginary,
  36.                 Real = a.Real + b.Real
  37.             };
  38.         }
  39.  
  40.         public static Complex operator -(Complex a, Complex b)
  41.         {
  42.             return new Complex()
  43.             {
  44.                 Imaginary = a.Imaginary - b.Imaginary,
  45.                 Real = a.Real - b.Real
  46.             };
  47.         }
  48.  
  49.         public static Complex operator +(Complex a, double k)
  50.         {
  51.             return new Complex()
  52.             {
  53.                 Imaginary = a.Imaginary,
  54.                 Real = a.Real + k
  55.             };
  56.         }
  57.  
  58.         public static Complex operator -(Complex a, double k)
  59.         {
  60.             return new Complex()
  61.             {
  62.                 Imaginary = a.Imaginary,
  63.                 Real = a.Real - k
  64.             };
  65.         }
  66.  
  67.         // Перегрузка оператора равенства. ВАЖНО, т.к. сравниваются числа с плавающей запятой, то сравнивать нужно их разницу по модулю
  68.         //
  69.         public static bool operator ==(Complex a, Complex b)
  70.         {
  71.             if ((Math.Abs(a.Imaginary - b.Imaginary) < 0.0000001 && Math.Abs(a.Real - b.Real) < 0.0000001))
  72.             {
  73.                 return true;
  74.             }
  75.             return false;
  76.         }
  77.  
  78.         public static bool operator !=(Complex a, Complex b)
  79.         {
  80.             if (Math.Abs(a.Imaginary - b.Imaginary) > 0.0000001 && Math.Abs(a.Real - b.Real) > 0.0000001)
  81.             {
  82.                 return true;
  83.             }
  84.             return false;
  85.         }
  86.  
  87.  
  88.         public override bool Equals(System.Object obj)
  89.         {
  90.             if (obj == null)
  91.             {
  92.                 return false;
  93.             }
  94.  
  95.             Complex n = obj as Complex;
  96.             if ((System.Object)n == null)
  97.             {
  98.                 return false;
  99.             }
  100.  
  101.             return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
  102.         }
  103.  
  104.         public bool Equals(Complex n)
  105.         {
  106.             if ((object)n == null)
  107.             {
  108.                 return false;
  109.             }
  110.  
  111.             return (Math.Abs(Imaginary - n.Imaginary) < 0.0000001) && (Math.Abs(Real - n.Real) < 0.0000001);
  112.         }
  113.  
  114.         // Указаний по реализации нет. Реализован таким образом
  115.         public override int GetHashCode()
  116.         {
  117.             return (int)Imaginary ^ (int)Real;
  118.         }
  119.  
  120.         public override string ToString()
  121.         {
  122.             if (Imaginary == 0) return string.Format("{0}", Math.Round(Real,6));
  123.             return string.Format("{0} + {1}i", Math.Round(Real,6), Math.Round(Imaginary,6));
  124.         }
  125.  
  126.  
  127.         public string ToString(string format, IFormatProvider formatProvider)
  128.         {
  129.  
  130.             switch (format.ToUpperInvariant())
  131.             {
  132.                 case "P":
  133.                     return string.Format("({0}, {1})", Real, Imaginary);
  134.                 case "A":
  135.                     return string.Format("({0} + i{1})", Real, Imaginary);
  136.                 default:
  137.                     throw new FormatException(String.Format("Формат строки: {0} - не поддерживается", format));
  138.             }
  139.         }
  140.  
  141.         // Возможно лучшее решение. Т.к. указаний не дано по сравнению комплексных чисел (только равенства и неравенство),
  142.         // то реализовано таким образом
  143.         public int CompareTo(Complex other)
  144.         {
  145.             if (this == other) return 0;
  146.  
  147.             if (Math.Abs(this.Real - other.Real) < 0.0000001)
  148.             {
  149.                 if (this.Imaginary - other.Imaginary > 0) return 1;
  150.                 else return -1;
  151.             }
  152.  
  153.             if (this.Real > other.Real) return 1;
  154.             return -1;
  155.         }
  156.  
  157.         // Задания по перегрузки операторов умножения и деления также нет, но вот они на всякий пожарный.
  158.  
  159.         public static Complex operator *(Complex a, Complex b)
  160.         {
  161.             return new Complex()
  162.             {
  163.                 Real = a.Real * b.Real - a.Imaginary * b.Imaginary,
  164.                 Imaginary = a.Real * b.Imaginary + a.Imaginary * b.Real
  165.             };
  166.         }
  167.  
  168.         public static Complex operator *(Complex a, double d)
  169.         {
  170.             return new Complex()
  171.             {
  172.                 Real = a.Real * d,
  173.                 Imaginary = a.Imaginary * d
  174.             };
  175.         }
  176.  
  177.         public static Complex operator /(Complex a, Complex b)
  178.         {
  179.             return new Complex()
  180.             {
  181.                 Real = (a.Real * b.Real + a.Imaginary * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary),
  182.                 Imaginary = (a.Imaginary * b.Real - a.Real * b.Imaginary) / (b.Real * b.Real + b.Imaginary * b.Imaginary)
  183.             };
  184.         }
  185.  
  186.         public static Complex operator /(Complex a, double d)
  187.         {
  188.             return new Complex()
  189.             {
  190.                 Real = a.Real / d,
  191.                 Imaginary = a.Imaginary / d
  192.             };
  193.         }
  194.     }
  195. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.75 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы