import java.util.
Scanner;
public class Fraction{
private int n;
private int d;
public Fraction() {
this.n=0;
this.d=1;
}
public Fraction(int n, int d) {
if(d==0){
System.out.println("Le dénominateur ne peut pas être nul");
}
this.n = n;
this.d = d;
}
public Fraction(int n) {
this.n = n;
this.d = 1;
}
public static int pgcd(int a,int b){
while (b != 0) {
int r=a%b;
a = b;
b = r;
}
return Math.abs(a);
}
public static int ppcm(int a, int b){
return Math.abs(a/(pgcd(a, b)*b));
}
public Fraction simplifier(){
int pgcd=pgcd(n, d);
int as=n/pgcd;
int bs=d/pgcd;
return new Fraction(as,bs);
}
@Override
public String toString() {
return n + "/" + d;
}
public Fraction plus( Fraction f2) {
int nr =this.n*f2.d+this.d*f2.n;
int dr=this.d*f2.d;
Fraction resultat = new Fraction(nr,dr);
return resultat.simplifier();
}
public Fraction moins( Fraction f2) {
int nr =this.n*f2.d-this.d*f2.n;
int dr=this.d*f2.d;
Fraction resultat = new Fraction(nr,dr);
return resultat.simplifier();
}
public Fraction multiplie(Fraction f2){
int nr=this.n*f2.n;
int dr=this.d*f2.d;
Fraction resultat= new Fraction(nr,dr);
return (resultat.simplifier());
}
public Fraction divise(Fraction f2){
int nr=this.n*f2.d;
int dr=this.d*f2.n;
Fraction resultat= new Fraction(nr,dr);
return (resultat.simplifier());
}
public boolean equals(Fraction other){
if(this==other){
return true;
}
this.simplifier();
other.simplifier();
return this.n == other.n && this.d == other.d;
}
public static boolean equals(Fraction f1,Fraction f2){
if (f1 == f2) {
return true;
}
f1.simplifier();
f2.simplifier();
return f1.n == f2.n && f1.d == f2.d;
}
public int compareTo(Fraction other) {
double thisValue = (double) this.n / this.d;
double otherValue = (double) other.n / other.d;
if (thisValue < otherValue) {
return -1;
} else if (thisValue > otherValue) {
return 1;
} else {
return 0;
}
}
public static int compareTo(Fraction f1, Fraction f2) {
double f1Value = (double) f1.n / f1.d;
double f2Value = (double) f2.n / f2.d;
if (f1Value < f2Value) {
return -1;
} else if (f1Value > f2Value) {
return 1;
} else {
return 0;
}
}
public boolean estDecimale() {
int denominateur = this.d;
denominateur /= pgcd(Math.abs(this.n),
Math.abs(denominateur)); //pour simplifier
// Diviser autant que possible par 2
while (denominateur % 2 == 0) {
denominateur /= 2;
}
// Diviser autant que possible par 5
while (denominateur % 5 == 0) {
denominateur /= 5;
}
return denominateur == 1;
}
public int[] nChiffres(int n) {
int[] chiffres = new int[n];
int a = Math.abs(this.n);
int b = Math.abs(this.d);
a %= b; // On s'intéresse uniquement à la partie décimale
for (int i = 0; i < n; i++) {
a *= 10; // Décalage d'une décimale
chiffres[i] = a / b; // Extraction du chiffre après la
virgule
a %= b; // Mise à jour du reste
}
return chiffres;
}
}
import java.util.Scanner;
public class TestFraction {
public static void main(String[] args) {
Fraction f1 = Fraction.valueOf(args[0].trim());
Fraction f2 = Fraction.valueOf(args[1].trim());
System.out.println(("f1 = " + f1));
System.out.println(("f2 = " + f2));
Scanner scanner= new Scanner(System.in);
System.out.print("donner le nominateur de F1:");
int a =scanner.nextInt();
System.out.print("donner le denominateur de F1:");
int b =scanner.nextInt();
Fraction f1=new Fraction(a,b);
System.out.print("donner le nominateur de F1:");
int c =scanner.nextInt();
System.out.print("donner le denominateur de F1:");
int d =scanner.nextInt();
Fraction f2=new Fraction(c,d);
System.out.println("Somme (f1 + f2) : " + f1.plus(f2));
// Affichage des fractions et de leur somme
System.out.println("Fraction 1 " + a+"/"+b);
System.out.println("Fraction 2 " + c + "/" +d);
System.out.println("Somme (f1 + f2) : " + f1.plus(f2));
// Création des fractions f3 et f4
Fraction f3 = new Fraction(6, 14);
Fraction f4 = new Fraction(1, 5);
// Calculs et affichages des résultats
System.out.println("\nFraction 3 (f3) : " + f3 + "Fraction 4
(f4) : " + f4);
System.out.println("Somme (f3 + f4) : " + f3.plus(f4));
System.out.println("Différence (f3 - f4) : " + f3.moins(f4));
System.out.println("Produit (f3 * f4) : " + f3.multiplie(f4));
System.out.println("Rapport (f3 / f4) : " + f3.divise(f4));
// Extraction et affichage des 5 premiers chiffres après la
virgule de la somme (f3 + f4)
int[] chiffres = f3.plus(f4).nChiffres(5);
System.out.print("5 premiers chiffres après la virgule de la
somme (f3 + f4) : ");
for (int chiffre : chiffres) {
System.out.print(chiffre);
}
System.out.println();
}
}