1 /** 2 * 分数的加、减、乘、除 3 * 4 * @author 王启文 5 * 6 */ 7 8 public class Private { 9 private double member; 10 private double denominator; 11 12 public Private(double member, double denominator) { 13 this.member = member; 14 this.denominator = denominator; 15 } 16 17 public double addition(Private other) { 18 double a = 0; 19 a = (member * other.denominator + denominator * other.member) 20 / (denominator * other.denominator); 21 return a; 22 } 23 24 public double subtraction(Private other) { 25 double a = 0; 26 a = (member * other.denominator - denominator * other.member) 27 / (denominator * other.denominator); 28 return a; 29 } 30 31 public double multiplication(Private other) { 32 double a = 0; 33 a = member * other.member / (denominator * other.denominator); 34 return a; 35 } 36 37 public double division(Private other) { 38 double a = 0; 39 a = member * other.denominator / (denominator * other.member); 40 return a; 41 } 42 }
1 import java.util.Scanner; 2 3 /** 4 * 对两个分数的运算 5 * 6 * @author 王启文 7 * 8 */ 9 10 public class p1 { 11 public static void main(String[] args) { 12 13 Scanner sc = new Scanner(System.in); 14 System.out.println("请输入第一个分数的分子、分母:"); 15 int a = sc.nextInt(); 16 int b = sc.nextInt(); 17 System.out.println("请输入第二个分数的分子、分母:"); 18 int c = sc.nextInt(); 19 int d = sc.nextInt(); 20 Private a1 = new Private(a, b); 21 Private a2 = new Private(c, d); 22 23 System.out.println(a + "/" + b + "+" + c + "/" + d + "=" 24 + a1.addition(a2)); 25 System.out.println(a + "/" + b + "-" + c + "/" + d + "=" 26 + a1.subtraction(a2)); 27 System.out.println(a + "/" + b + "*" + c + "/" + d + "=" 28 + a1.multiplication(a2)); 29 System.out.println(a + "/" + b + "/" + "(" + c + "/" + d + ")" + "=" 30 + a1.division(a2)); 31 32 sc.close(); 33 } 34 }
时间: 2024-10-25 06:25:19