View Code of Problem 3494

import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		while(in.hasNext()) {
			String str = in.next();
			String[] line = str.split("\\+|\\-|/");
			BigInteger x1 = new BigInteger(line[0]);
			BigInteger y1 = new BigInteger(line[1]);
			BigInteger x2 = new BigInteger(line[2]);
			BigInteger y2 = new BigInteger(line[3]);
			BigInteger x;
			if(str.charAt(3)=='+') {
				x = x1.multiply(y2).add(x2.multiply(y1));	
			}else {
				x = x1.multiply(y2).subtract(x2.multiply(y1));
			}
			BigInteger y = y1.multiply(y2);
			if(x.equals(BigInteger.ZERO)) {
				System.out.println(0);
			}else {
				BigInteger gcd = x.gcd(y);
				x=x.divide(gcd);
				y=y.divide(gcd);
				if(y.equals(BigInteger.ONE)) {
					System.out.println(x);	
				}else {
					System.out.println(x+"/"+y);		
				}
			}
		}
	}
}

Double click to view unformatted code.


Back to problem 3494