Added subtract and divide operations

This commit is contained in:
Yadukrishnan 2024-04-09 09:00:01 +02:00
parent cdf1bcdc0e
commit 9ddceb29f1
2 changed files with 63 additions and 12 deletions

View File

@ -4,17 +4,17 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ComplexNumber {
public long real;
public long imaginary;
public double real;
public double imaginary;
public ComplexNumber(long a, long b) {
public ComplexNumber(double a, double b) {
this.real = a;
this.imaginary = b;
}
public ComplexNumber(String complexNumberStr) {
Pattern pattern = Pattern.compile("(-?\\d+)?(?:([+-]?\\d+)i)?");
Pattern pattern = Pattern.compile("(-?\\d*\\.?\\d+)?(?:([+-]?\\d*\\.?\\d+)i)?");
Matcher matcher = pattern.matcher(complexNumberStr.replaceAll("\\s", ""));
if (matcher.matches()) {
@ -23,20 +23,20 @@ public class ComplexNumber {
String imaginaryPartStr = matcher.group(2);
// Parse real part (if present)
real = (realPartStr != null) ? Long.parseLong(realPartStr) : 0;
real = (realPartStr != null) ? Double.parseDouble(realPartStr) : 0;
// Parse imaginary part (if present)
imaginary = (imaginaryPartStr != null) ? Long.parseLong(imaginaryPartStr) : 0;
imaginary = (imaginaryPartStr != null) ? Double.parseDouble(imaginaryPartStr) : 0;
} else {
throw new IllegalArgumentException("Invalid complex number format, supported format is `a+bi`");
throw new IllegalArgumentException("Invalid complex number format(" + complexNumberStr + "), supported format is `a+bi`");
}
}
public long getReal() {
public double getReal() {
return real;
}
public long getImaginary() {
public double getImaginary() {
return imaginary;
}
@ -49,8 +49,19 @@ public class ComplexNumber {
}
public ComplexNumber multiply(ComplexNumber that) {
long newReal = this.real * that.real - this.imaginary * that.imaginary;
long newImaginary = this.real * that.imaginary + this.imaginary * that.real;
double newReal = this.real * that.real - this.imaginary * that.imaginary;
double newImaginary = this.real * that.imaginary + this.imaginary * that.real;
return new ComplexNumber(newReal, newImaginary);
}
public ComplexNumber subtract(ComplexNumber that) {
return new ComplexNumber(real - that.getReal(), imaginary - that.getImaginary());
}
public ComplexNumber divide(ComplexNumber that) {
double c2d2 = Math.pow(that.real, 2) + Math.pow(that.imaginary, 2);
double newReal = (this.real * that.real + this.imaginary * that.imaginary) / c2d2;
double newImaginary = (this.imaginary * that.real - this.real * that.imaginary) / c2d2;
return new ComplexNumber(newReal, newImaginary);
}

View File

@ -4,7 +4,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class ComplexNumberAddMultiplyUnitTest {
public class ComplexNumberOperationsUnitTest {
@ParameterizedTest(name = "Multiplying {0} and {1}")
@CsvSource({
@ -46,6 +46,46 @@ public class ComplexNumberAddMultiplyUnitTest {
Assertions.assertTrue(isSame(sum, expected));
}
@ParameterizedTest(name = "Subtracting {0} and {1}")
@CsvSource({
"3+2i, 1+7i, 2-5i",
"2, 4, -2",
"2, 4i, 2-4i",
"1+1i, 1+1i, 0",
" 3+ 2i, 1+ 7i, 2-5i",
"0+5i, 3+0i, -3+5i",
"0+0i, -2+0i, 2+0i",
"-3+2i, 1-7i, -4+9i",
"2+4i, 0, 2+4i"
})
public void subtract_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) {
ComplexNumber complex1 = new ComplexNumber(complexStr1);
ComplexNumber complex2 = new ComplexNumber(complexStr2);
ComplexNumber expected = new ComplexNumber(expectedStr);
ComplexNumber sum = complex1.subtract(complex2);
Assertions.assertTrue(isSame(sum, expected));
}
@ParameterizedTest(name = "Dividing {0} and {1}")
@CsvSource({
"3+2i, 1+7i, 0.34-0.38i",
"2, 4, 0.5",
"2, 4i, 0-0.5i",
"1+1i, 1+1i, 1",
"3 + 2i, 1 + 7i, 0.34-0.38i",
"0+5i, 3+0i, 0+1.6666666666666667i",
"0+0i, -2+0i, 0+0i",
"-3+2i, 1-7i, -0.34-0.38i",
"2+4i, 1, 2+4i"
})
public void divide_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) {
ComplexNumber complex1 = new ComplexNumber(complexStr1);
ComplexNumber complex2 = new ComplexNumber(complexStr2);
ComplexNumber expected = new ComplexNumber(expectedStr);
ComplexNumber sum = complex1.divide(complex2);
Assertions.assertTrue(isSame(sum, expected));
}
public boolean isSame(ComplexNumber result, ComplexNumber expected) {
return result.real == expected.real && result.imaginary == expected.imaginary;
}