2013-12-15 127 views
-1

問題是表達式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; 
     } 
    } 
} 
+1

你加入'2 1/8 + 1/4'。你爲什麼在'2 3/8'時翹首以盼呢? – Rotem

回答

0

你的分數是正確的。對於第一個例子,你應該使用:

  firstfraction.Numerator = 1; 
      firstfraction.Denominator = 4; 
      secondfraction.Numerator = 1; 
      secondfraction.Denominator = 2; 
      secondfraction.WholeNumber = 2; 

你將得到2 3/4