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

该博客展示了如何使用Java实现复数和有理数的加减乘除运算,包括大整数的欧几里得算法求最大公约数。还提供了将十进制数转换为有理数的示例,并演示了如何计算一元二次方程的根。此外,还包含了计算复数绝对值的功能。
摘要由CSDN通过智能技术生成

13.15

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

}

}

13.20

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

}

13.21

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,b,c: ");
	float a1 = input.nextInt();
	float b1 = input.nextInt();
	float c1 = input.nextInt();

	float h = (-1 * b1) / (2 * a1);
	float k = (4 * a1 * c1 - b1 * b1) / 4 / a1;

	String str = h + "";
	String str1 = k + "";
	System.out.print("h is " + getJieguo(str));
	System.out.print(" k is " + getJieguo(str1));
}

public static String getJieguo(String str) {
	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)
		return "" + r1;
	else
		return "-" + 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;

}

}

第十三章习题 完

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值