Fixed the issue with some formats

This commit is contained in:
Yadukrishnan 2024-04-07 21:35:11 +02:00
parent eed82faf31
commit 1ede00ad50
1 changed files with 34 additions and 17 deletions

View File

@ -17,24 +17,39 @@ class ComplexNumber {
this.imaginary = b; this.imaginary = b;
} }
public ComplexNumber(String complexNumberStr) { public ComplexNumber(String complexNumberStr1) {
Pattern pattern = Pattern.compile("(-?\\d+)\\s*[+]\\s*(-?\\d+)\\s*i|(-?\\d+)\\s*i\\s*[+]\\s*(-?\\d+)");
Matcher matcher = pattern.matcher(complexNumberStr); String complexNumberStr = complexNumberStr1.replaceAll("\\s", "");
if (matcher.find()) { if (complexNumberStr.contains("i")) {
// Extract real and imaginary parts // Split the string based on '+' or '-'
long realPart; String[] parts = complexNumberStr.split("(?=[+-])");
long imaginaryPart;
if (matcher.group(1) != null && matcher.group(2) != null) { if (parts.length == 1) {
realPart = Long.parseLong(matcher.group(1)); // Group 1 is the real part // Only real part (no imaginary part)
imaginaryPart = Long.parseLong(matcher.group(2)); // Group 2 is the imaginary part if (complexNumberStr.endsWith("i")) {
real = 0;
imaginary = Long.parseLong(parts[0].substring(0, parts[0].length() - 1));
} else { } else {
realPart = Long.parseLong(matcher.group(4)); // Group 4 is the real part real = Long.parseLong(complexNumberStr);
imaginaryPart = Long.parseLong(matcher.group(3)); // Group 3 is the imaginary part imaginary = 0;
} }
this.real = realPart; } else if (parts.length == 2) {
this.imaginary = imaginaryPart; // Both real and imaginary parts present
real = Long.parseLong(parts[0]);
String imaginaryString = parts[1].substring(0, parts[1].length() - 1); // Remove 'i'
if (imaginaryString.isEmpty()) {
// Only 'i' without coefficient, hence imaginary part is 1
imaginary = 1;
} else { } else {
throw new IllegalArgumentException("String does not match the complex number pattern of `a+bi` or `bi+a`."); imaginary = Long.parseLong(imaginaryString);
}
} else {
throw new IllegalArgumentException("Invalid complex number format");
}
} else {
// Only real part without 'i'
real = Long.parseLong(complexNumberStr);
imaginary = 0;
} }
} }
@ -71,18 +86,20 @@ public class ComplexNumberAddMultiplyUnitTest {
@ParameterizedTest(name = "Multiplying {0} and {1}") @ParameterizedTest(name = "Multiplying {0} and {1}")
@CsvSource({ @CsvSource({
"3+2i, 1+7i, -11+23i", "3+2i, 1+7i, -11+23i",
"2, 4, 8",
"2, 4i, 8i",
"1+1i, 1+1i, 0+2i", "1+1i, 1+1i, 0+2i",
" 3+2i, 1 + 7i, -11 + 23i ", " 3+2i, 1 + 7i, -11 + 23i ",
"0+5i, 3+0i, 0+15i", "0+5i, 3+0i, 0+15i",
"0+0i, -2+0i, 0+0i", "0+0i, -2+0i, 0+0i",
"-3+2i, 1-7i, -23-23i" "-3+2i, 1-7i, 11+23i",
"2+4i, 0, 0"
}) })
public void sum_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) { public void sum_two_complex_numbers(String complexStr1, String complexStr2, String expectedStr) {
ComplexNumber complex1 = new ComplexNumber(complexStr1); ComplexNumber complex1 = new ComplexNumber(complexStr1);
ComplexNumber complex2 = new ComplexNumber(complexStr2); ComplexNumber complex2 = new ComplexNumber(complexStr2);
ComplexNumber expected = new ComplexNumber(expectedStr); ComplexNumber expected = new ComplexNumber(expectedStr);
ComplexNumber multiplied = complex1.multiply(complex2); ComplexNumber multiplied = complex1.multiply(complex2);
System.out.println(multiplied.toString());
Assertions.assertTrue(multiplied.isSame(expected)); Assertions.assertTrue(multiplied.isSame(expected));
} }