import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hailx1701
*/
class SoHang{
protected float heSo;
protected int soMu;
public SoHang(){
this.heSo = 0;
this.soMu = 0;
}
public SoHang(float _a,int _b){
this.heSo = _a;
this.soMu = _b;
}
public void nhapSoHang(){
this.heSo = Float.parseFloat(JOptionPane.showInputDialog("hangso :","nhap"));
this.soMu = Integer.parseInt(JOptionPane.showInputDialog("so mu:","so mu"));
}
public void InKetQua(){
System.out.print(this.heSo+"*x^"+this.soMu);
}
public void sapxep(int x,SoHang _sh[]){
float _the_heso;
int _the_somu;
for(int nut=1;nut<=x-1;nut++){
for(int next=nut+1;next<=x;next++){
if(_sh[nut].soMu<_sh[next].soMu){
_the_somu = _sh[nut].soMu;
_sh[nut].soMu = _sh[next].soMu;
_sh[next].soMu = _the_somu;
_the_heso = _sh[nut].heSo;
_sh[nut].heSo = _sh[next].heSo;
_sh[next].heSo = _the_heso;
}
}
}
System.out.print("R(x)=");
int j=0;
for(int i=1;i<=x;i++){
System.out.print(_sh.heSo+"x^"+_sh.soMu);
j++;
if(j!=x){
System.out.print("+");
}else{
System.out.print(".\n");
}
}
}
public void ToiGian(int x,SoHang _sh[]){
int k=0;
for(int q=1;q<x;q++){
for(int j=q+1;j<=x;j++){
if(_sh[q].soMu==_sh[j].soMu){
_sh[q].heSo+=_sh[j].heSo;
_sh[j].heSo=0;
_sh[j].soMu=0;
k++;
}
}
}
if(k!=0){
_sh[k].sapxep(x, _sh);
System.out.println("Đa thức sau khi tối giản:");
System.out.print("R(x)=");
int j=0;
for(int i=1;i<=x-k+1;i++){
System.out.print(_sh.heSo+"x^"+_sh.soMu);
j++;
if(j!=x-k+1){
System.out.print("+");
}else{
System.out.print(".\n");
}
}
}else{
System.out.println("Đa thức của bạn đã tối giản.");
}
}
public void NhanDaThuc(SoHang _sh[],SoHang _hs[],int x,int y){
int l=x*y;
SoHang _nhan[] = new SoHang[l+1];
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
_nhan[i*j] = new SoHang();
_nhan[i*j].soMu=_sh.soMu+_hs[j].soMu;
_nhan[i*j].heSo = _sh.heSo*_hs[j].heSo;
}
}
_nhan[l].ToiGian(l, _nhan);
}
}
public class phanTich {
public static void main(String[] phanthuc){
final int NUM_SO_HANG = 100;
int i=0,m=0;
SoHang sohang[] = new SoHang[NUM_SO_HANG];
SoHang g[] = new SoHang[NUM_SO_HANG];
System.out.print("R(x)=");
do{
i++;
sohang = new SoHang();
sohang.nhapSoHang();
sohang.InKetQua();
if(sohang.soMu!=0){
System.out.print("+");
}else{
System.out.print(".\n");
}
}while(sohang.soMu!=0);
//sap xep cac so hang trong da thuc bang phuong phap noi bot -bubble_Sort
System.out.println("Đa thức thứ nhất:");
sohang.sapxep(i, sohang);
//Toi gian Da Thuc
sohang.ToiGian(i,sohang);
//nhan hai da thuc - lay cai don gian ko nhan chet nguoi luon - tu lam nhe De tu.
System.out.print("Nhap da thuc thu 2:\n G(x)=");
do{
m++;
g[m] = new SoHang();
g[m].nhapSoHang();
g[m].InKetQua();
if(g[m].soMu!=0){
System.out.print("+");
}else{
System.out.print(".\n");
}
}while(g[m].soMu!=0);
System.out.println("Đa thức thứ hai:");
g[m].sapxep(m, g);
//nhan hai da thuc vua nhap
// g[m].NhanDaThuc(sohang, g, i, m);
}
}
// code thu bang java