CGC_Codes

C# AutoRest Controller

Feb 21st, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2.  
  3. struct Complex
  4. {
  5.     float real;
  6.     float imaginary;
  7.  
  8.     public Complex(float real, float imaginary)
  9.     {
  10.         this.real = real;
  11.         this.imaginary = imaginary;
  12.     }
  13.  
  14.     public float real
  15.     {
  16.         get
  17.         {
  18.             return (real);
  19.         }
  20.         set
  21.         {
  22.             real = value;
  23.         }
  24.     }
  25.  
  26.     public float Imaginary
  27.     {
  28.         get
  29.         {
  30.             return (imaginary);
  31.         }
  32.         set
  33.         {
  34.             imaginary = value;
  35.         }
  36.     }
  37.  
  38.     public override string ToString()
  39.     {
  40.        return (String.Format("({0}, {1}i)", real, imaginary));
  41.     }
  42.  
  43.     public static bool operator==(Complex c1, Complex c2)
  44.     {
  45.         if ((c1.real == c2.real) &&
  46.         (c1.imaginary == c2.imaginary))
  47.             return (true);
  48.         else
  49.             return (false);
  50.     }
  51.  
  52.     public static bool operator !=(Complex c1, Complex c2)
  53.     {
  54.         return (!(c1 == c2));
  55.     }
  56.  
  57.     public override bool Equals(object o2)
  58.     {
  59.         Complex c2 = (Complex)o2;
  60.  
  61.         return (this == c2);
  62.     }
  63.  
  64.     public override int GetHashCode()
  65.     {
  66.         return (real.GetHashCode() ^ imaginary.GetHashCode());
  67.     }
  68.  
  69.     public static Complex operator +(Complex c1, Complex c2)
  70.     {
  71.         return (new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary));
  72.     }
  73.  
  74.     public static Complex operator -(Complex c1, Complex c2)
  75.     {
  76.         return (new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary));
  77.     }
  78.  
  79.     // product of two complex numbers
  80.     public static Complex operator *(Complex c1, Complex c2)
  81.     {
  82.         return (new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
  83.         c1.real * c2.imaginary + c2.real * c1.imaginary));
  84.     }
  85.  
  86.     // quotient of two complex numbers
  87.     public static Complex operator /(Complex c1, Complex c2)
  88.     {
  89.         if ((c2.real == 0.0f) &&
  90.         (c2.imaginary == 0.0f))
  91.             throw new DivideByZeroException("Can't divide by zero Complex number");
  92.  
  93.         float newReal =
  94.         (c1.real * c2.real + c1.imaginary * c2.imaginary) /
  95.         (c2.real * c2.real + c2.imaginary * c2.imaginary);
  96.         float newImaginary =
  97.         (c2.real * c1.imaginary - c1.real * c2.imaginary) /
  98.         (c2.real * c2.real + c2.imaginary * c2.imaginary);
  99.  
  100.         return (new Complex(newReal, newImaginary));
  101.     }
  102.  
  103.     // non-operator versions for other languages
  104.     public static Complex Add(Complex c1, Complex c2)
  105.     {
  106.         return (c1 + c2);
  107.     }
  108.  
  109.     public static Complex Subtract(Complex c1, Complex c2)
  110.     {
  111.         return (c1 - c2);
  112.     }
  113.  
  114.     public static Complex Multiply(Complex c1, Complex c2)
  115.     {
  116.         return (c1 * c2);
  117.     }
  118.  
  119.     public static Complex Divide(Complex c1, Complex c2)
  120.     {
  121.         return (c1 / c2);
  122.     }
  123. }
  124.  
  125. public class AComplexNumberClass
  126. {
  127.     public static void Main()
  128.     {
  129.         Complex c1 = new Complex(3, 1);
  130.         Complex c2 = new Complex(1, 2);
  131.  
  132.         Console.WriteLine("c1 == c2: {0}", c1 == c2);
  133.         Console.WriteLine("c1 != c2: {0}", c1 != c2);
  134.         Console.WriteLine("c1 + c2 = {0}", c1 + c2);
  135.         Console.WriteLine("c1 - c2 = {0}", c1 - c2);
  136.         Console.WriteLine("c1 * c2 = {0}", c1 * c2);
  137.         Console.WriteLine("c1 / c2 = {0}", c1 / c2);
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment