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
}
}
==================================================================
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()));
}
}
}
==================================================================
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
}
}
==================================================================
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);
}
}
==================================================================
方向好像写错了,结果对!,想改改的自己改改就行。
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) {
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) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
String str = input.next();
boolean bl = true;
if (str.charAt(0) == ‘-’)
bl = false;
String[] s = new String[2];
s = str.split(“\.”);
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int length = s[1].length();
int numerator1 = (int) (a * Math.pow(10, length) + b);
int denominator1 = (int) Math.pow(10, length);
int greatestCommonDivisor = maxCommonDivisor(numerator1, denominator1);
int numrator2 = numerator1 / greatestCommonDivisor;
int denominator2 = denominator1 / greatestCommonDivisor;
Rational r1 = new Rational(new BigInteger(numrator2 + “”), new BigInteger(denominator2 + “”));
if (bl)
System.out.println("The fraction number is " + r1);
else
System.out.println(“The fraction number is -” + r1);
}
public static int maxCommonDivisor(int m, int n) {
if (m < n) {
int temp = m;
m = n;
n = temp;
}
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
return n;
}
}
==================================================================
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) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print(“Enter a, b, c:”);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double delta = b * b - 4 * a * c;
if (delta < 0) {
double real1 = -1 * b / 2 / a;
double imaginary1 = Math.pow(-delta, 0.5) / 2 / a;
Complex complex1 = new Complex(real1, imaginary1);
double real2 = -1 * b / 2 / a;
double imaginary2 = -Math.pow(-delta, 0.5) / 2 / a;
Complex complex2 = new Complex(real2, imaginary2);
System.out.println("The roots are " + complex1 + " and " + complex2);
} else if (delta == 0)
System.out.println("The root is " + (-1 * b / 2 / a));
else {
double r1 = (-1 * b + Math.pow(delta, 0.5)) / 2 / a;
double r2 = (-1 * b - Math.pow(delta, 0.5)) / 2 / a;
System.out.println("The roots are " + r1 + " and " + r2);
}
}
}
==================================================================
package dishisanzhang;
import java.math.BigInteger;
public class Rational extends Number implements Comparable< Rational >{
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
real2 = -1 * b / 2 / a;
double imaginary2 = -Math.pow(-delta, 0.5) / 2 / a;
Complex complex2 = new Complex(real2, imaginary2);
System.out.println("The roots are " + complex1 + " and " + complex2);
} else if (delta == 0)
System.out.println("The root is " + (-1 * b / 2 / a));
else {
double r1 = (-1 * b + Math.pow(delta, 0.5)) / 2 / a;
double r2 = (-1 * b - Math.pow(delta, 0.5)) / 2 / a;
System.out.println("The roots are " + r1 + " and " + r2);
}
}
}
==================================================================
package dishisanzhang;
import java.math.BigInteger;
public class Rational extends Number implements Comparable< Rational >{
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-09D8GkJE-1715776631876)]
[外链图片转存中…(img-1PE5Ov8v-1715776631876)]
[外链图片转存中…(img-Xczvfxfw-1715776631877)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!