wang_songyi 2017-09-26 15:48 采纳率: 0%
浏览 1093

java小白求问一个和rational number有关的问题

rational

 import java.math.BigInteger;

public class Rational {
 // Data fields for numerator and denominator
 private BigInteger numerator = BigInteger.ZERO;
 private BigInteger denominator = BigInteger.ONE;


 /** Construct a rational with default properties */
 public Rational() {
  this(BigInteger.ZERO, BigInteger.ONE);
 }

 /** Construct a rational with specified numerator and denominator */
 public Rational(BigInteger numerator, BigInteger denominator) {
     BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator, denominator)));
     BigInteger r1=new BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
     this.numerator = (r1.multiply(numerator)).divide(gcd);
     this.denominator = (denominator.abs()).divide(gcd);
 }

 /** Find GCD of two numbers */
 private static long gcd(BigInteger n, BigInteger d) {
  BigInteger n1 = n.abs();
  BigInteger n2 = d.abs();
  int gcd = 1;

  for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 && (new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
   if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) && n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO)) 
    gcd = k;
  }

  return gcd;
 }

 /** Return numerator */
 public BigInteger getNumerator() {
  return numerator;
 }

 /** Return denominator */
 public BigInteger getDenominator() {
  return denominator;
 }

 /** Add a rational number to this rational */
 public Rational add(Rational secondRational) {
  BigInteger n = numerator.multiply(secondRational.getDenominator()).add(denominator.multiply(secondRational.getNumerator()));
  BigInteger d = denominator.multiply(secondRational.getDenominator());
  return new Rational(n, d);
 }

 /** Subtract a rational number from this rational */
 public Rational subtract(Rational secondRational) {
     BigInteger n = (numerator.multiply(secondRational.getDenominator())).subtract(denominator.multiply(secondRational.getNumerator()));
     BigInteger d = denominator.multiply(secondRational.getDenominator());
  return new Rational(n, d);
 }

 /** Multiply a rational number to this rational */
 public Rational multiply(Rational secondRational) {
     BigInteger n = numerator.multiply(secondRational.getNumerator());
     BigInteger d = denominator.multiply(secondRational.getDenominator());
  return new Rational(n, d);
 }

 /** Divide a rational number from this rational */
 public Rational divide(Rational secondRational) {
     BigInteger n = numerator.multiply(secondRational.getDenominator());
     BigInteger d = denominator.multiply(secondRational.numerator);
  return new Rational(n, d);
 }

/** Compute the square of this rational number*/
 public Rational square() {
     BigInteger n = numerator.multiply(numerator);
     BigInteger d = denominator.multiply(denominator);
  return new Rational(n, d);
 }

/** toString */
 public String toString() {
      return numerator + "/" + denominator;
 }
}

testrational

 public static void main(String[]args){
        int y = 1;

        BigInteger i=new BigInteger(String.valueOf(1));
        BigInteger a=new BigInteger(String.valueOf(2));
        BigInteger b=new BigInteger(String.valueOf(3));
        BigInteger c=new BigInteger(String.valueOf(5));



        Rational r1 = new  Rational(i,a);
        Rational r2 = new  Rational(a,b);
        Rational r3 = new  Rational(a,c);


        Rational s1 = r3.multiply(r2);
        Rational s2 = r3.square();
        Rational s3 = r2.divide(r3);

        Rational r0 = new  Rational(b,b.add(i));
        do{

            r1 = r1.add(r0);
            b = b.add(a);
            y++;




        }while(y<49);
            System.out.println(r1.multiply(r1));
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(r0);
    }
}

这个程序的大概意思就是要在testRational里面计算1/2+3/4+...+99/100
求解为什么我的r0输出的结果为3/4,而不是期望的99/100
为什么循环没有正常的执行?

  • 写回答

1条回答 默认 最新

  • cloudyzhao 2017-09-27 00:51
    关注

    贴下代码吧 图片不好看呀

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配