Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Rationals
  5. {
  6.     public struct Rational : IEquatable<Rational>
  7.     {
  8.         public static implicit operator Rational(int x) => new Rational(x, 1);
  9.         public static implicit operator float(Rational x) => (float)x.numerator / x.quotient;
  10.         public static implicit operator double(Rational x) => (double)x.numerator / x.quotient;
  11.         public static implicit operator decimal(Rational x) => (decimal)x.numerator / x.quotient;
  12.         public static explicit operator int(Rational x) => x.numerator / x.quotient;
  13.  
  14.         public static bool operator ==(Rational x, Rational y) => x.numerator == y.numerator && x.quotient == y.quotient;
  15.         public static bool operator !=(Rational x, Rational y) => !(x == y);
  16.  
  17.         public static bool operator <(Rational x, Rational y) =>
  18.             x.numerator * y.quotient < y.numerator * x.quotient;
  19.  
  20.         public static bool operator >(Rational x, Rational y) =>
  21.             x.numerator * y.quotient > y.numerator * x.quotient;
  22.  
  23.         public static Rational operator -(Rational x, Rational y) => new Rational(
  24.             x.numerator * y.quotient - y.numerator * x.quotient,
  25.             x.quotient * y.quotient
  26.         );
  27.  
  28.         public static Rational operator +(Rational x, Rational y) => new Rational(
  29.             x.numerator * y.quotient + y.numerator * x.quotient,
  30.             x.quotient * y.quotient
  31.         );
  32.  
  33.         public static Rational operator *(Rational x, Rational y) => new Rational(
  34.             x.numerator * y.numerator,
  35.             x.quotient * y.quotient
  36.         );
  37.  
  38.         public static Rational operator /(Rational x, Rational y) => new Rational(
  39.             x.numerator * y.quotient,
  40.             y.numerator * x.quotient
  41.         );
  42.  
  43.         public static Rational operator +(Rational x) => x;
  44.         public static Rational operator -(Rational x) => new Rational(-x.numerator, x.quotient);
  45.  
  46.         public static Rational Parse(string s) =>
  47.             Parse(s.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray());
  48.         private static Rational Parse(string[] s) =>
  49.             s.Length == 2 ? new Rational(int.Parse(s[0]), int.Parse(s[1])) :
  50.             s.Length == 1 ? new Rational(int.Parse(s[0]), 1) :
  51.             throw new FormatException();
  52.  
  53.         public static int GreatestCommonDivisor(int a, int b)
  54.         {
  55.             while (a != 0 && b != 0)
  56.             {
  57.                 if (a > b) a %= b;
  58.                 else b %= a;
  59.             }
  60.  
  61.             return a == 0 ? b : a;
  62.         }
  63.  
  64.  
  65.         readonly int numerator, quotient;
  66.  
  67.         public Rational(int numerator, int quotient)
  68.         {
  69.             int gcd = GreatestCommonDivisor(Math.Abs(numerator), Math.Abs(quotient));
  70.             numerator /= gcd;
  71.             quotient /= gcd;
  72.             if (quotient < 0)
  73.             {
  74.                 numerator = -numerator;
  75.                 quotient = -quotient;
  76.             }
  77.  
  78.             this.numerator = numerator;
  79.             this.quotient = quotient;
  80.         }
  81.  
  82.         public Rational GetReciprocal() => new Rational(quotient, numerator);
  83.  
  84.         public override int GetHashCode() => (int)Math.Truncate((float)this * 100000);
  85.         public override bool Equals(object obj) => obj is Rational x && this == x;
  86.         public bool Equals(Rational x) => this == x;
  87.         public override string ToString() => $"{numerator}/{quotient}";
  88.  
  89.     }
  90.  
  91.     class Program
  92.     {
  93.         static readonly string BinaryOperators = "+-*/";
  94.  
  95.         static void Main(string[] args)
  96.         {
  97.             for (; ; )
  98.             {
  99.                 string command = ReadString("Command (+ - * / R)");
  100.                 if (string.IsNullOrEmpty(command)) break;
  101.  
  102.                 char commandChar = char.ToLower(command[0]);
  103.                 if (BinaryOperators.Contains(commandChar))
  104.                 {
  105.                     Rational a = ReadRational("A");
  106.                     Rational b = ReadRational("B");
  107.  
  108.                     Rational result =
  109.                         commandChar == '+' ? a + b :
  110.                         commandChar == '-' ? a - b :
  111.                         commandChar == '*' ? a * b :
  112.                         commandChar == '/' ? a / b :
  113.                         throw new NotImplementedException();
  114.  
  115.                     Console.WriteLine($"{a} {commandChar} {b} = {result}");
  116.                 }
  117.                 else if (commandChar == 'r')
  118.                 {
  119.                     Rational x = ReadRational("rational to find reciprocal of");
  120.                     Rational result = x.GetReciprocal();
  121.                     Console.WriteLine($"Reciprocal of {x} is {result}");
  122.                 }
  123.                 else throw new NotImplementedException();
  124.             }
  125.         }
  126.  
  127.         static Rational ReadRational(string name) => Rational.Parse(ReadString(name));
  128.         static string ReadString(string name)
  129.         {
  130.             Console.WriteLine($"Enter {name}: "); ;
  131.             return Console.ReadLine();
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement