Операции с коплексными числами (сложение, вычитание, умножение, деление, модуль) - C#
Формулировка задачи:
Операции с коплексными числами(сложение,вычитание, умножение, деление, модуль).Помогите исправить ошибки в программе
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Complex { int re, im; public Complex() { re = 0; im = 0; } public Complex(int re, int im) { this.re = re; this.im = im; } public static Complex operator +(Complex a, Complex b) { Complex c = new Complex(a.Get_re() + b.Get_re(), a.Get_im() + b.Get_im()); return c; } public int Get_re() { return re; } public int Get_im() { return im; } } public static Complex operator -(Complex a, Complex b) { Complex c = new Complex(a.Get_re() - b.Get_re(), a.Get_im() - b.Get_im()); return c; } public static Complex operator *(Complex a, Complex b) { Complex c = new Complex(a.Get_re() * b.Get_re(), a.Get_im() * b.Get_im()); return c; } public static Complex operator /(Complex a, Complex b) { Complex c = new Complex(a.Get_re() / b.Get_re(), a.Get_im() / b.Get_im()); return c; } public static double operator !(Complex a) { double c; c = Math.Sqrt((a.Get_re() * a.Get_re()) + (a.Get_im() * a.Get_im())); return c; } class Program { static void Main() {Complex a=new Complex(); Complex b,c; int re,im; Console.WriteLine("Вводим а"); re = Int32.Parse(Console.ReadLine()); im = Int32.Parse(Console.ReadLine()); b = new Complex(re, im); c=a+b; Console.WriteLine("a+b="+c.Get_re()+"i*"+c.Get_im());}} } }}
Решение задачи: «Операции с коплексными числами (сложение, вычитание, умножение, деление, модуль)»
textual
Листинг программы
class MainEntryPoint { public class Complex { public int Re { set; get; } public int Im { set; get; } public Complex() { Re = 0; Im = 0; } public Complex(int re, int im) { Re = re; Im = im; } public static Complex operator +(Complex lhs, Complex rhs) { return new Complex(lhs.Re + rhs.Re, lhs.Im + rhs.Im); } public static Complex operator -(Complex lhs, Complex rhs) { return new Complex(lhs.Re - rhs.Re, lhs.Im - rhs.Im); } public static Complex operator *(Complex lhs, Complex rhs) { Complex c = new Complex(); // используем правило умножения комплексных чисел c.Re = lhs.Im * rhs.Im + lhs.Re * rhs.Re; c.Im = lhs.Re * rhs.Im + lhs.Im * lhs.Re; return c; } public static Complex operator /(Complex lhs, Complex rhs) { Complex c = new Complex(); // используем правило деления комплексных чисел if ((lhs.Re != 0) && (rhs.Re != 0)) { c.Re = (lhs.Im * rhs.Im - lhs.Re * rhs.Re) / (rhs.Re * rhs.Re + rhs.Im * rhs.Im); c.Im = (rhs.Re * lhs.Im - rhs.Im * lhs.Re) / (rhs.Re * rhs.Re + rhs.Im * rhs.Im); } else { throw new DivideByZeroException(); } return c; } } static void Main(string[] args) { int re = 0; int im = 0; Console.WriteLine("Вводим a"); re = Int32.Parse(Console.ReadLine()); im = Int32.Parse(Console.ReadLine()); Complex a = new Complex(re, im); Console.WriteLine("Вводим b"); re = Int32.Parse(Console.ReadLine()); im = Int32.Parse(Console.ReadLine()); Complex b = new Complex(re, im); Complex c = a + b; Console.WriteLine("a+b=" + c.Re + "i*" + c.Im); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д