码迷,mamicode.com
首页 > 其他好文 > 详细

两分数的加减乘除

时间:2014-10-22 23:39:55      阅读:453      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

 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 }

 

两分数的加减乘除

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/Wqwwd/p/4044441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!