我正在尝试制作一个程序,它需要两个分数,然后对它们进行加、减、乘和除,然后在必要时将它们以最简单的形式放置。
我遇到的唯一问题是分数没有简化,我不知道为什么。
例如,现在当我运行分数 3/4 和 1/4 时,它给了我以下答案: 加法:4/4 减法:2/4 乘法:3/16 除法:12/4
我希望加法是 1/1,减法是 1/2,除法是 3/1。下面是我的代码。
import java.util.*;
public class RationalNumber
{
private int numerator;
private int denominator;//instance variables
//constructor
public RationalNumber()
{
numerator = 0;
denominator = 1;
}
public RationalNumber(int num, int den)
{
setNumerator(num);
setDenominator(den);
int gcf= greatestCommonFactor(num, den);
num = num/gcf;
den = den/gcf;
/*num = num/(greatestCommonFactor(num, den));
den = den/(greatestCommonFactor(num, den));*/
}
//getters
public int getDenominator()
{
return denominator;
}
public int getNumerator()
{
return numerator;
}
//setters
public void setNumerator(int num)
{
numerator = num;
}
public void setDenominator(int den)
{
denominator = den;
}
//adds the rational numbers
public RationalNumber add(RationalNumber two)
{
RationalNumber add = new RationalNumber();
if(getDenominator() == two.getDenominator())
{
int newNum = numerator + two.numerator;//simply adds the numerators if the denominators are the same
/*newNum = newNum/(greatestCommonFactor(newNum, denominator));
int newDen = denominator/(greatestCommonFactor(newNum, denominator));//simplification*/
return new RationalNumber(newNum, denominator);
}
else
{
int newDen = getDenominator() * two.getDenominator();
int leftNum = getNumerator() * two.getDenominator();
int rightNum = getDenominator() * two.getNumerator();//cross multiplication
/*int newNum = (leftNum + rightNum)/(greatestCommonFactor((leftNum + rightNum), getDenominator()));//simplification
newDen = denominator/(greatestCommonFactor(newNum, getDenominator()));*/
return new RationalNumber(leftNum + rightNum, newDen);
}
}
//subtracts the rational numbers, same math as the add method but with subtraction signs
public RationalNumber sub(RationalNumber two)
{
RationalNumber sub = new RationalNumber();
if(this.getDenominator() == two.getDenominator())
{
int newNum = this.numerator - two.numerator;
return new RationalNumber(newNum, this.getDenominator());
}
else
{
int newDen = this.getDenominator() * two.getDenominator();
int leftNum = this.getNumerator() * two.getDenominator();
int rightNum = this.getDenominator() * two.getNumerator();
return new RationalNumber(leftNum - rightNum, newDen);
}
}
//multiplies the two rational numbers
public RationalNumber mult(RationalNumber two)
{
RationalNumber mult = new RationalNumber();
mult.denominator = denominator * two.denominator;
mult.numerator = numerator * two.numerator;
return mult;
}
//divides the rational numbers
public RationalNumber div(RationalNumber two)
{
RationalNumber div = new RationalNumber();
div.denominator = denominator * (two.numerator);
div.numerator = numerator * two.denominator;//multiply by reciprocal in order to divide
return div;
}
//returns the reciprocal of a rational number
public RationalNumber reciprocal(RationalNumber two)
{
RationalNumber reciprocal = new RationalNumber();
reciprocal.denominator = numerator;
reciprocal.numerator = denominator;
return reciprocal;
}
//returns the inverse of a rational number
public RationalNumber inverse(RationalNumber two)
{
RationalNumber inverse = new RationalNumber();
inverse.numerator = (numerator*-1);
return inverse;
}
//finds the greatest common factor, used in simplifying
public int greatestCommonFactor(int num, int den)
{
for(int i = (Math.max(num,den)/2)+1; i > 0; i--)
{
if((num/i) == Math.round(num/i) && (den/i) == Math.round(den/i))
return i;//loop finds the GCF by finding if numerator & denominator divided by i is an even number
}
return 1;
}
public String toString()
{
return numerator + "/" + denominator;//prints the numerator and denominator as a string
}
}