Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Fraction
- {
- public class Fraction : IComparable
- {
- public Fraction(double numerator, double denominator)
- {
- _numerator = numerator;
- if (denominator == 0) throw new ArgumentException();
- _denominator = denominator;
- }
- public double _numerator { get; private set; }
- public double _denominator { get; private set; }
- public double Value { get { return _numerator / _denominator; } }
- public void Write()
- {
- Console.WriteLine("answer: {0} / {1} = {2}", _numerator, _denominator, Value);
- }
- public Fraction ReduceFraction()
- {
- var gcd = FindTheGreatestCommonDivisor(_numerator, _denominator);
- _numerator /= gcd;
- _denominator /= gcd;
- return new Fraction(_numerator, _denominator);
- }
- public double FindTheGreatestCommonDivisor(double firstNumb, double secondNumb)
- {
- firstNumb = Math.Abs(firstNumb);
- secondNumb = Math.Abs(secondNumb);
- while (firstNumb != secondNumb)
- {
- if (firstNumb > secondNumb)
- (firstNumb, secondNumb) = (secondNumb, firstNumb);
- secondNumb -= firstNumb;
- }
- return firstNumb;
- }
- public Fraction Sum(Fraction fraction)
- {
- double firstDen = _denominator, secondDen = fraction._denominator;
- double firstNum = _numerator, secondNum = fraction._numerator;
- double finalDen, finalNum;
- Fraction result;
- var gcd = FindTheGreatestCommonDivisor(_denominator, fraction._denominator);
- if (firstDen == secondDen)
- {
- finalNum = firstNum + secondNum;
- finalDen = firstDen;
- result = new Fraction(finalNum, finalDen).ReduceFraction();
- }
- else if (gcd == Math.Min(firstDen, secondDen))
- {
- var factor = Math.Max(firstDen, secondDen) / gcd;
- finalNum = (firstDen < secondDen ? firstNum * factor + secondNum : secondNum * factor + firstNum);
- finalDen = Math.Max(firstDen, secondDen);
- result = new Fraction(finalNum, finalDen).ReduceFraction();
- }
- else
- {
- finalNum = firstNum * secondDen + secondNum * firstDen;
- finalDen = firstDen * secondDen;
- result = new Fraction(finalNum, finalDen).ReduceFraction();
- }
- return result;
- }
- public Fraction Subtraction(Fraction fraction)
- {
- return Sum(new Fraction(-fraction._numerator, fraction._denominator));
- }
- public Fraction Multipy(Fraction fraction)
- {
- var numerator = _numerator * fraction._numerator;
- var denominator = _denominator * fraction._denominator;
- var result = new Fraction(numerator, denominator).ReduceFraction();
- return result;
- }
- public Fraction Divide(Fraction fraction)
- {
- return Multipy(new Fraction(fraction._denominator, fraction._numerator));
- }
- public int CompareTo(object obj)
- {
- var fraction = (Fraction)obj;
- if (_denominator != fraction._denominator)
- {
- _numerator *= fraction._denominator;
- fraction._numerator *= _denominator;
- }
- if (_numerator < fraction._numerator)
- return -1;
- if (_numerator > fraction._numerator)
- return 1;
- return 0;
- }
- }
- internal class Program
- {
- static void Main()
- {
- Console.Write("first numerator: ");
- var firstNumerator = int.Parse(Console.ReadLine());
- Console.Write("\nfirst denominator: ");
- var firstDenominator = int.Parse(Console.ReadLine());
- Console.Write("\nsecond numerator: ");
- var secondNumerator = int.Parse(Console.ReadLine());
- Console.Write("\nsecond denominator: ");
- var secondDenominator = int.Parse(Console.ReadLine());
- var firstFract = new Fraction(firstNumerator, firstDenominator);
- var secondFract = new Fraction(secondNumerator, secondDenominator);
- //firstFract.Sum(secondFract).Write();
- //firstFract.Subtraction(secondFract).Write();
- //firstFract.Multipy(secondFract).Write();
- //firstFract.Divide(secondFract).Write();
- //Console.WriteLine(firstFract.CompareTo(secondFract));
- //firstFract.ReduceFraction().Write();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement