package rpn.fc;

public class fraction {
	long n, d;
	
	public fraction(int n, int d) {
		this.n = n;
		this.d = d;
		reduce(this);
	}
	
	public fraction(long n, long d) {
		this.n = n;
		this.d = d;
		reduce(this);
	}
	
	public void reduce() {
		long r = gcd(n, d);
		if (d < 0) {
			n /= r;
			d /= r;
		} else {
			n /= -r;
			d /= -r;
		}
	}
	
	public static void reduce(fraction f) {
		long r = gcd(f.n, f.d);
		if (f.d < 0) {
			f.n /= r;
			f.d /= r;
		} else {
			f.n /= -r;
			f.d /= -r;
		}
	}
	
	public boolean isLegal(long n) {
		return(n == (int) n);
	}
	
	public static boolean isLegal(fraction f) {
		return(f.n == (int) f.n && f.d == (int) f.d);
	}
	
	public static boolean isInt(fraction f) {
		return(f.d == 1);
	}
	
	public boolean isZero(fraction f) {
		return(f.n == 0);
	}
	
	public static int gcd(int n, int m) {
		while (m != 0) {
			int r = n % m;
			n = m;
			m = r;
		}
		return(n);
	}
	
	public static long gcd(long n, long m) {
		while (m != 0) {
			long r = n % m;
			n = m;
			m = r;
		}
		return(n);
	}
	
	public static fraction sum(fraction a, fraction b) {
		long N = a.n*b.d + b.n*a.d;
		long D = a.d*b.d;
		fraction f = new fraction(N, D);
		f.reduce();
		return(f);
	}

	public static fraction diff(fraction a, fraction b) {
		long N = a.n*b.d - b.n*a.d;
		long D = a.d*b.d;
		fraction f = new fraction(N, D);
		f.reduce();
		return(f);
	}

	public static fraction prod(fraction a, fraction b) {
		long N = a.n*b.n;
		long D = a.d*b.d;
		fraction f = new fraction(N, D);
		f.reduce();
		return(f);
	}

	public static fraction quot(fraction a, fraction b) {
		long N = a.n*b.d;
		long D = a.d*b.n;
		fraction f = new fraction(N, D);
		f.reduce();
		return(f);
	}
	
}