Skip to content

Instantly share code, notes, and snippets.

@NuarkNoir
Last active November 1, 2017 13:52
Show Gist options
  • Save NuarkNoir/5934547599da8172a5be20af99884394 to your computer and use it in GitHub Desktop.
Save NuarkNoir/5934547599da8172a5be20af99884394 to your computer and use it in GitHub Desktop.
Класс для решения квадратных уравнений
package xyz.nuark;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Nuark
* @version 1.0
*
* Класс, который решает квадратные уравнения.
* Уравнения должны поступать в формате "ax^2 + bx + c = 0"(можно и без пробелов).
* Аргументы a, b и c не должны выходить за рамки double
* double - 64-разрядное число в формате IEEE 754 с плавающей точкой [-1.7E+308 .. 1.7E+308]
*/
public class EquationSolver {
public String equation;
private double a, b, c;
private final String regex = "([+-]?\\d+|[+-])?x\\^2[\\s]([+-][\\s]?\\d+|[+-])?x[\\s]+([+-]\\s?\\d+)[\\s]=[\\s]([+-]?\\d+)";
private Pattern pattern;
private Matcher matcher;
public EquationSolver(String equation) throws Exception {
equation = formatString(equation);
if (checkEquation(equation)){
if (!equation.contains("x^2") || !equation.contains("x"))
throw new Exception("Quadratic equation expected, linear given");
this.equation = equation;
splitToPieces();
} else throw new Exception("Not an equation");
}
private boolean checkEquation(String equation){
pattern = Pattern.compile(regex);
matcher = pattern.matcher(equation);
return matcher.find();
}
private String formatString(String equation){
return equation.replace(" ", "").replace("-0", "0").replace("=", " = ").replace("+", " +").replace("-", " -").trim();
}
private void splitToPieces(){
String eq = this.equation;
String[] tmp = eq.split(" = ");
int exc = Integer.parseInt(tmp[1]);
String[] t = tmp[0].split(" ");
String _a = t[0].replace("x^2", "");
String _b = t[1].replace("x", "");
String _c = t[2];
if ((_a.length() == 1) && ("-".equals(_a) || "+".equals(_a))) _a = _a.concat("1");
else if (_a.length() == 0) _a = _a.concat("1");
if ((_b.length() == 1) && ("-".equals(_b) || "+".equals(_b))) _b = _b.concat("1");
else if (_b.length() == 0) _b = _b.concat("1");
if ((_c.length() == 1 || _c.length() == 0) && ("-".equals(_c) || "+".equals(_c))) _c = _c.concat("1");
else if (_c.length() == 0) _c = _c.concat("1");
a = Double.parseDouble(_a);
b = Double.parseDouble(_b);
c = Double.parseDouble(_c) + exc;
}
public double[] solveEquation() throws Exception{
double _a = this.a;
double _b = this.b;
double _c = this.c;
double discr = (_b * _b) - (4 * _a * _c);
if (discr > 0){
double sq_discr = Math.sqrt(discr);
double x1 = (_b * -1) + sq_discr / (2 * _a);
double x2 = (_b * -1) - sq_discr / (2 * _a);
return new double[]{x1, x2};
} else if (discr == 0) {
double x1 = (_b * -1) / (2 * _a);
return new double[]{x1};
} else {
throw new Exception("Complex number can not be expressed with double type");
}
}
}
package xyz.nuark;
public class Main {
public static void main(String[] args) {
String test = "4x2-4x-42=0\n" +
"-1x^2 -2x +4 = 0\n" +
"x^2 +0x -16 = 0\n" +
"-x^2 +0x -16 = 0\n" +
"x^2 +x -16 = 0\n" +
"-x^2 +x -16 = 0\n" +
"x^2 -x -16 = 0\n" +
"-x^2 -x -16 = 0\n" +
"4x^2 +0x -16 = 0\n" +
"-0x^2 -25x +4225 = 0\n" +
"9x^2 - 30 = 0\n" +
"8x^2 + 45454541x - 42 = 0\n" +
"3x^2 - 0x - 27 = 0\n" +
"1x^2 - 0x - 16 = 0\n" +
"25x + 50 = 0\n" +
"123154549480 + 4\n" +
"-25x - 50 = 0\n" +
"1x^2 - 25x - 0 = 9\n" +
"2x^2 - 0x - 8 = 5\n" +
"1x^2 -0x - 16 = 0\n" +
"3x^2 - 0x - 27 = 0\n" +
"9x^2 - 3x + 0 = 0\n" +
"3x^2 - 0x - 257 = 0\n" +
"1313215645564x^2 + 6546x - 45451212115484 = 0\n" +
"9х = 4545454\n" +
"0\n" +
"3x^2 - 0x + 27 = 0\n" +
"2x^2 -1x + 28 = 0\n" +
"1x^2 - 2x + 29 = 0\n" +
"8x^2 a 2x + 29 = 0\n" +
"1x^2 - 2x 1 29 = 0\n" +
"1x^2 - 2x ++ 29 = 0\n" +
"-2x . 29 = 0\n" +
"-2x + 29 = 0\n" +
"фx^2 -1x + a8 = 0";
for (String eq : test.split("\n")){
try {
EquationSolver su = new EquationSolver(eq);
double[] answers = su.solveEquation();
if (answers.length == 1)
System.out.println("Equation " + eq + " has one answer: " + answers[0]);
else
System.out.println("Equation " + eq + " has two answers: " + answers[0] + " and " + answers[1]);
} catch (NumberFormatException ex) {
System.out.println("Wow, how? " + ex + " :: " + eq);
} catch (Exception ex) {
System.out.println(ex + " :: " + eq);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment