Advertisement
ZazoTazo

Complex

Nov 10th, 2020
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. struct complex
  2.     {
  3.         public float real;
  4.         public float imaginary;
  5.  
  6.         public complex(float real, float imaginary)
  7.         {
  8.             this.real = real;
  9.             this.imaginary = imaginary;
  10.         }
  11.  
  12.         public static complex operator +(complex a, complex b)
  13.         {
  14.             return new complex(a.real + b.real, a.imaginary + b.imaginary);
  15.         }
  16.         public static complex operator -(complex a, complex b)
  17.         {
  18.             return new complex(a.real - b.real, a.imaginary - b.imaginary);
  19.         }
  20.  
  21.         public override string ToString()
  22.         {
  23.             return (String.Format("{0} + {1}i", real, imaginary));
  24.         }
  25.     }
  26.  
  27.     class Program
  28.     {
  29.         static void Main(string[] args)
  30.         {
  31.             complex c1 = new complex(3.0f, 5.0f);
  32.             complex c2 = new complex(2.5f, 3.0f);
  33.  
  34.             complex c = c1 + c2;
  35.             Console.WriteLine("Sum = {0}", c);
  36.             Console.ReadLine();
  37.         }
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement