0% found this document useful (0 votes)
9 views

Đa TH C Public Class Dathuc

The document describes a DaThuc (polynomial) class with methods to calculate the value of a polynomial for a given x value, add two polynomials together, multiply a polynomial by a scalar, solve a polynomial equation, take the derivative of a polynomial, and convert a polynomial to a string. It then provides examples of using the class to add, multiply, take derivatives of, solve equations for, and read polynomials from a file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Đa TH C Public Class Dathuc

The document describes a DaThuc (polynomial) class with methods to calculate the value of a polynomial for a given x value, add two polynomials together, multiply a polynomial by a scalar, solve a polynomial equation, take the derivative of a polynomial, and convert a polynomial to a string. It then provides examples of using the class to add, multiply, take derivatives of, solve equations for, and read polynomials from a file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ĐA THỨC

public class DaThuc


{
private double a,b,c;
public DaThuc(){a=b=c=0;}
public DaThuc(double x, double y, double z){a=x;b=y;c=z;}
public double GiaTriDaThuc(double x)
{
return a*x*x+b*x+c;
}
public DaThuc CongDaThuc(DaThuc f)
{
return new DaThuc(a+f.a,b+f.b,c+f.c);
}
public static DaThuc CongDaThuc (DaThuc f, DaThuc g)
{
return new DaThuc(f.a+g.a,f.b+g.b,f.c+g.c);
}
public DaThuc NhanSoThuc (double k)
{
return new DaThuc(a*k,b*k,c*k);
}
public String[] GiaiPhuongTrinh()
{
if (a==0)
if(b==0)
if(c==0) {String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo so nghiem";
return ketqua;}
else {String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo nghiem";
return ketqua;}
else {String[] ketqua=new String[1];
ketqua[0]=Double.toString(-c/b);
return ketqua;}
else
{
double delta=b*b-4*a*c;
if (delta<0)
{String[] ketqua=new String[1];
ketqua[0]="Phuong trinh vo nghiem";
return ketqua;}
else {String[] ketqua=new String[2];
ketqua[0]="x1= "+Double.toString((-b+Math.sqrt(delta))/2/a);
ketqua[1]="x2= "+Double.toString((-b-Math.sqrt(delta))/2/a);
return ketqua;}
}
}
public DaThuc DaoHam()
{
return new DaThuc(0,2*a,b);
}
public static DaThuc DaoHam(DaThuc f)
{
return new DaThuc(0,2*f.a,f.b);
}
public String toString()
{
String hs1=a!=0?(a+"x "):"";
String hs2=(b>0?"+":"")+(b!=0?(b+"x"):"");
String hs3=(c>0?"+":"")+(c!=0?(c+""):"");
return a==0&&b==0&&c==0?"0":hs1+hs2+hs3;
}
}
//MAIN
//Cho da thuc f(x)=2x2+4 -9
DaThuc f= new DaThuc(2,4,-9);
System.out.println("f(x)= "+f);
System.out.println("f(0) = "+f.GiaTriDaThuc(0));
//Gan lai da thuc f va tao da thuc g nhu sau
//f(x)= x2 -4
//g(x)= x2 + 5x -6
f= new DaThuc(1,0,-4);
DaThuc g= new DaThuc(1,5,-6);
System.out.println("f(x)= "+f);
System.out.println("g(x)= "+g);
double f0= f.GiaTriDaThuc(0);
//Tinh gia tri f(0) va g(-2)
System.out.println("f(0)= "+f0);
System.out.println("g(-2)= "+g.GiaTriDaThuc(-2));
//In ra dao ham cua g
System.out.println("g'(x)= "+g.DaoHam());
//In ra gia tri g'(0)
System.out.println("g'(0)= "+(g.DaoHam()).GiaTriDaThuc(0));

//Tinh h(x)= f(x)+g(x)


DaThuc h= DaThuc.CongDaThuc(f, g);
System.out.println("f(x)+ g(x)= "+h);
//Tinh f(g(-1)) v g(f(2))
System.out.println("f(g(-1)) = "+f.GiaTriDaThuc(g.GiaTriDaThuc(-1)));
System.out.println("g(f(2)) = "+g.GiaTriDaThuc(f.GiaTriDaThuc(2)));
//Tinh g(x)-f(x)
DaThuc m= f.NhanSoThuc(-1);
DaThuc n = DaThuc.CongDaThuc(g, m);
System.out.println("g(x)-f(x)= "+n);
//4f(x)-2g(x)
m= f.NhanSoThuc(4);
n=g.NhanSoThuc(-2);
System.out.println("4f(x)-2g(x)= "+DaThuc.CongDaThuc(m,n));
//Giai PT f(x)=0
String[] kq=f.GiaiPhuongTrinh();
for (int i=0;i<kq.length;i++)
System.out.println(kq[i]);
//Giai PT g(x)=0
kq=g.GiaiPhuongTrinh();
for (int i=0;i<kq.length;i++)
System.out.println(kq[i]);
//Tim giao diem cua y=3 va y=x
kq=(DaThuc.CongDaThuc(new DaThuc(0,0,3),(new DaThuc(0,1,0).NhanSoThuc(-
1)))).GiaiPhuongTrinh();
for (int i=0;i<kq.length;i++)
System.out.println(kq[i]);
//Duong thang y=2x+3 cat f(x) tai dau?
DaThuc y= new DaThuc(0,2,3);
kq=(DaThuc.CongDaThuc(f,y.NhanSoThuc(-1))).GiaiPhuongTrinh();
for (int i=0;i<kq.length;i++)
System.out.println(kq[i]);
//Doc va giai PTB2 trong tap tin
File file = new File("D:\\PTB2.txt");
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
{
String[] tokens = st.split(",");
DaThuc k= new
DaThuc(Double.valueOf(tokens[0]),Double.valueOf(tokens[1]),Double.valueOf(tokens[2]));
String[] res=k.GiaiPhuongTrinh();
System.out.println("Giai PT "+k+" = 0");
for (int i=0;i<res.length;i++) System.out.println(res[i]+" ");
System.out.println();
}
}
catch (Exception e) {}

SINHVIEN

CLASS SINHVIEN
package demo;

public class SinhVien


{
private String MSSV;
private String HoLot;
private String Ten;
private String Nganh;
private String QueQuan;
private int Ngay,Thang,Nam;
public SinhVien(){}
public SinhVien(String mssv,String holot, String ten, String nganh, String quequan, int ngay, int thang,
int nam)
{
MSSV=mssv; HoLot=holot;Ten=ten;Nganh=nganh;QueQuan=quequan;
Ngay=ngay; Thang=thang; Nam=nam;
}
public String toString()
{
String kq=MSSV+", "+HoLot+" "+Ten+", "+Ngay+"/"
+Thang+"/"+Nam+", "+Nganh+", "+QueQuan;
return kq;
}
}

/*
static final int MAX=4000;
static SinhVien dssv[]=new SinhVien[MAX];
static int slsv=0;
public static void main(String[] args) {
slsv=MyLibrary.ReadData("D:\\SinhVien.txt",dssv);
for (int i=0;i<slsv;i++)
System.out.println(dssv[i]);
System.out.println(slsv);
*/

//CLASS QLY SINHVEIN


package demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class TenComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return -(a.Ten.compareTo(b.Ten));
}
}
class QueQuanComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return (a.QueQuan.compareTo(b.QueQuan));
}
}
class SV
{
public String MSSV;
public String HoLot;
public String Ten;
public String Nganh;
public String QueQuan;
public int Ngay,Thang,Nam;
public SV(){}
public SV(String mssv,String holot, String ten, String nganh, String quequan, int ngay, int thang, int
nam)
{
MSSV=mssv; HoLot=holot;Ten=ten;Nganh=nganh;QueQuan=quequan;
Ngay=ngay; Thang=thang; Nam=nam;
}
//public String getTen(){return Ten;}
public String toString()
{
String kq=MSSV+", "+HoLot+" "+Ten+", "+Ngay+"/"
+Thang+"/"+Nam+", "+Nganh+", "+QueQuan;
return kq;
}
}

class ThangSinhComparator implements Comparator <SV>


{
public int compare(SV a, SV b)
{
return -(a.Thang-b.Thang);
}
}
class NgaySinhComparator implements Comparator <SV>
{
public int compare(SV a, SV b)
{
return (a.Nam-b.Nam)!=0?(a.Nam-b.Nam):
((a.Thang-b.Thang)!=0?(a.Thang-b.Thang):a.Ngay-b.Ngay);
}
}
public class QuanLySV {
static ArrayList<SV> dssv=new ArrayList<SV>();
static int ReadDataToArrayList(String fname, ArrayList<SV> ds)
{
int soluong=0;
File f = new File(fname);
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = br.readLine()) != null)
{
String[] tokens = st.split(",");
soluong++;
FillDataToArrayList(tokens,ds);
}
}
catch (Exception e) {System.out.println("Loi tap tin");}
return soluong;
}

static void FillDataToArrayList(String[] s, ArrayList<SV> ds)


{
SV sv = new SV();
sv.MSSV=s[0];
sv.HoLot=s[1];
sv.Ten=s[2];
String[] tokens = s[3].split("-");
sv.Ngay=Integer.valueOf(tokens[0]);
sv.Thang=Integer.valueOf(tokens[1]);
sv.Nam=Integer.valueOf(tokens[2]);
sv.Nganh=s[4];
sv.QueQuan=s[5];
ds.add(sv);
}

public static void main(String[] args) {


int slsv=ReadDataToArrayList("D:\\SinhVien.txt",dssv);
//for (int i=0;i<slsv;i++)
// System.out.println(dssv.get(i));
System.out.println(slsv);
//Sap xep theo ngay sinh (nam-thang-ngay
NgaySinhComparator cp= new NgaySinhComparator();

Collections.sort(dssv,cp);
//Dao nguoc danh sach
Collections.reverse(dssv);
for (int i=0;i<slsv;i++)
System.out.println(dssv.get(i));
//Tim Kiem sinh vien ten Thanh
SV kq = dssv.stream()
.filter(sv -> "Thanh".equals(sv.Ten))
.findAny()
.orElse(null);
System.out.println("===>"+kq);
//Tim Kiem sinh vien co que quan Thanh Hoa
kq = dssv.stream()
.filter(sv -> "Thanh Hoa".equals(sv.QueQuan))
.findAny()
.orElse(null);
System.out.println("===>"+kq);
//Tim Kiem sinh vien sinh thang 4 nam 2001
kq = dssv.stream()
.filter(sv -> sv.Thang==4 && sv.Nam==2000)
.findAny()
.orElse(null);
System.out.println("===>"+kq);
//Sap xep bang cach dung cu phap Lambda 1.8
dssv.sort((SV sv1, SV sv2) -> {
return sv1.Ten.compareTo(sv2.Ten);
});
//Duyet danh sach theo cu phap Lambda
//de in ra cac sinh vien sinh ngay 1 thang 4
dssv.forEach(
sv ->
{
if (sv.Ngay==1&&sv.Thang==4)
System.out.println(sv);}
);
}
}

//MY LIBRARY
package demo;

import java.io.*;
import java.util.ArrayList;
class MyLibrary {
static void FillData(String[] s, SinhVien[] ds, int pos)
{
String mssv=s[0];
String holot=s[1];
String ten=s[2];
String[] tokens = s[3].split("-");
int ngay=Integer.valueOf(tokens[0]);
int thang=Integer.valueOf(tokens[1]);
int nam=Integer.valueOf(tokens[2]);
String nganh=s[4];
String quequan=s[5];
ds[pos]= new SinhVien(mssv,holot,ten,nganh,quequan,ngay,thang,nam);
}

static int ReadData(String fname, SinhVien[] ds)


{
int soluong=0;
File f = new File(fname);
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = br.readLine()) != null)
{
String[] tokens = st.split(",");
soluong++;
FillData(tokens,ds,soluong);
}
}
catch (Exception e) {System.out.println("Loi tap tin");}
return soluong;
}
}

//MAIN
package demo;

public class Main


{
static final int MAX=4000;
static SinhVien dssv[]=new SinhVien[MAX];
static int slsv=0;
public static void main(String[] args) {
slsv=MyLibrary.ReadData("D:\\SinhVien.txt",dssv);
for (int i=0;i<slsv;i++)
System.out.println(dssv[i]);
System.out.println(slsv);
}

PHAN SO
package demo;

public class PhanSo {


private int tu, mau;

public PhanSo(int tu, int mau) {


this.tu = tu;
this.mau = mau;
}

public int getTu() {


return tu;
}
public void setTu(int tu) {
this.tu = tu;
}

public int getMau() {


return mau;
}

public void setMau(int mau) {


this.mau = mau;
}

public PhanSo() {
tu=0;
mau=1;
}
public PhanSo(PhanSo ps) {
tu=ps.tu;
mau=ps.mau;
}
public int timUSCLN(int a, int b) {
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
public PhanSo RutGonPS()
{
int i = timUSCLN(tu, mau);
this.setTu(this.getTu() / i);
this.setMau(this.getMau() / i);
PhanSo p= new PhanSo(tu,mau);
return p;
}

public PhanSo congPhanSo(PhanSo p) {


int tuSo= tu*p.mau+mau*p.tu;
int mauSo= mau*p.mau;

PhanSo tong= new PhanSo(tuSo,mauSo);


return tong.RutGonPS();

}
public PhanSo nhanPhanSo(PhanSo p) {
int tuSo= tu*p.tu;
int mauSo= mau*p.mau;
PhanSo tich= new PhanSo(tuSo,mauSo);
return tich.RutGonPS();
}

public PhanSo nghichDaoPS() {

PhanSo dao= new PhanSo(mau,tu);


return dao;
}
public double GiaTriPS() {
return (double) this.tu/this.mau;
}

public PhanSo nhanVoiSo(int n) {


PhanSo p= new PhanSo(this.tu*n,this.mau);
return p.RutGonPS();
}
@Override
public String toString() {
return "PhanSo " + tu + "/" + mau;
}

//MAIN
public class Test {
public static void main(String[] args) {

PhanSo A= new PhanSo(5,6);


PhanSo B= new PhanSo(12, 36);
System.out.println(A);
System.out.println("Tong cua 2 phan so A va B: "+ A.congPhanSo(B));
System.out.println("Ket qua cua phep chia phan so A voi phan so B:
"+A.nhanPhanSo(B).nghichDaoPS());
System.out.println("Phan so toi gian cua phan so B: "+ B.RutGonPS());
// PhanSo kq;
// kq=A.nhanVoiSo(5).congPhanSo(B.nhanVoiSo(10));
// System.out.println("Ket qua cua 5*A+10*B o dang toi gian: "+kq);
// PhanSo kq2;
// PhanSo AB,BA;
// AB= A.nhanPhanSo(B.nghichDaoPS());
// System.out.println(AB);
// BA= B.nhanPhanSo(A.nghichDaoPS());
// System.out.println(BA);
// kq2= AB.nhanVoiSo(2).congPhanSo(BA.nhanVoiSo(-4));
// System.out.println("Ket qua cua 2(A/B)-4(B/A): "+kq2);
ArrayList<PhanSo> L= new ArrayList<PhanSo>();
L.add(new PhanSo(1,1));
L.add(new PhanSo(2,1));
L.add(new PhanSo(0,1));
L.add(new PhanSo(1,3));
L.add(new PhanSo(1,4));
L.add(new PhanSo(4,1));
L.add(new PhanSo(4,3));

System.out.println("Tong cac cap phan so trong danh sach: ");


for(int i=0; i<L.size();i++)
{
for(int j=0;j<L.size();j++)
{
if(i!=j)
{
System.out.println(L.get(i).congPhanSo(L.get(j)));
}
}
PhanComparator ps= new PhanComparator();
PhanSo min=L.get(0);
for (PhanSo phanSo : L) {
if(ps.compare(min, phanSo)>0)
min=phanSo;

}
System.out.println("Phan So nho nhat trong danh sach: "+ min);
PhanSo max=L.get(0);
for (PhanSo phanSo : L) {
if(ps.compare(max, phanSo)<0)
max= phanSo;
}
System.out.println("Phan So lon nhat trong danh sach: "+ max);
Collections.sort(L,ps);
for (PhanSo phanSo : L) {
System.out.println(phanSo);
}
}

}
}

You might also like