如果一个分数不能简化,我将如何制作一个新的分数?
How would I make a new fraction if a fraction cannot be simplified?
所以我正在为我的 OOP class 编写作业,要求用户提供一些分数,然后随机生成该数量的分数。作业的一部分指出,如果无法简化其中一个分数,则跳过它并创建一个可以简化的新分数。然而,不可还原的分数仍然可以通过。如果分数的 GCD 为 1,我需要一种方法让程序生成一个新分数。
代码:
import java.util.Random;
// The class is called boxes because I'm supposed to print boxes around the fractions, but I'll
implement it later.
class SimpleFracBoxes{
private int numberOfFractions = 0;
private String result = "";
Random myRandom = new Random( );
int min = 1;
int max = 100000;
public SimpleFracBoxes( )
{
}
public SimpleFracBoxes(int nOFracs)
{
numberOfFractions = nOFracs;
}
// makes the fractions
public void makeTheFractions()
{
for (int i = 0; i < numberOfFractions; i++)
{
Fraction frac = getAGoodFraction();
Fraction frac2 = frac.simplify();
result = result + " "+ (int)(i + 1) + ": " + frac + " simplifies to "+ frac2 + "\n" ;
}
}
// creates a fraction that can be divided
private Fraction getAGoodFraction()
{
Fraction frac;
do {
frac = new Fraction (myRandom.nextInt(max-min), myRandom.nextInt(max-min));
}
while (frac.getNumerator() >= frac.getDenominator());
int num = frac.getNumerator();
int denom = frac.getDenominator();
int gcd = frac.gcd(num, denom);
if (gcd == 1) {
getAGoodFraction();
} else if (gcd !=1) {
frac.simplify();
}
return frac;
}
public String getFractions()
{
return result;
}
}
分数Class代码
public class Fraction{
private int numerator;
private int denominator;
public Fraction(){
this(0,1);
}
public Fraction(int number) {
this(number,1);
}
public Fraction(Fraction frac) {
this(frac.getNumerator(), frac.getDenominator());
}
public Fraction(int num, int denom){
setNumerator(num);
setDenominator(denom);
}
public static int gcd(int m, int n) {
int r = n % m;
while (r != 0) {
n = m;
m = r;
r = n % m;
}
return m;
}
public static Fraction min(Fraction f1, Fraction f2) {
double f1_dec = f1.decimal();
double f2_dec = f2.decimal();
if (f1_dec <= f2_dec) {
return f1;
} else {
return f2;
}
}
public Fraction add(Fraction frac) {
int a, b, c, d;
Fraction sum;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
sum = new Fraction(a*d + b*c, b*d);
return sum;
}
public Fraction add(int number) {
Fraction frac = new Fraction(number, 1);
Fraction sum = add(frac);
return sum;
}
public Fraction divide(Fraction frac) {
int a, b, c, d;
Fraction quotient;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
quotient = new Fraction(a*d, b*c);
return quotient;
}
public Fraction divide(int number) {
Fraction frac = new Fraction(number, 1);
Fraction quotient = divide(frac);
return quotient;
}
public boolean equals(Fraction frac) {
Fraction f1 = simplify();
Fraction f2 = frac.simplify();
if (f1.getNumerator() == f2.getNumerator() &&
f1.getDenominator() == f2.getDenominator()) {
return true;
} else {
return false;
}
}
public int getDenominator() {
return denominator;
}
public int getNumerator(){
return numerator;
}
public Fraction multiply(Fraction frac){
int a, b, c, d;
Fraction product;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
product = new Fraction(a*c, b*d);
return product;
}
public Fraction multiply(int number){
Fraction frac = new Fraction(number, 1);
Fraction product = multiply(frac);
return product;
}
public void setDenominator(int denom){
if (denom == 0) {
System.err.println("Fatal Error");
System.exit(1);
}
denominator = denom;
}
public void setNumerator(int num) {
numerator = num;
}
public Fraction simplify(){
int num = getNumerator();
int denom = getDenominator();
int gcd = gcd(num, denom);
Fraction simp = new Fraction(num/gcd, denom/gcd);
return simp;
}
public Fraction subtract(Fraction frac) {
int a, b, c, d;
Fraction diff;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
diff = new Fraction(a*d - b*c, b*d);
return diff;
}
public Fraction subtract(int number) {
Fraction frac = new Fraction(number, 1);
Fraction difference = subtract(frac);
return difference;
}
public String toString() {
return getNumerator() + "/" + getDenominator();
}
private double decimal() {
return (double) getNumerator() / getDenominator();
}
}
Output:
How many fractions? 3
1: 28181/38503 simplifies to 28181/38503 // Unsimplified
2: 75654/99570 simplifies to 12609/16595
3: 787/31255 simplifies to 787/31255 // Unsimplified
听起来你需要一个分数比较功能。
// returns true if two fractions are identical
public boolean identical(Fraction frac) ...
那么你可以这样写一个 if 语句
if (!frac.identical(frac.reduce())) {
... print fraction ...
}
此外,我会考虑将分数缩减逻辑移到分数中,因为这样它可以在将来重复使用而无需复制逻辑。通过将它放在“分数的用户”中,您将拥有一个“数据结构”,其中“相关逻辑”位于其他地方。 class 最基本的定义是“具有密切相关逻辑的数据”,减少分数与分数密切相关,但与在数据库中存储分数关系不大。
- class
Fraction
中的方法decimal()
没有用到,所以去掉了
- 不要在 GCD 值为 1 时递归调用方法
getAGoodFraction()
,而是使用循环。
- 与其在每次随机生成的分子大于随机生成的分母时都创建一个新的
Fraction
对象,不如使用方法 setNumerator()
和 setDenominator()
。这就是他们的目的。
- 方法
getAGoodFraction()
中不需要调用方法simplify()
。只是 return 的分数。因为GCD不等于1,就知道可以化简了。
将以下代码与您的进行比较。
请注意,我向 class Fraction
添加了一个 main()
方法,以便能够 运行 代码。而且我还更改了方法 gcd()
由于 来自@KevinAnderson 的问题。
import java.util.Random;
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
this(0, 1);
}
public Fraction(int number) {
this(number, 1);
}
public Fraction(Fraction frac) {
this(frac.getNumerator(), frac.getDenominator());
}
public Fraction(int num, int denom) {
setNumerator(num);
setDenominator(denom);
}
public static int gcd(int m, int n) {
int factor = m;
int r = n % factor;
while (r != 0 && factor > 1) {
r = n % --factor;
if (r == 0) {
r = m % factor;
}
}
return factor;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public void setDenominator(int denom) {
if (denom == 0) {
System.err.println("Fatal Error");
System.exit(1);
}
denominator = denom;
}
public void setNumerator(int num) {
numerator = num;
}
public Fraction simplify() {
int num = getNumerator();
int denom = getDenominator();
int gcd = gcd(num, denom);
Fraction simp = new Fraction(num / gcd, denom / gcd);
return simp;
}
public String toString() {
return getNumerator() + "/" + getDenominator();
}
public static void main(String[] args) {
SimpleFracBoxes sfb = new SimpleFracBoxes(10);
sfb.makeTheFractions();
System.out.println(sfb.getFractions());
}
}
class SimpleFracBoxes {
private int numberOfFractions = 0;
private String result = "";
Random myRandom = new Random();
int min = 1;
int max = 100000;
public SimpleFracBoxes() {
}
public SimpleFracBoxes(int nOFracs) {
numberOfFractions = nOFracs;
}
//makes the fractions
public void makeTheFractions() {
for (int i = 0; i < numberOfFractions; i++) {
Fraction frac = getAGoodFraction();
Fraction frac2 = frac.simplify();
result += String.format("%" + String.valueOf(numberOfFractions).length() + "d. %5d/%5d simplifies to %5d/%5d%n",
(i + 1),
frac.getNumerator(),
frac.getDenominator(),
frac2.getNumerator(),
frac2.getDenominator());
}
}
// creates a fraction that can be divided
private Fraction getAGoodFraction() {
int gcd = 1;
Fraction frac = new Fraction();
while (gcd == 1) {
do {
frac.setNumerator(myRandom.nextInt(max - min));
frac.setDenominator(myRandom.nextInt(max - min));
} while (frac.getNumerator() >= frac.getDenominator());
int num = frac.getNumerator();
int denom = frac.getDenominator();
gcd = Fraction.gcd(num, denom);
}
return frac;
}
public String getFractions() {
return result;
}
}
这里是 运行 上面代码生成的示例输出。
1. 64480/84728 simplifies to 8060/10591
2. 33376/79317 simplifies to 4768/11331
3. 50944/97026 simplifies to 25472/48513
4. 21339/45510 simplifies to 7113/15170
5. 35884/38628 simplifies to 8971/ 9657
6. 15148/17199 simplifies to 2164/ 2457
7. 72670/95005 simplifies to 14534/19001
8. 19810/44730 simplifies to 283/ 639
9. 61790/63956 simplifies to 30895/31978
10. 4824/ 5352 simplifies to 201/ 223
所以我正在为我的 OOP class 编写作业,要求用户提供一些分数,然后随机生成该数量的分数。作业的一部分指出,如果无法简化其中一个分数,则跳过它并创建一个可以简化的新分数。然而,不可还原的分数仍然可以通过。如果分数的 GCD 为 1,我需要一种方法让程序生成一个新分数。
代码:
import java.util.Random;
// The class is called boxes because I'm supposed to print boxes around the fractions, but I'll
implement it later.
class SimpleFracBoxes{
private int numberOfFractions = 0;
private String result = "";
Random myRandom = new Random( );
int min = 1;
int max = 100000;
public SimpleFracBoxes( )
{
}
public SimpleFracBoxes(int nOFracs)
{
numberOfFractions = nOFracs;
}
// makes the fractions
public void makeTheFractions()
{
for (int i = 0; i < numberOfFractions; i++)
{
Fraction frac = getAGoodFraction();
Fraction frac2 = frac.simplify();
result = result + " "+ (int)(i + 1) + ": " + frac + " simplifies to "+ frac2 + "\n" ;
}
}
// creates a fraction that can be divided
private Fraction getAGoodFraction()
{
Fraction frac;
do {
frac = new Fraction (myRandom.nextInt(max-min), myRandom.nextInt(max-min));
}
while (frac.getNumerator() >= frac.getDenominator());
int num = frac.getNumerator();
int denom = frac.getDenominator();
int gcd = frac.gcd(num, denom);
if (gcd == 1) {
getAGoodFraction();
} else if (gcd !=1) {
frac.simplify();
}
return frac;
}
public String getFractions()
{
return result;
}
}
分数Class代码
public class Fraction{
private int numerator;
private int denominator;
public Fraction(){
this(0,1);
}
public Fraction(int number) {
this(number,1);
}
public Fraction(Fraction frac) {
this(frac.getNumerator(), frac.getDenominator());
}
public Fraction(int num, int denom){
setNumerator(num);
setDenominator(denom);
}
public static int gcd(int m, int n) {
int r = n % m;
while (r != 0) {
n = m;
m = r;
r = n % m;
}
return m;
}
public static Fraction min(Fraction f1, Fraction f2) {
double f1_dec = f1.decimal();
double f2_dec = f2.decimal();
if (f1_dec <= f2_dec) {
return f1;
} else {
return f2;
}
}
public Fraction add(Fraction frac) {
int a, b, c, d;
Fraction sum;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
sum = new Fraction(a*d + b*c, b*d);
return sum;
}
public Fraction add(int number) {
Fraction frac = new Fraction(number, 1);
Fraction sum = add(frac);
return sum;
}
public Fraction divide(Fraction frac) {
int a, b, c, d;
Fraction quotient;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
quotient = new Fraction(a*d, b*c);
return quotient;
}
public Fraction divide(int number) {
Fraction frac = new Fraction(number, 1);
Fraction quotient = divide(frac);
return quotient;
}
public boolean equals(Fraction frac) {
Fraction f1 = simplify();
Fraction f2 = frac.simplify();
if (f1.getNumerator() == f2.getNumerator() &&
f1.getDenominator() == f2.getDenominator()) {
return true;
} else {
return false;
}
}
public int getDenominator() {
return denominator;
}
public int getNumerator(){
return numerator;
}
public Fraction multiply(Fraction frac){
int a, b, c, d;
Fraction product;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
product = new Fraction(a*c, b*d);
return product;
}
public Fraction multiply(int number){
Fraction frac = new Fraction(number, 1);
Fraction product = multiply(frac);
return product;
}
public void setDenominator(int denom){
if (denom == 0) {
System.err.println("Fatal Error");
System.exit(1);
}
denominator = denom;
}
public void setNumerator(int num) {
numerator = num;
}
public Fraction simplify(){
int num = getNumerator();
int denom = getDenominator();
int gcd = gcd(num, denom);
Fraction simp = new Fraction(num/gcd, denom/gcd);
return simp;
}
public Fraction subtract(Fraction frac) {
int a, b, c, d;
Fraction diff;
a = this.getNumerator();
b = this.getDenominator();
c = frac.getNumerator();
d = frac.getDenominator();
diff = new Fraction(a*d - b*c, b*d);
return diff;
}
public Fraction subtract(int number) {
Fraction frac = new Fraction(number, 1);
Fraction difference = subtract(frac);
return difference;
}
public String toString() {
return getNumerator() + "/" + getDenominator();
}
private double decimal() {
return (double) getNumerator() / getDenominator();
}
}
Output:
How many fractions? 3
1: 28181/38503 simplifies to 28181/38503 // Unsimplified
2: 75654/99570 simplifies to 12609/16595
3: 787/31255 simplifies to 787/31255 // Unsimplified
听起来你需要一个分数比较功能。
// returns true if two fractions are identical
public boolean identical(Fraction frac) ...
那么你可以这样写一个 if 语句
if (!frac.identical(frac.reduce())) {
... print fraction ...
}
此外,我会考虑将分数缩减逻辑移到分数中,因为这样它可以在将来重复使用而无需复制逻辑。通过将它放在“分数的用户”中,您将拥有一个“数据结构”,其中“相关逻辑”位于其他地方。 class 最基本的定义是“具有密切相关逻辑的数据”,减少分数与分数密切相关,但与在数据库中存储分数关系不大。
- class
Fraction
中的方法decimal()
没有用到,所以去掉了 - 不要在 GCD 值为 1 时递归调用方法
getAGoodFraction()
,而是使用循环。 - 与其在每次随机生成的分子大于随机生成的分母时都创建一个新的
Fraction
对象,不如使用方法setNumerator()
和setDenominator()
。这就是他们的目的。 - 方法
getAGoodFraction()
中不需要调用方法simplify()
。只是 return 的分数。因为GCD不等于1,就知道可以化简了。
将以下代码与您的进行比较。
请注意,我向 class Fraction
添加了一个 main()
方法,以便能够 运行 代码。而且我还更改了方法 gcd()
由于
import java.util.Random;
public class Fraction {
private int numerator;
private int denominator;
public Fraction() {
this(0, 1);
}
public Fraction(int number) {
this(number, 1);
}
public Fraction(Fraction frac) {
this(frac.getNumerator(), frac.getDenominator());
}
public Fraction(int num, int denom) {
setNumerator(num);
setDenominator(denom);
}
public static int gcd(int m, int n) {
int factor = m;
int r = n % factor;
while (r != 0 && factor > 1) {
r = n % --factor;
if (r == 0) {
r = m % factor;
}
}
return factor;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public void setDenominator(int denom) {
if (denom == 0) {
System.err.println("Fatal Error");
System.exit(1);
}
denominator = denom;
}
public void setNumerator(int num) {
numerator = num;
}
public Fraction simplify() {
int num = getNumerator();
int denom = getDenominator();
int gcd = gcd(num, denom);
Fraction simp = new Fraction(num / gcd, denom / gcd);
return simp;
}
public String toString() {
return getNumerator() + "/" + getDenominator();
}
public static void main(String[] args) {
SimpleFracBoxes sfb = new SimpleFracBoxes(10);
sfb.makeTheFractions();
System.out.println(sfb.getFractions());
}
}
class SimpleFracBoxes {
private int numberOfFractions = 0;
private String result = "";
Random myRandom = new Random();
int min = 1;
int max = 100000;
public SimpleFracBoxes() {
}
public SimpleFracBoxes(int nOFracs) {
numberOfFractions = nOFracs;
}
//makes the fractions
public void makeTheFractions() {
for (int i = 0; i < numberOfFractions; i++) {
Fraction frac = getAGoodFraction();
Fraction frac2 = frac.simplify();
result += String.format("%" + String.valueOf(numberOfFractions).length() + "d. %5d/%5d simplifies to %5d/%5d%n",
(i + 1),
frac.getNumerator(),
frac.getDenominator(),
frac2.getNumerator(),
frac2.getDenominator());
}
}
// creates a fraction that can be divided
private Fraction getAGoodFraction() {
int gcd = 1;
Fraction frac = new Fraction();
while (gcd == 1) {
do {
frac.setNumerator(myRandom.nextInt(max - min));
frac.setDenominator(myRandom.nextInt(max - min));
} while (frac.getNumerator() >= frac.getDenominator());
int num = frac.getNumerator();
int denom = frac.getDenominator();
gcd = Fraction.gcd(num, denom);
}
return frac;
}
public String getFractions() {
return result;
}
}
这里是 运行 上面代码生成的示例输出。
1. 64480/84728 simplifies to 8060/10591
2. 33376/79317 simplifies to 4768/11331
3. 50944/97026 simplifies to 25472/48513
4. 21339/45510 simplifies to 7113/15170
5. 35884/38628 simplifies to 8971/ 9657
6. 15148/17199 simplifies to 2164/ 2457
7. 72670/95005 simplifies to 14534/19001
8. 19810/44730 simplifies to 283/ 639
9. 61790/63956 simplifies to 30895/31978
10. 4824/ 5352 simplifies to 201/ 223