Advertisement
Ivan_Bochev

Fraction calculator

Mar 30th, 2019 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. package kr_2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FractionCalc {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         while (true) {
  9.             String exp = sc.nextLine();
  10.             if (exp.equalsIgnoreCase("end")) {
  11.                 break;
  12.             }
  13.             char op = exp.charAt(exp.indexOf(" ") + 1);
  14.             String[] drobi = exp.split(" ([+]|[-]|[*]|[/]) ");
  15.             Fraction d1 = new Fraction(drobi[0]);
  16.             Fraction d2 = new Fraction(drobi[1]);
  17.             Fraction d3 = null;
  18.             if (op == '+') {
  19.                 d3 = d1.sum(d2);
  20.             } else if (op == '-') {
  21.                 d3 = d1.subtract(d2);
  22.             } else if (op == '*') {
  23.                 d3 = d1.multiply(d2);
  24.             }
  25.             if (op == '/') {
  26.                 d3 = d1.divide(d2);
  27.             }
  28.             System.out.println(d3);
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement