public static Fraction subtract(Fraction a, Fraction b, Fraction... fractions) { Fraction result = Fraction.subtract(a, b); for (Fraction c : fractions) { result = Fraction.subtract(result, c); } return result; }
public static void main(String args[]){ Scanner in=new Scanner(System.in); String op=null; while(in.hasNext()){ String line=in.nextLine(); StringTokenizer token=new StringTokenizer(line,"/+-",true); int i=0; String data[]=new String[token.countTokens()]; while(token.hasMoreTokens()) { data[i]=token.nextToken(); i++; } Fraction a=new Fraction(); Fraction b=new Fraction(); Fraction c=new Fraction(); Fraction d=new Fraction(); a.set(data[0],data[2]); b.set(data[4],data[6]); if(data[3].equals("+")) a.add(b).print(); else a.subtract(b).print(); } }
@Test public void testSubtract() { assertEquals(myFraction1.subtract(myFraction2).toString(), "1 / 5"); assertEquals(myFraction3.subtract(myFraction4).toString(), "3 / 7"); myFraction1 = myFraction1.add(myFraction1); }