Skip to content

Instantly share code, notes, and snippets.

@LaBanHSPO
Last active December 7, 2015 12:03
Show Gist options
  • Save LaBanHSPO/faad67805276185a902e to your computer and use it in GitHub Desktop.
Save LaBanHSPO/faad67805276185a902e to your computer and use it in GitHub Desktop.
[JAVA-PRACTICE] - Giải hệ phương trình bậc nhất (có khả năng thi kết thúc học phần >viết code + vấn đáp)
package ktpmk12b-ictu;
import java.util.Scanner;
/**
*
* @author HARD-Code
*/
public class HePtB1 {
double a1, b1, c1;
double a2, b2, c2;
public void Input() {
Scanner s = new Scanner(System.in);
System.out.println("------------SSG-----------");
System.out.println("Vao he so: a1, b1, c1 = ");
a1 = s.nextDouble(); b1 = s.nextDouble(); c1 = s.nextDouble();
System.out.println("--Het PT1. Moi ban Nhap PT2--");
System.out.println("a2, b2, c2 = ");
a2 = s.nextDouble(); b2 = s.nextDouble(); c2 = s.nextDouble();
System.out.println("-------Hoan thanh Nhap-------");
System.out.println("");
}
public void PpThe(){
double b11=b1*a2;
double c11=c1*a2;
double b22=b2*a1;
double c22=c2*a1;
double y = (c22-c11)/(b11-b22);
System.out.println("y = "+y);
double x = (-c11-b11*y)/(a1*a2);
System.out.println("x = "+x);
}
}
package ktpmk12b-ictu;
/**
* @author HARD-Code
*/
public class Main {
public static void main(String[] args) {
HePtB1 pt1 = new HePtB1();
pt1.Input();
if (!THUATTOAN.checkPTVN(pt1)) {
System.out.println("__TB: PT VÔ NGHIÊM__");
} else {
System.out.println("-----NGHIEM THEO PP THẾ-------");
pt1.PpThe();
Double D[] = THUATTOAN.DinhThuc(pt1);
System.out.println("----NGHIEM THEO ĐỊNH THỨC-----");
System.out.println("Phuong trinh co nghiem la:");
System.out.println("y= " + D[2] / D[0]);
System.out.println("x= " + D[1] / D[0]);
}
}
}
package ktpmk12b-ictu;
/**
*
* @author HARD-Code
*/
public class THUATTOAN {
/**
* @param pt:(ax+b+c=0) => pt.a1 pt.b1 -pt.c1
* Return: D[0] = D a1b2 - a2b1
* D[1]= Dx c1.b2 - b1.c2
* D[2]= Dy a1.c2 - c1. a2
*/
public static Double[] DinhThuc(HePtB1 pt) {
Double D[] = new Double[3];
D[0] = (pt.a1 * pt.b2) - (pt.a2 * pt.b1);
D[1] = ((-(pt.c1)) * pt.b2) - (pt.b1 * (-(pt.c2)));
D[2] = (pt.a1 * (-(pt.c2))) - (pt.a2 * (-(pt.c1)));
return D;
}
public static boolean checkPTVN(HePtB1 _pt) {
boolean check = true;
if (((_pt.a1 / _pt.a2) == (_pt.b1 / _pt.b2)) && ((_pt.c1 / _pt.c2) == (_pt.b1 / _pt.b2)))
check = false;
if ((_pt.a1 ==0 && _pt.a2==0 ) || (_pt.a2 ==0 && _pt.b2 ==0)) check =false;
double c = THUATTOAN.DinhThuc(_pt)[0];
if (c==0) check=false;
return check;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment