public class Rational{
private long numerator=0;//分子
private long denominator=1;//分母
public Rational() {
super();
}
public Rational(long numerator, long denominator) {
// TODO Auto-generated constructor stub
long gcd=gcd(numerator,denominator);
this.numerator=((denominator>0)?1:-1)*numerator/gcd;
this.denominator=Math.abs(denominator)/gcd;
}
//求两个数最大公约数以便约分
private static long gcd(long n, long d) {
// TODO Auto-generated method stub
long n1=Math.abs(n);
long n2=Math.abs(d);
int gcd=1;
for(int k=1;k<=n1&&k<=n2;k++){
if(n1%k==0&&n2%k==0)
gcd=k;
}
return gcd;
}
//获得有理数的分子或者分母
public long getNumerator(){
return numerator;
}
public long getDenominator(){
return denominator;
}
//有理数相加
public Rational add(Rational secondRational){
long n=numerator*secondRational.getDenominator()+
denominator*secondRational.getNumerator();
long d=denominator*secondRational.getDenominator();
return new Rational(n,d);
}
//有理数相减
public Rational subtract(Rational secondRational){
long n=numerator*secondRational.getDenominator()-
denominator*secondRational.getNumerator();
long d=denominator*secondRational.getDenominator();
return new Rational(n,d);
}
//有理数相乘
public Rational multiply(Rational secondRational){
long n=numerator*secondRational.getNumerator();
long d=denominator*secondRational.getDenominator();
return new Rational(n,d);
}
//有理数相除
public Rational divide(Rational secondRational){
long n=numerator*secondRational.denominator;
long d=denominator*secondRational.numerator;
return new Rational(n,d);
}
public String toString(){
if(denominator==1)
return numerator+"";
if(numerator==0)
return "0";
else
return numerator+"/"+denominator;
}
public boolean equals(Object parm1){
if( this.denominator!=0 )
return true;
else
return false;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Rational R=new Rational(sc.nextInt(),sc.nextInt());
/* long numerator=sc.nextInt();
long denominator=sc.nextInt();*/
Rational secondRational=new Rational(sc.nextInt(),sc.nextInt());
/*
System.out.println("分子:"+R.getNumerator());
System.out.println("分母:"+R.getDenominator());
System.out.println(R.toString());
*/ if(R.equals(R)&secondRational.equals(secondRational)) {
System.out.println("相加后:"+R.add(secondRational).toString());
System.out.println("相减后:"+R.subtract(secondRational).toString());
if(secondRational.getDenominator()==0||secondRational.getNumerator()==0)
System.out.println("被除数不能为0");
else
System.out.println("相除后:"+R.divide(secondRational).toString());
System.out.println("相乘后:"+R.multiply(secondRational).toString());
}
else {
System.out.println("分母不能为0");
}
}
}