Skip to content

Instantly share code, notes, and snippets.

@kabachuha
Created February 9, 2016 09:35
Show Gist options
  • Save kabachuha/71ed4ac65c16eed3cfd7 to your computer and use it in GitHub Desktop.
Save kabachuha/71ed4ac65c16eed3cfd7 to your computer and use it in GitHub Desktop.
Quadratic. Java programm to generate quadratic equadtions
package artem226.quadratic.main;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Core {
public static Equadtions equadtionsPub = null;
public Core(){}
public static void main(String[] args) {
System.out.println("☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭");
System.out.println("☭Created by Artem226☭");
System.out.println("☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭☭");
System.out.println("-------------------------------------------------------------------------------------");
Random rand = new Random(System.currentTimeMillis());
Scanner scan = new Scanner(System.in);
System.out.println("Select action:");
System.out.println("a - Square root from quadratic equadtion(ax^2+bx+c=0)");
System.out.println("g - Generate n quadratic equadtions(ax^2+bx+c=0) and save them to file");
String s = null;
if(checkNoSave(args))
{
s= scan.nextLine();
}
char ch = '?';
if(s!=null && s.length() > 0)
{
ch = s.charAt(0);
}
else
ch='g';
if(ch == 'a' && checkNoSave(args))
{
int a;
int b;
int c;
System.out.println("ax^2+bx+c=0");
System.out.println("Enter a");
a = scan.nextInt();
if(a == 0)
{
System.out.println("a mustn't be 0!");
}
System.out.println("Enter b");
b = scan.nextInt();
System.out.println("Enter c");
c = scan.nextInt();
double d = b * b - (4 * a * c);
double x1 = (Math.sqrt(d) - b) / (2 * a);
double x2 = (-Math.sqrt(d) - b) / (2 * a);
System.out.println("X1 = "+x1);
System.out.println("X2 = "+x2);
}
if(ch == 'g')
{
String path = null;
if(checkNoSave(args))
{
System.out.println("Enter path to save file");
path = scan.nextLine();
}
int n;
if(args == null || args[1] == null)
{
System.out.println("Enter number of equations");
n= scan.nextInt();
}
else
{
n = Integer.parseInt(args[1]);
}
int toN = new Integer(n);
Equadtions eq = new Equadtions();
if(n>0)
{
double oldPercentage = 0.0D;
while(true)
{
int a = 0;
int b = 0;
int c = 0;
a = rand.nextInt(100);
//a=1;
b = rand.nextInt(100);
c = rand.nextInt(100);
if(rand.nextBoolean())
{
a = -a;
}
if(rand.nextBoolean())
{
b = -b;
}
if(rand.nextBoolean())
{
c = -c;
}
if(a != 0)
{
double d = b * b - (4 * a * c);
double x1 = (Math.sqrt(d) - b) / (2 * a);
double x2 = (-Math.sqrt(d) - b) / (2 * a);
int newB = -b/a;
int newC = c/a;
if(x1 + x2 == newB && x1 * x2 == newC)
{
int ii = (int)x1;
int jj = (int)x2;
double id = ii;
double jd = jj;
if(x1 != id || x2 != jd)
{
continue;
}
Equadtion[] eqOld = eq.getEquadtions();
Equadtion[] newE;
if(eqOld != null && eqOld.length >0)
{
newE = new Equadtion[eqOld.length + 1];
for(int i = 0; i < eqOld.length; i++)
{
newE[i] = eqOld[i];
}
}
else
{
newE = new Equadtion[1];
}
Equadtion equ = new Equadtion();
equ.setA(a);
equ.setB(b);
equ.setC(c);
equ.setX1(x1);
equ.setX2(x2);
equ.setStringForm(equ.getStringForm());
newE[newE.length - 1] = equ;
eq.setEquadtions(newE);
int xz = toN - n;
int xzz = toN - xz;
//double percent = (toN / 100);
//double percents = percent * (xz+1);
double percentage = (xzz * 100/ toN);
//System.out.println("persent ="+n);
//System.out.println("n ="+n);
//System.out.println("toN ="+toN);
//System.out.println("xz ="+xz);
if(percentage != oldPercentage)
System.out.println("Generating... " + (100 - percentage)+"%");
oldPercentage = percentage;
--n;
}
else
{
continue;
}
}
else
continue;
if(n <= 0)
{
equadtionsPub = eq;
if(checkNoSave(args))
if(write(path, eq))
{
System.out.println("Equadtions generated and writes to file in "+path+" Successfull!");
}
else
{
System.out.println("Something went wrong");
System.out.println("¯\\_(ツ)_/¯");
}
break;
}
}
}
}
scan.close();
}
public static boolean write(String path, Equadtions equad)
{
System.out.println("Turning data into json");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(equad, Equadtions.class);
//System.out.println(json);
boolean failed = false;
File fi = new File(path);
if(fi != null)
{
if(!fi.isDirectory())
{
System.out.println("Path isn't a directory!");
failed = true;
return false;
}
try
{
String worldPath = fi.getAbsolutePath();
File file = new File(worldPath+"//Equadtions.json");
if(file.isDirectory())
{
failed=true;
throw new IOException("File Equadtions.json is a directory! Please, delete it and reload world!");
}
if(!file.exists())
{
file.createNewFile();
}
System.out.println("Starting writing data into file");
if(file.canWrite())
{
FileUtils.writeStringToFile(file, json);
}
else
{
failed = true;
throw new IOException();
}
File only = new File(worldPath+"//OnlyF.txt");
if(only.isDirectory())
{
failed=true;
throw new IOException("File OnlyF.txt is a directory! Please, delete it and reload world!");
}
if(!only.exists())
{
only.createNewFile();
}
else
{
only.delete();
only.createNewFile();
}
System.out.println("Starting writing data into file(txt)");
if(only.canWrite())
{
if(equad.getEquadtions() != null && equad.getEquadtions().length > 0)
for(Equadtion e : equad.getEquadtions())
{
if(e != null)
if(e.getStringForm() != null && e.getStringForm().length() > 0)
{
FileUtils.writeStringToFile(only, e.getStringForm()+"\n", true);
}
}
}
else
{
failed = true;
throw new IOException();
}
}
catch (IOException e)
{
System.out.println("Unable to write "+ json.toString() +" to file!");
e.printStackTrace();
failed = true;
}
}
return !failed;
}
public static boolean checkNoSave(String[] arg)
{
return arg==null || arg.length<1 || arg[0]==null || !arg[0].equalsIgnoreCase("nosave");
}
public static class Equadtions
{
private Equadtion[] equad;
public Equadtion[] getEquadtions() {
return equad;
}
public void setEquadtions(Equadtion[] equad) {
this.equad = equad;
}
}
}
package artem226.quadratic.main;
public class Equadtion
{
private int a;
private int b;
private int c;
private double x1;
private double x2;
private String stringForm;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public String getStringForm() {
if(this.stringForm == null || this.stringForm.length() <= 0)
{
if(a != 0)
{
if(a == 1 && c == 0 && b == 0)
{
this.stringForm = "x^2=0";
return stringForm;
}
if(c == 0 && b != 0)
{
this.stringForm = ""+a+"x^2+"+ b + "x=0";
return stringForm;
}
if(c == 0 && b == 0)
{
this.stringForm = ""+a+"x^2=0";
return stringForm;
}
this.stringForm = ""+a+"x^2+"+ b + "x+"+c+"=0";
return stringForm;
}
}
return stringForm;
}
public void setStringForm(String stringForm) {
this.stringForm = stringForm;
}
public String toString()
{
return this.getStringForm();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment