|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace CsCodeExample |
| 8 | +{ |
| 9 | + public class ComplexNumber // class |
| 10 | + { |
| 11 | + private double real; // Fields (usually hidden with properties) |
| 12 | + |
| 13 | + public double Real // Properties ( Encapsulation, auto generated - Encapsulate field ) |
| 14 | + { |
| 15 | + get { return real; } |
| 16 | + set { real = value; } |
| 17 | + } |
| 18 | + |
| 19 | + public double Imag { get; set; } // Property with hidded field, PascalCase |
| 20 | + public static string Format { get { return "a + b * i"; } } |
| 21 | + |
| 22 | + protected const double PI = 3.14; // keyword: const (just for example, in this class Math.PI is used because it has more precision) |
| 23 | + |
| 24 | + public ComplexNumber() { } // class constructor, default - no parameters |
| 25 | + |
| 26 | + public ComplexNumber(double real, double imag = 0) // constructor with 2 parameters, second has default value |
| 27 | + { |
| 28 | + Real = real; |
| 29 | + Imag = imag; |
| 30 | + } |
| 31 | + |
| 32 | + public ComplexNumber Add(ComplexNumber input) |
| 33 | + { |
| 34 | + ComplexNumber result = new ComplexNumber(); |
| 35 | + result.Real = Real + input.Real; |
| 36 | + result.Imag = Imag + input.Imag; |
| 37 | + return result; |
| 38 | + } |
| 39 | + |
| 40 | + public static ComplexNumber Add(ComplexNumber a, ComplexNumber b) |
| 41 | + { |
| 42 | + ComplexNumber result = new ComplexNumber(); |
| 43 | + result.Real = a.Real + b.Real; |
| 44 | + result.Imag = b.Imag + b.Imag; |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + public ComplexNumber Subtract(ComplexNumber input) |
| 49 | + { |
| 50 | + ComplexNumber result = new ComplexNumber(); |
| 51 | + result.Real = Real - input.Real; |
| 52 | + result.Imag = Imag - input.Imag; |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + public ComplexNumber Multiply(ComplexNumber input) |
| 57 | + { |
| 58 | + ComplexNumber result = new ComplexNumber(); |
| 59 | + result.Real = Real * input.Real - Imag * input.Imag; |
| 60 | + result.Imag = Real * input.Imag + Imag * input.Real; |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + public ComplexNumber Divide(ComplexNumber input) |
| 65 | + { |
| 66 | + ComplexNumber result = new ComplexNumber(); |
| 67 | + result.Real = (Real * input.Real + Imag * input.Imag) / (input.Real * input.Real + input.Imag * input.Imag); |
| 68 | + result.Imag = (Real * input.Imag - Imag * input.Real) / (input.Real * input.Real + input.Imag * input.Imag); |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + public double Modul () { |
| 73 | + return Math.Sqrt(Real * Real + Imag * Imag); |
| 74 | + } |
| 75 | + |
| 76 | + public ComplexNumber Pow(int n) |
| 77 | + { |
| 78 | + ComplexNumber result = new ComplexNumber(1, 0); |
| 79 | + if (n != 0) |
| 80 | + { |
| 81 | + for (int i = 0; i < Math.Abs(n); i++) |
| 82 | + result = result.Multiply(this); |
| 83 | + if (n < 0) |
| 84 | + result = new ComplexNumber(1, 0).Divide(result); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + public List<ComplexNumber> Nroot(int n) { |
| 90 | + List<ComplexNumber> result = new List<ComplexNumber>(); |
| 91 | + double r = this.Modul(); |
| 92 | + double f = Math.Atan(Imag / Real); |
| 93 | + if (Real == 0.0) |
| 94 | + { |
| 95 | + f = Math.PI/2; |
| 96 | + if (Imag < 0) |
| 97 | + f += Math.PI; |
| 98 | + } |
| 99 | + if (Real < 0) |
| 100 | + f += Math.PI; |
| 101 | + double p1 = Math.Pow(r, n); |
| 102 | + |
| 103 | + for(var i = 0; i < n; i++) { |
| 104 | + double p2 = (f + 2 * i * Math.PI) / n; |
| 105 | + result.Add(new ComplexNumber(p1 * Math.Cos(p2), p1 * Math.Sin(p2))); |
| 106 | + } |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + public override string ToString() // keyword: override |
| 111 | + { |
| 112 | + return String.Format("({0:0.00}, {1:0.00})", Real, Imag); |
| 113 | + } |
| 114 | + |
| 115 | + public string ToCSV() // CSV - Comma Separated Value |
| 116 | + { |
| 117 | + return String.Format("{0:0.00},{1:0.00}", Real, Imag); |
| 118 | + } |
| 119 | + |
| 120 | + public virtual string ToDiffString() // keyword: virtual(can be overridden in child class) |
| 121 | + { |
| 122 | + string output = null; |
| 123 | + if(Imag == 0.0) |
| 124 | + output = String.Format("{0}", Real); |
| 125 | + else if(Imag > 0) |
| 126 | + output = String.Format("{0} + {1}i", Real, Imag); |
| 127 | + else if (Imag < 0) |
| 128 | + output = String.Format("{0} - {1}i", Real, -Imag); |
| 129 | + |
| 130 | + return output; |
| 131 | + } |
| 132 | + |
| 133 | + public static string GetDescription() |
| 134 | + { |
| 135 | + return "This class implements basic math function with complex numbers."; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public class MoreComplexNumber : ComplexNumber |
| 140 | + { |
| 141 | + public MoreComplexNumber() : base() { } |
| 142 | + |
| 143 | + public MoreComplexNumber(double real, double imag = 0) : base(real, imag) { } |
| 144 | + |
| 145 | + //public MoreComplexNumber(double real, double imag = 0) |
| 146 | + //{ |
| 147 | + // Real = real; |
| 148 | + // Imag = imag; |
| 149 | + //} |
| 150 | + |
| 151 | + public override string ToDiffString() |
| 152 | + { |
| 153 | + string output = null; |
| 154 | + if (Imag == 0.0) |
| 155 | + output = String.Format("{0}", Real); |
| 156 | + else if (Imag > 0) |
| 157 | + output = String.Format("{0} + {1} * i", Real, Imag); |
| 158 | + else if (Imag < 0) |
| 159 | + output = String.Format("{0} - {1} * i", Real, -Imag); |
| 160 | + |
| 161 | + return output; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments