Advertisement
Layvu

Class Fraction

Dec 21st, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Fraction
  5. {
  6.     public class Fraction : IComparable
  7.     {
  8.         public Fraction(double numerator, double denominator)
  9.         {
  10.             _numerator = numerator;
  11.             if (denominator == 0) throw new ArgumentException();
  12.             _denominator = denominator;
  13.         }
  14.  
  15.         public double _numerator { get; private set; }
  16.  
  17.         public double _denominator { get; private set; }
  18.  
  19.         public double Value { get { return _numerator / _denominator; } }
  20.  
  21.         public void Write()
  22.         {
  23.             Console.WriteLine("answer: {0} / {1} = {2}", _numerator, _denominator, Value);
  24.         }        
  25.  
  26.         public Fraction ReduceFraction()
  27.         {
  28.             var gcd = FindTheGreatestCommonDivisor(_numerator, _denominator);
  29.             _numerator /= gcd;
  30.             _denominator /= gcd;
  31.             return new Fraction(_numerator, _denominator);
  32.         }
  33.  
  34.         public double FindTheGreatestCommonDivisor(double firstNumb, double secondNumb)
  35.         {
  36.             firstNumb = Math.Abs(firstNumb);
  37.             secondNumb = Math.Abs(secondNumb);
  38.             while (firstNumb != secondNumb)
  39.             {
  40.                 if (firstNumb > secondNumb)
  41.                     (firstNumb, secondNumb) = (secondNumb, firstNumb);
  42.                 secondNumb -= firstNumb;
  43.             }
  44.             return firstNumb;
  45.         }
  46.  
  47.         public Fraction Sum(Fraction fraction)
  48.         {
  49.             double firstDen = _denominator, secondDen = fraction._denominator;
  50.             double firstNum = _numerator, secondNum = fraction._numerator;
  51.             double finalDen, finalNum;
  52.             Fraction result;
  53.  
  54.             var gcd = FindTheGreatestCommonDivisor(_denominator, fraction._denominator);
  55.  
  56.             if (firstDen == secondDen)
  57.             {
  58.                 finalNum = firstNum + secondNum;
  59.                 finalDen = firstDen;
  60.                 result = new Fraction(finalNum, finalDen).ReduceFraction();
  61.             }
  62.             else if (gcd == Math.Min(firstDen, secondDen))
  63.             {
  64.                 var factor = Math.Max(firstDen, secondDen) / gcd;
  65.                 finalNum = (firstDen < secondDen ? firstNum * factor + secondNum : secondNum * factor + firstNum);
  66.                 finalDen = Math.Max(firstDen, secondDen);
  67.                 result = new Fraction(finalNum, finalDen).ReduceFraction();
  68.             }
  69.             else
  70.             {
  71.                 finalNum = firstNum * secondDen + secondNum * firstDen;
  72.                 finalDen = firstDen * secondDen;
  73.                 result = new Fraction(finalNum, finalDen).ReduceFraction();
  74.             }
  75.             return result;
  76.         }
  77.  
  78.         public Fraction Subtraction(Fraction fraction)
  79.         {
  80.             return Sum(new Fraction(-fraction._numerator, fraction._denominator));
  81.         }
  82.  
  83.         public Fraction Multipy(Fraction fraction)
  84.         {
  85.             var numerator = _numerator * fraction._numerator;
  86.             var denominator = _denominator * fraction._denominator;
  87.             var result = new Fraction(numerator, denominator).ReduceFraction();
  88.             return result;
  89.         }
  90.  
  91.         public Fraction Divide(Fraction fraction)
  92.         {
  93.             return Multipy(new Fraction(fraction._denominator, fraction._numerator));
  94.         }
  95.  
  96.         public int CompareTo(object obj)
  97.         {
  98.             var fraction = (Fraction)obj;
  99.  
  100.             if (_denominator != fraction._denominator)
  101.             {
  102.                 _numerator *= fraction._denominator;
  103.                 fraction._numerator *= _denominator;
  104.             }
  105.  
  106.             if (_numerator < fraction._numerator)
  107.                 return -1;
  108.             if (_numerator > fraction._numerator)
  109.                 return 1;
  110.             return 0;
  111.         }
  112.     }
  113.  
  114.     internal class Program
  115.     {
  116.         static void Main()
  117.         {
  118.             Console.Write("first numerator: ");
  119.             var firstNumerator = int.Parse(Console.ReadLine());
  120.             Console.Write("\nfirst denominator: ");
  121.             var firstDenominator = int.Parse(Console.ReadLine());
  122.  
  123.             Console.Write("\nsecond numerator: ");
  124.             var secondNumerator = int.Parse(Console.ReadLine());
  125.             Console.Write("\nsecond denominator: ");
  126.             var secondDenominator = int.Parse(Console.ReadLine());
  127.  
  128.             var firstFract = new Fraction(firstNumerator, firstDenominator);
  129.             var secondFract = new Fraction(secondNumerator, secondDenominator);
  130.  
  131.             //firstFract.Sum(secondFract).Write();
  132.  
  133.             //firstFract.Subtraction(secondFract).Write();
  134.  
  135.             //firstFract.Multipy(secondFract).Write();
  136.  
  137.             //firstFract.Divide(secondFract).Write();
  138.  
  139.             //Console.WriteLine(firstFract.CompareTo(secondFract));
  140.  
  141.             //firstFract.ReduceFraction().Write();
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement