Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Rationals
- {
- public struct Rational : IEquatable<Rational>
- {
- public static implicit operator Rational(int x) => new Rational(x, 1);
- public static implicit operator float(Rational x) => (float)x.numerator / x.quotient;
- public static implicit operator double(Rational x) => (double)x.numerator / x.quotient;
- public static implicit operator decimal(Rational x) => (decimal)x.numerator / x.quotient;
- public static explicit operator int(Rational x) => x.numerator / x.quotient;
- public static bool operator ==(Rational x, Rational y) => x.numerator == y.numerator && x.quotient == y.quotient;
- public static bool operator !=(Rational x, Rational y) => !(x == y);
- public static bool operator <(Rational x, Rational y) =>
- x.numerator * y.quotient < y.numerator * x.quotient;
- public static bool operator >(Rational x, Rational y) =>
- x.numerator * y.quotient > y.numerator * x.quotient;
- public static Rational operator -(Rational x, Rational y) => new Rational(
- x.numerator * y.quotient - y.numerator * x.quotient,
- x.quotient * y.quotient
- );
- public static Rational operator +(Rational x, Rational y) => new Rational(
- x.numerator * y.quotient + y.numerator * x.quotient,
- x.quotient * y.quotient
- );
- public static Rational operator *(Rational x, Rational y) => new Rational(
- x.numerator * y.numerator,
- x.quotient * y.quotient
- );
- public static Rational operator /(Rational x, Rational y) => new Rational(
- x.numerator * y.quotient,
- y.numerator * x.quotient
- );
- public static Rational operator +(Rational x) => x;
- public static Rational operator -(Rational x) => new Rational(-x.numerator, x.quotient);
- public static Rational Parse(string s) =>
- Parse(s.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray());
- private static Rational Parse(string[] s) =>
- s.Length == 2 ? new Rational(int.Parse(s[0]), int.Parse(s[1])) :
- s.Length == 1 ? new Rational(int.Parse(s[0]), 1) :
- throw new FormatException();
- public static int GreatestCommonDivisor(int a, int b)
- {
- while (a != 0 && b != 0)
- {
- if (a > b) a %= b;
- else b %= a;
- }
- return a == 0 ? b : a;
- }
- readonly int numerator, quotient;
- public Rational(int numerator, int quotient)
- {
- int gcd = GreatestCommonDivisor(Math.Abs(numerator), Math.Abs(quotient));
- numerator /= gcd;
- quotient /= gcd;
- if (quotient < 0)
- {
- numerator = -numerator;
- quotient = -quotient;
- }
- this.numerator = numerator;
- this.quotient = quotient;
- }
- public Rational GetReciprocal() => new Rational(quotient, numerator);
- public override int GetHashCode() => (int)Math.Truncate((float)this * 100000);
- public override bool Equals(object obj) => obj is Rational x && this == x;
- public bool Equals(Rational x) => this == x;
- public override string ToString() => $"{numerator}/{quotient}";
- }
- class Program
- {
- static readonly string BinaryOperators = "+-*/";
- static void Main(string[] args)
- {
- for (; ; )
- {
- string command = ReadString("Command (+ - * / R)");
- if (string.IsNullOrEmpty(command)) break;
- char commandChar = char.ToLower(command[0]);
- if (BinaryOperators.Contains(commandChar))
- {
- Rational a = ReadRational("A");
- Rational b = ReadRational("B");
- Rational result =
- commandChar == '+' ? a + b :
- commandChar == '-' ? a - b :
- commandChar == '*' ? a * b :
- commandChar == '/' ? a / b :
- throw new NotImplementedException();
- Console.WriteLine($"{a} {commandChar} {b} = {result}");
- }
- else if (commandChar == 'r')
- {
- Rational x = ReadRational("rational to find reciprocal of");
- Rational result = x.GetReciprocal();
- Console.WriteLine($"Reciprocal of {x} is {result}");
- }
- else throw new NotImplementedException();
- }
- }
- static Rational ReadRational(string name) => Rational.Parse(ReadString(name));
- static string ReadString(string name)
- {
- Console.WriteLine($"Enter {name}: "); ;
- return Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement