示例#1
0
 //Overloading operators in next lines
 public static Fraction operator +(Fraction a, Fraction b)
 {
     Fraction result = new Fraction();
     result.Numerator = (a.Numerator * b.Denominator)
     + (a.Denominator * b.Numerator);
     result.Denominator = a.Denominator * b.Denominator;
     result.Normalize();
     return result;
 }
示例#2
0
 public static Fraction operator -(Fraction a, Fraction b)
 {
     Fraction result = new Fraction();
     if (a.Numerator > b.Denominator)
     {
         result.Numerator = (a.Numerator * b.Denominator) - (a.Denominator * b.Numerator);
         result.Denominator = a.Denominator * b.Denominator;
     }
     else
     {
         result.Numerator = (a.Denominator * b.Numerator) - (a.Numerator * b.Denominator);
         result.Denominator = a.Denominator * b.Denominator;
     }
     result.Normalize();
     return result;
 }
示例#3
0
 static void Main(string[] args)
 {
     string action = string.Empty;
     string fractionA = string.Empty;
     string fractionB = string.Empty;
     do
     {
         Console.WriteLine("Enter +, -, * or / (q to quit)");
         action = Console.ReadLine();
         if (!(action == "q" || action == "Q"))
         {
             Console.WriteLine("Enter the first fraction (example: 1/2):");
             fractionA = Console.ReadLine();
             Console.WriteLine("Enter the second fraction (example: 5/9):");
             fractionB = Console.ReadLine();
             Fraction A = new Fraction(fractionA);
             Fraction B = new Fraction(fractionB);
             Fraction result = new Fraction();
             switch (action)
             {
                 case "+":
                     result = A + B;
                     break;
                 case "-":
                     result = A - B;
                     break;
                 case "*":
                     result = A * B;
                     break;
                 case "/":
                     result = A / B;
                     break;
             }
             Console.WriteLine("The result is: " + result);
         }
     }
     while (!(action == "q" || action == "Q"));
 }