First commit
This commit is contained in:
commit
7fb31e9917
7 changed files with 135 additions and 0 deletions
42
.gitignore
vendored
Normal file
42
.gitignore
vendored
Normal file
|
@ -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
|
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
8
.idea/artifacts/Lab1_jar.xml
Normal file
8
.idea/artifacts/Lab1_jar.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<component name="ArtifactManager">
|
||||
<artifact type="jar" name="Lab1:jar">
|
||||
<output-path>$PROJECT_DIR$/out/artifacts/Lab1_jar</output-path>
|
||||
<root id="archive" name="Lab1.jar">
|
||||
<element id="module-output" name="Lab1" />
|
||||
</root>
|
||||
</artifact>
|
||||
</component>
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="ASK" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
3
src/META-INF/MANIFEST.MF
Normal file
3
src/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Main-Class: Main
|
||||
|
66
src/Main.java
Normal file
66
src/Main.java
Normal file
|
@ -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};
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue