// Exercise 3g from Lecture 3 using System; public class Tester { public struct Complex { // explicit fields: // public int Real; // public int Imag; // automatic properties: public int Real { get ; set ; } public int Imag { get ; set ; } public Complex (int r, int i) { this.Real = r; this.Imag = i; } public override string ToString() { return this.Real.ToString()+"+"+this.Imag.ToString()+"i"; } public static int sqr (int x) { return x*x; } public static Complex operator+ (Complex a, Complex b) { return new Complex(a.Real + b.Real, a.Imag + b.Imag); } public static Complex operator- (Complex a, Complex b) { return new Complex(a.Real - b.Real, a.Imag - b.Imag); } public static Complex operator* (Complex a, Complex b) { return new Complex(a.Real*b.Real - a.Imag*b.Imag, a.Real*b.Imag + a.Imag*b.Real); } public static Complex operator/ (Complex a, Complex b) { return new Complex(a.Imag*b.Real - a.Real*b.Imag, sqr(b.Real) + sqr(b.Imag)); } } public static void Main (string []args) { if (args.Length != 4) { // expect 4 args: r1 i1 r2 i2 System.Console.WriteLine("Usage: exercise4f "); } else { int r1 = Convert.ToInt32(args[0]); int i1 = Convert.ToInt32(args[1]); int r2 = Convert.ToInt32(args[2]); int i2 = Convert.ToInt32(args[3]); Complex c1 = new Complex(r1,i1); Complex c2 = new Complex(r2,i2); System.Console.WriteLine("Complex addition: {0} + {1} = {2}", c1, c2, c1+c2); System.Console.WriteLine("Complex subtraction: {0} - {1} = {2}", c1, c2, c1-c2); System.Console.WriteLine("Complex multiplication: {0} * {1} = {2}", c1, c2, c1*c2); System.Console.WriteLine("Complex division: {0} / {1} = {2}", c1, c2, c1/c2); } } }