commit 7fb31e99172f56fbc498462aa0d6047e3a21cd07 Author: erius Date: Fri Jul 21 00:07:52 2023 +0300 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b63da45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/artifacts/Lab1_jar.xml b/.idea/artifacts/Lab1_jar.xml new file mode 100644 index 0000000..a140e5a --- /dev/null +++ b/.idea/artifacts/Lab1_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/Lab1_jar + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..1349636 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,66 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Main { + + public static void main(String[] args) { + System.out.println("Enter the quadratic equations coefficients (e.g. for x^2 + 2x + 0.9 = 0 enter 1 2 0.9"); + while (true) { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String input; + try { + input = reader.readLine(); + } catch (IOException e) { + System.err.println("Failed to read user's input"); + e.printStackTrace(); + continue; + } + double[] coefficients = inputToDoubleArray(input); + if (coefficients == null) continue; + double[] solutions = solveEquation(coefficients); + switch (solutions.length) { + case 0: + System.out.println("No real solutions"); + break; + case 1: + System.out.printf("One real solution: x1 = %.3f\n", solutions[0]); + break; + case 2: + System.out.printf("Two real solutions: x1 = %.3f\tx2 = %.3f\n", solutions[0], solutions[1]); + break; + } + } + } + + private static double[] inputToDoubleArray(String input) { + String[] arr = input.trim().split(" "); + double[] coefficients = new double[3]; + if (arr.length != 3) { + System.out.println("There must be only 3 arguments separated by a white space (e.g. 1 2 3)"); + return null; + } + + for (int i = 0; i < arr.length; i++) { + try { + double coefficient = Double.parseDouble(arr[i]); + coefficients[i] = coefficient; + } catch (NumberFormatException e) { + System.out.println("Your input should contain only numbers (if it's a float - use a dot instead of a comma e.g. 1.2)"); + return null; + } + } + return coefficients; + } + + private static double[] solveEquation(double... coefficients) { + double a = coefficients[0], b = coefficients[1], c = coefficients[2]; + double d = b * b - 4 * a * c; + if (d < 0) return new double[]{}; + double sqrtD = Math.sqrt(d); + double x1 = (-b + sqrtD) / (2 * a), x2 = (-b - sqrtD) / (2 * a); + if (d == 0) return new double[]{x1}; + return new double[]{x1, x2}; + } + +}