Задача.
Напишите класс для хранения комплексных чисел и реализуйте основные операции работы с ними.
Тесты.
Исходные числа | Операция | Результат |
z1 = 2 + 3i
z2 = -1 + 2i |
+ | 1.0 + 5.0i |
— | 3.0 + i | |
* | -8.0 + i | |
/ | 0.8 — 1.4i | |
3 + 4i | √ | 2.0 + i,
-2.0 -i |
-1 + 2i | pow | -3.0 — 4.0i |
Код программы
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/** * <h1>Complex Numbers</h1> * The ComplexNumber program implements an application that * allows to calculate complex numbers **/ public class ComplexNumber { /** * Represents the real part of a complex number */ private double re; /** * Represents imaginary part of a complex number */ private double im; public ComplexNumber(double re, double im) { this.re = re; this.im = im; } public double getRe() { return re; } public double getIm() { return im; } /** * @return modulus (or absolute value) of the number */ private double getModule() { return Math.sqrt(this.re * this.re + this.im * this.im); } /** * Allows to get the sum of two complex numbers given in the parameters. * * @return the new complex number */ public static ComplexNumber sum(ComplexNumber cn1, ComplexNumber cn2) { return new ComplexNumber(cn1.getRe() + cn2.getRe(), cn1.getIm() + cn2.getIm()); } /** * Allows to get the product of two complex numbers given in the parameters. * * @return the new complex number */ public static ComplexNumber multiply(ComplexNumber cn1, ComplexNumber cn2) { return new ComplexNumber(cn1.getRe() * cn2.getRe() - cn1.getIm() * cn2.getIm(), cn1.getRe() * cn2.getIm() + cn1.getIm() * cn2.getRe()); } /** * Allows to get the difference of two complex numbers given in the parameters. * * @return the new complex number */ public static ComplexNumber subtract(ComplexNumber cn1, ComplexNumber cn2) { return new ComplexNumber(cn1.getRe() - cn2.getRe(), cn1.getIm() - cn2.getIm()); } /** * Allows to get the product of two complex numbers given in the parameters. * * @return the new complex number */ public static ComplexNumber divide(ComplexNumber cn1, ComplexNumber cn2) { ComplexNumber temp = new ComplexNumber(cn2.getRe(), (-1) * cn2.getIm()); temp = ComplexNumber.multiply(cn1, temp); double denominator = cn2.getRe() * cn2.getRe() + cn2.getIm() * cn2.getIm(); return new ComplexNumber(temp.getRe() / denominator, temp.getIm() / denominator); } /** * This function allows to get the argument of complex number to represent it in trigonometric form * * @return argument of complex number */ private double GetArg() { if (this.re > 0) { return Math.atan(im / re); } else { if (re < 0 && im > 0) { return Math.PI + Math.atan(im / re); } else { return -Math.PI + Math.atan(im / re); } } } /** * Allows to raise complex number to specified power with the help of de Moivre's formula. * * @param cn needed complex number (the base) * @param power the exponent * @return the new complex number */ public static ComplexNumber pow(ComplexNumber cn, int power) { double factor = Math.pow(cn.getModule(), power); return new ComplexNumber(factor * Math.cos(power * cn.GetArg()), factor * Math.sin(power * cn.GetArg())); } /** * The function of getting square roots of complex number cn * * @return an array of pair of square roots */ public static ComplexNumber[] sqrt(ComplexNumber cn) { double a = cn.getModule() / 2; ComplexNumber pos = new ComplexNumber(Math.sqrt(a + cn.getRe() / 2), Math.signum(cn.getIm()) * Math.sqrt(a - cn.getRe() / 2)); ComplexNumber neg = new ComplexNumber((-1) * pos.getRe(), (-1) * pos.getIm()); ComplexNumber[] answer = {pos, neg}; return answer; } /** * Defines and returns the sign required for correct record of a number * * @return string with appropriate sign */ private String sign() { if (im > 0) return " + "; else return " - "; } @Override public String toString() { String string; if (im == 1 || im == -1) { if (re == 0) { string = sign() + "i"; } else { string = Double.toString(re) + sign() + "i"; } } else { string = Double.toString(re) + sign() + Double.toString(Math.abs(im)) + "i"; } return string; } @Override public boolean equals(Object obj) { if (this.getClass() != obj.getClass() || obj == null) return false; return true; } /** * In this function main test on the correctness of this program are done. * All operations on complex numbers are shown. */ public static void main(String[] args) { ComplexNumber x = new ComplexNumber(2, 3); ComplexNumber y = new ComplexNumber(-1, 2); System.out.println("z1 = " + x + ", z2 = " + y); ComplexNumber z; z = ComplexNumber.sum(x, y); System.out.println("+ : " + z); z = ComplexNumber.subtract(x, y); System.out.println("- : " + z); z = ComplexNumber.divide(x, y); System.out.println("/ : " + z); z = ComplexNumber.multiply(x, y); System.out.println(" * :" + z); z = ComplexNumber.pow(y, 2); System.out.println("Pow 2 of z2 : " + z); ComplexNumber b = new ComplexNumber(3, 4); ComplexNumber[] ans = ComplexNumber.sqrt(b); System.out.println("Sqrt of " + b + " = " + ans[0] + ", " + ans[1]); } } |
Ссылка на Ideone.