Реализация действий над комплексными числами - C#

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

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

не выходит реализовать операции действий над числами
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ComplexNumbers
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. Complex a = new Complex(2, 3);
  12. Complex b = new Complex(4, 5);
  13. Console.WriteLine(a.re.ToString() + " " + a.im.ToString());
  14. Console.Read();
  15. // your code goes here
  16. }
  17. }
  18.  
  19. public class Complex
  20. {
  21. public float re;
  22. public float im;
  23. public Complex(float r, float m)
  24. {
  25. re = r;
  26. im = m;
  27. }
  28. public Complex() //конструктор без параметров
  29. {
  30. re = im = 0;
  31. }
  32. /* public static Complex& operator - (Complex C2);
  33. public static Complex& operator *(Complex C2); //объявляем в классе
  34. public static Complex& operator /(Complex C2);
  35. */
  36. /*virtual void Show() //метод для отображения переменной на экране
  37. {
  38. if (im > 0)
  39. System.Console.WriteLine("(%.2f%.2f\n)", re, im);
  40. else System.Console.WriteLine("(%.2f%.2f\n)", re, im);
  41. }
  42. */
  43. public static Complex operator + (Complex C2) //а здесь в классе Complex -оператор
  44. {
  45. Complex C;
  46. C.re = re + C2.re;
  47. C.im = im + C2.im;
  48. return C; }
  49. public static Complex operator - (Complex C2)
  50. { return new Complex(re - C2.re, im - C2.im);
  51. }
  52. public static Complex operator * (Complex C2) // Complex:: означает,что оператор относиться к классу
  53. {Complex C;
  54. C.Re=(Re*C2.Re-Im*C2.Im);
  55. C.Im=(C2.Re*Im+Re*C2.Im);
  56. return C;
  57. }
  58. public static Complex operator / (Complex C2)
  59. {
  60. Complex C;
  61. C.Re=(Re*C2.Re+Im*C2.Im)/(C2.Re*C2.Re+C2.Im*C2.Im);
  62. C.Im=(C2.Re*Im-C2.Im*Re)/(C2.Re*C2.Re+C2.Im*C2.Im);
  63. return C;
  64. }
  65. }

Решение задачи: «Реализация действий над комплексными числами»

textual
Листинг программы
  1.         static void Main(string[] args)
  2.         {
  3.             Complex a = new Complex(2, 3);
  4.             Complex b = new Complex(4, 5);
  5.             Complex c = a - b;
  6.             Console.WriteLine(c.re.ToString() + " " + c.im.ToString());
  7.         }
  8.  
  9.     public class Complex
  10.     {
  11.         public float re;
  12.         public float im;
  13.  
  14.         public Complex(float r, float m)
  15.         {
  16.             re = r;
  17.             im = m;
  18.         }
  19.  
  20.         public Complex() //конструктор без параметров
  21.         {
  22.             re = im = 0;
  23.         }
  24.  
  25.         //public static Complex& operator - (Complex  C2);
  26.         //public static   Complex& operator *(Complex C2); //объявляем в классе
  27.         //public static Complex& operator /(Complex C2);
  28.  
  29.         //virtual void Show() //метод для отображения переменной на экране
  30.         //{
  31.         //if (im > 0)
  32.         //System.Console.WriteLine("(%.2f%.2f\n)", re, im);
  33.         //else System.Console.WriteLine("(%.2f%.2f\n)", re, im);
  34.         //}
  35.  
  36.         public static Complex operator +(Complex C1, Complex C2) //а здесь в классе  Complex -оператор
  37.         {
  38.             return new Complex(C1.re + C2.re, C1.im + C2.im);
  39.         }
  40.         public static Complex operator -(Complex C1, Complex C2)
  41.         {
  42.             return new Complex(C1.re - C2.re, C1.im - C2.im);
  43.         }
  44.  
  45.         public static Complex operator *(Complex C1, Complex C2) // Complex:: означает,что оператор относиться к классу
  46.         {
  47.             Complex C = new Complex();
  48.             C.re = (C1.re * C2.re - C1.im * C2.im);
  49.             C.im = (C2.re * C1.im + C1.re * C2.im);
  50.             return C;
  51.         }
  52.         public static Complex operator /(Complex C1, Complex C2)
  53.         {
  54.             Complex C = new Complex();
  55.             C.re = (C1.re * C2.re + C1.im * C2.im) / (C2.re * C2.re + C2.im * C2.im);
  56.             C.im = (C2.re * C1.im - C2.im * C1.re) / (C2.re * C2.re + C2.im * C2.im);
  57.             return C;
  58.         }
  59.     }

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


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

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

11   голосов , оценка 3.909 из 5

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

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

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