Java语言程序设计与数据结构(基础篇)课后练习题 第十三章

BigInteger n = numerator.multiply(secondRational.getDenominator())

.subtract(denominator.multiply(secondRational.getNumerator()));

BigInteger d = denominator.multiply(secondRational.getDenominator());

return new Rational(n, d);

}

public Rational multiply(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getNumerator());

BigInteger d = denominator.multiply(secondRational.getDenominator());

return new Rational(n, d);

}

public Rational divide(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getDenominator());

BigInteger d = denominator.multiply(secondRational.getNumerator());

return new Rational(n, d);

}

@Override

public String toString() {

if (denominator.equals(new BigInteger(“1”)) || numerator.equals(new BigInteger(“0”))) {

return numerator.toString();

} else

return numerator.toString() + “/” + denominator.toString();

}

@Override

public boolean equals(Object other) {

if (this.subtract((Rational) other).getNumerator().equals(new BigInteger(“0”))) {

return true;

} else

return false;

}

@Override

public int intValue() {

return (int) doubleValue();

}

@Override

public float floatValue() {

return (float) doubleValue();

}

@Override

public double doubleValue() {

return numerator.divide(denominator).doubleValue();

}

@Override

public long longValue() {

return (long) doubleValue();

}

@Override

public int compareTo(Rational o) {

if (this.subtract(o).getNumerator().compareTo(new BigInteger(“0”)) > 0)

return 1;

else if (this.subtract(o).getNumerator().compareTo(new BigInteger(“0”)) < 0)

return -1;

else

return 0;

}

}

package dishisanzhang;

import java.math.BigInteger;

import java.util.Scanner;

public class dishisanzhang {

public static void main(String[] args) throws CloneNotSupportedException {

Scanner input = new Scanner(System.in);

System.out.print("Enter the first rational number: ");

BigInteger n1 = new BigInteger(input.nextInt()+“”);

BigInteger n2 = new BigInteger(input.nextInt()+“”);

Rational r1 = new Rational(n1,n2);

System.out.print("Enter the second second number: ");

BigInteger n3 = new BigInteger(input.nextInt()+“”);

BigInteger n4 = new BigInteger(input.nextInt()+“”);

Rational r2 = new Rational(n3,n4);

System.out.println(n1+“/”+n2+" + “+n3+”/“+n4+” = "+r1.add(r2));

System.out.println(n1+“/”+n2+" - “+n3+”/“+n4+” = "+r1.subtract(r2));

System.out.println(n1+“/”+n2+" * “+n3+”/“+n4+” = "+r1.multiply(r2));

System.out.println(n1+“/”+n2+" / “+n3+”/“+n4+” = "+r1.divide(r2));

System.out.println(n3+“/”+n4+" is "+(n3.floatValue()/n4.floatValue()));

// 3 454

// 7 2389

}

}

13.16

==================================================================

package dishisanzhang;

import java.math.BigInteger;

public class dishisanzhang {

public static void main(String[] args) throws CloneNotSupportedException {

String[] s = args[0].split(“[/ ]”);

if (s.length == 5) {

calculator(s);

} else if (s.length == 6) {

String[] s2 = { s[0], s[1], “/”, s[4], s[5] };

calculator(s2);

}

}

public static void calculator(String[] s) {

BigInteger n1 = new BigInteger(s[0]);

BigInteger d1 = new BigInteger(s[1]);

Rational r1 = new Rational(n1, d1);

BigInteger n2 = new BigInteger(s[3]);

BigInteger d2 = new BigInteger(s[4]);

Rational r2 = new Rational(n2, d2);

System.out.print(Integer.parseInt(s[0]) + “/” + Integer.parseInt(s[1]) + " " + Integer.parseInt(s[2]) + " "

  • Integer.parseInt(s[3]) + “/” + Integer.parseInt(s[4]) + " = ");

switch (s[2].charAt(0)) {

case ‘+’:

System.out.println(Integer.parseInt(r1.add(r2).toString()));

break;

case ‘-’:

System.out.println(Integer.parseInt(r1.subtract(r2).toString()));

break;

case ‘*’:

System.out.println(Integer.parseInt(r1.multiply(r2).toString()));

break;

case ‘/’:

System.out.println(Integer.parseInt(r1.divide(r2).toString()));

}

}

}

13.17

==================================================================

package dishisanzhang;

public class Complex implements Cloneable, Comparable< Complex > {

private double realPart;

private double imaginaryPart;

public Complex() {

}

public Complex(double a) {

this(a, 0);

}

public Complex(double a, double b) {

this.realPart = a;

this.imaginaryPart = b;

}

public double getRealPart() {

return realPart;

}

public double getImaginaryPart() {

return imaginaryPart;

}

public Complex add(Complex c) {

double r = realPart + c.getRealPart();

double i = imaginaryPart + c.getImaginaryPart();

return new Complex(r, i);

}

public Complex subtract(Complex c) {

double r = realPart - c.getRealPart();

double i = imaginaryPart - c.getImaginaryPart();

return new Complex(r, i);

}

public Complex multiply(Complex c) {

double r = realPart * c.getRealPart() - imaginaryPart * c.getImaginaryPart();

double i = realPart * c.getImaginaryPart() + imaginaryPart * c.getRealPart();

return new Complex(r, i);

}

public Complex divide(Complex c) {

double r = (realPart * c.getRealPart() + imaginaryPart * c.getImaginaryPart())

/ (c.getRealPart() * c.getRealPart() + c.getImaginaryPart() * c.getImaginaryPart());

double i = (realPart * c.getImaginaryPart() - imaginaryPart * c.getRealPart())

/ (c.getRealPart() * c.getRealPart() + c.getImaginaryPart() * c.getImaginaryPart());

return new Complex(r, i);

}

public double abs() {

return Math.sqrt(realPart * realPart + imaginaryPart * imaginaryPart);

}

@Override

public String toString() {

if (realPart == 0)

return imaginaryPart + “i”;

else if (imaginaryPart == 0)

return realPart + “”;

else

return realPart + " + " + imaginaryPart + “i”;

}

@Override

public Object clone() {

return new Complex(realPart, imaginaryPart);

}

@Override

public int compareTo(Complex c) {

if (this.abs() > c.abs())

return 1;

else if (this.abs() < c.abs())

return -1;

else

return 0;

}

}

package dishisanzhang;

import java.util.Scanner;

public class dishisanzhang {

public static void main(String[] args) throws CloneNotSupportedException {

Scanner input = new Scanner(System.in);

System.out.print("Enter the first complex number : ");

double r1 = input.nextDouble();

double i1 = input.nextDouble();

Complex complex1 = new Complex(r1, i1);

System.out.print("Enter the second complex number : ");

double r2 = input.nextDouble();

double i2 = input.nextDouble();

Complex complex2 = new Complex(r2, i2);

String c1 = “(” + r1 + " + " + i1 + “i)”;

String c2 = “(” + r2 + " + " + i2 + “i)”;

System.out.println( c1 + " + " + c2 + " = " + complex1.add(complex2));

System.out.println( c1 + " - " + c2 + " = " + complex1.subtract(complex2));

System.out.println( c1 + " * " + c2 + " = " + complex1.multiply(complex2));

System.out.println( c1 + " / " + c2 + " = " + complex1.divide(complex2));

System.out.println( “|” + c1 + " + " + c2 + "| = " + complex1.abs());

// 3.5 5.5

// -3.5 1

}

}

13.18

==================================================================

package dishisanzhang;

import java.math.BigInteger;

public class Rational implements Comparable< Rational > {

private BigInteger numerator = new BigInteger(“0”);

private BigInteger denominator = new BigInteger(“1”);

public Rational() {

this(new BigInteger(“0”), new BigInteger(“1”));

}

public Rational(BigInteger numerator, BigInteger denominator) {

BigInteger gcd = gcd(numerator, denominator);

if (denominator.compareTo(new BigInteger(“0”)) > 0) {

this.numerator = numerator.divide(gcd);

} else

this.numerator = numerator.multiply(new BigInteger(“-1”)).divide(gcd);

this.denominator = denominator.abs().divide(gcd);

}

private static BigInteger gcd(BigInteger n, BigInteger d) {

BigInteger n1 = n.abs();

BigInteger n2 = d.abs();

BigInteger gcd = new BigInteger(“1”);

BigInteger n3 = new BigInteger(“1”);

for (int i = 1; i <= n1.intValue() && i <= n2.intValue(); i++) {

if ((n1.remainder(n3).equals(new BigInteger(“0”))) && (n2.remainder(n3).equals(new BigInteger(“0”)))) {

gcd = n3;

}

n3 = n3.add(new BigInteger(“1”));

}

return gcd;

}

public BigInteger getNumerator() {

return numerator;

}

public BigInteger getDenominator() {

return denominator;

}

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);

}

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);

}

public Rational multiply(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getNumerator());

BigInteger d = denominator.multiply(secondRational.getDenominator());

return new Rational(n, d);

}

public Rational divide(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getDenominator());

BigInteger d = denominator.multiply(secondRational.getNumerator());

return new Rational(n, d);

}

@Override

public String toString() {

if (denominator.equals(new BigInteger(“1”)) || numerator.equals(new BigInteger(“0”))) {

return numerator.toString();

} else

return numerator.toString() + “/” + denominator.toString();

}

@Override

public boolean equals(Object other) {

if (this.subtract((Rational) other).getNumerator().equals(new BigInteger(“0”))) {

return true;

} else

return false;

}

public int intValue() {

return (int) doubleValue();

}

public float floatValue() {

return (float) doubleValue();

}

public double doubleValue() {

return numerator.divide(denominator).doubleValue();

}

public long longValue() {

return (long) doubleValue();

}

@Override

public int compareTo(Rational o) {

if (this.subtract(o).getNumerator().compareTo(new BigInteger(“0”)) > 0)

return 1;

else if (this.subtract(o).getNumerator().compareTo(new BigInteger(“0”)) < 0)

return -1;

else

return 0;

}

}

package dishisanzhang;

import java.math.BigInteger;

public class dishisanzhang {

public static void main(String[] args) throws CloneNotSupportedException {

Rational sum = new Rational();

Rational s = null;

for (int i = 1; i < 100; i++) {

s = new Rational(new BigInteger(new Integer(i)+“”),

new BigInteger(new Integer(i + 1)+“”));

sum = sum.add(s);

System.out.println(100 - i);

}

//慢慢等吧,等到输出0,你就看到结果了哈哈!!!

System.out.println(sum);

}

}

13.19

==================================================================

方向好像写错了,结果对!,想改改的自己改改就行。

package dishisanzhang;

import java.math.BigInteger;

public class Rational extends Number implements Comparable< Rational > {

private BigInteger numerator = new BigInteger(“0”);

private BigInteger denominator = new BigInteger(“1”);

public Rational() {

this(new BigInteger(“0”), new BigInteger(“1”));

}

public Rational(BigInteger numerator, BigInteger denominator) {

BigInteger gcd = gcd(numerator, denominator);

if (denominator.compareTo(new BigInteger(“0”)) > 0) {

this.numerator = numerator.divide(gcd);

} else

this.numerator = numerator.multiply(new BigInteger(“-1”)).divide(gcd);

this.denominator = denominator.abs().divide(gcd);

}

private static BigInteger gcd(BigInteger n, BigInteger d) {

BigInteger n1 = n.abs();

BigInteger n2 = d.abs();

BigInteger gcd = new BigInteger(“1”);

BigInteger n3 = new BigInteger(“1”);

for (int i = 1; i <= n1.intValue() && i <= n2.intValue(); i++) {

if ((n1.remainder(n3).equals(new BigInteger(“0”))) && (n2.remainder(n3).equals(new BigInteger(“0”)))) {

gcd = n3;

}

n3 = n3.add(new BigInteger(“1”));

}

return gcd;

}

public BigInteger getNumerator() {

return numerator;

}

public BigInteger getDenominator() {

return denominator;

}

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);

}

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);

}

public Rational multiply(Rational secondRational) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

自学几个月前端,为什么感觉什么都没学到??


这种现象在很多的初学者和自学前端的同学中是比较的常见的。

因为自学走的弯路是比较的多的,会踩很多的坑,学习的过程中是比较的迷茫的。

最重要的是,在学习的过程中,不知道每个部分该学哪些知识点,学到什么程度才算好,学了能做什么。

很多自学的朋友往往都是自己去找资料学习的,资料上有的或许就学到了,资料上没有的或许就没有学到。

这就会给人一个错误的信息就是,我把资料上的学完了,估计也-就差不多的了。

但是真的是这样的吗?非也,因为很多人找的资料就是很基础的。学完了也就是掌握一点基础的东西。分享给你一份前端分析路线,你可以参考。

还有很多的同学在学习的过程中一味的追求学的速度,很快速的刷视频,写了后面忘了前面,最后什么都没有学到,什么都知道,但是什么都不懂,要具体说,也说不出个所以然。

所以学习编程一定要注重实践操作,练习敲代码的时间一定要多余看视频的时间。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

g-pCbsIkKL-1712495294636)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
[外链图片转存中…(img-mdY3wwP9-1712495294637)]

自学几个月前端,为什么感觉什么都没学到??


这种现象在很多的初学者和自学前端的同学中是比较的常见的。

因为自学走的弯路是比较的多的,会踩很多的坑,学习的过程中是比较的迷茫的。

最重要的是,在学习的过程中,不知道每个部分该学哪些知识点,学到什么程度才算好,学了能做什么。

很多自学的朋友往往都是自己去找资料学习的,资料上有的或许就学到了,资料上没有的或许就没有学到。

这就会给人一个错误的信息就是,我把资料上的学完了,估计也-就差不多的了。

但是真的是这样的吗?非也,因为很多人找的资料就是很基础的。学完了也就是掌握一点基础的东西。分享给你一份前端分析路线,你可以参考。

还有很多的同学在学习的过程中一味的追求学的速度,很快速的刷视频,写了后面忘了前面,最后什么都没有学到,什么都知道,但是什么都不懂,要具体说,也说不出个所以然。

所以学习编程一定要注重实践操作,练习敲代码的时间一定要多余看视频的时间。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-uSFGvuYo-1712495294637)]

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值