问题是表达式1/4 + 2 1/2
会显示2 3/8
而不是2 3/4
的结果。表达式1/8 + 2 1/2
也是如此,结果也显示2 3/8
。请帮我解决一下这个。谢谢!
class FractionDemo
{
static void Main(string[] args)
{
Fraction firstfraction = new Fraction();
Fraction secondfraction = new Fraction();
firstfraction.Numerator = 1;
firstfraction.Denominator = 4;
secondfraction.Numerator = 1;
secondfraction.Denominator = 8;
secondfraction.WholeNumber = 2;
Fraction add = new Fraction();
add = firstfraction + secondfraction;
Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
Console.WriteLine(" + {0}/{1} = {2}/{3}", secondfraction.Numerator, secondfraction.Denominator, add.Numerator, add.Denominator);
Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
Console.Write("\n {0}/{1}", firstfraction.Numerator, secondfraction.Denominator);
Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
Console.ReadLine();
}
public class Fraction
{
private int wholenumber;
private int numerator;
private int denominator;
public int WholeNumber
{
get
{
return wholenumber;
}
set
{
wholenumber = value;
}
}
public int Numerator
{
get
{
return numerator;
}
set
{
numerator = value;
}
}
public int Denominator
{
get
{
return denominator;
}
set
{
denominator = value;
if (denominator > 0)
{
denominator = value;
}
else
{
denominator = 1;
}
}
}
public Fraction(int wholenumber, int numerator, int denominator)
: this(numerator, denominator)
{
WholeNumber = wholenumber;
}
public Fraction(int numerator, int denominator)
{
WholeNumber = 0;
Numerator = numerator;
Denominator = denominator;
}
public Fraction()
{
WholeNumber = 0;
Numerator = 0;
Denominator = 1;
}
public int gcd()
{
int x = Numerator;
int y = Denominator;
int m;
if (x > y)
m = y;
else
m = x;
for (int i = m; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
{
return i;
}
}
return 1;
}
public void Reduce()
{
int gcdNum = gcd();
if (gcdNum != 0)
{
Numerator = Numerator / gcdNum;
Denominator = Denominator / gcdNum;
}
if (Denominator < 0)
{
Denominator = Denominator * -1;
Numerator = Numerator * -1;
}
convertFraction();
}
public void convertFraction()
{
WholeNumber = Numerator / Denominator;
Numerator = Numerator % Denominator;
}
public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
{
int firstNum = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
int secondNum = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
Fraction Result = new Fraction();
Result.Numerator = firstNum * secondfraction.Denominator + firstfraction.Denominator * secondNum;
Result.Denominator = firstfraction.Denominator * secondfraction.Denominator;
Result.Reduce();
return Result;
}
}
}
答案 0 :(得分:0)
你的分数是正确的。对于第一个示例,您应该使用:
firstfraction.Numerator = 1;
firstfraction.Denominator = 4;
secondfraction.Numerator = 1;
secondfraction.Denominator = 2;
secondfraction.WholeNumber = 2;
然后你会得到2 3/4