Ladies_Man

#Scala Lab4 Implicit crap COMPLETE

Oct 24th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.54 KB | None | 0 0
  1. Класс EquationSystem[T], представляющий систему из двух линейных уравнений
  2. от двух переменных с коэффициентами типа T и оперцией solve, возвращающей
  3. решение уравнения в виде Option[(S, S)], где тип S зависит от типа T. Эта
  4. зависимость задаётся следующими правилами: если T – тип с плавающей точкой,
  5. то S совпадает с T; если T – целочисленный тип, то S – дробь, числитель и
  6. знаменатель которой имеют тип T; в остальных случаях операция solve
  7. недоступна. Для представления дробей следует создать отдельный класс.
  8.  
  9.  
  10.  
  11. //fully provided by Daria
  12.  
  13.  
  14. abstract class customNumeric[T] {
  15.     def div(x: T, y: T): T
  16.     def mul(x: T, y: T): T
  17.     def sub(x: T, y: T): T
  18. }
  19.  
  20. object customNumeric {
  21.  
  22.     implicit object FloatIsMyNumeric extends customNumeric[Float] {
  23.         def div(x: Float, y: Float): Float = x / y
  24.         def mul(x: Float, y: Float): Float = x * y
  25.         def sub(x: Float, y: Float): Float = x - y
  26.     }
  27.  
  28.     implicit object DoubleIsMyNumeric extends customNumeric[Double] {
  29.         def div(x: Double, y: Double): Double = x / y
  30.         def mul(x: Double, y: Double): Double = x * y
  31.         def sub(x: Double, y: Double): Double = x - y
  32.     }
  33.  
  34.     implicit object IntIsMyNumeric extends customNumeric[Int] {
  35.         def div(x: Int, y: Int): Int = x / y
  36.         def mul(x: Int, y: Int): Int = x * y
  37.         def sub(x: Int, y: Int): Int = x - y
  38.     }
  39.  
  40.     implicit object LongIsMyNumeric extends customNumeric[Long] {
  41.         def div(x: Long, y: Long): Long = x / y
  42.         def mul(x: Long, y: Long): Long = x * y
  43.         def sub(x: Long, y: Long): Long = x - y
  44.     }
  45.  
  46.     implicit object FractionIntIsCustomNumeric extends customNumeric[Fraction[Int]] {
  47.         def div(x: Fraction[Int], y: Fraction[Int]): Fraction[Int] =
  48.             new Fraction[Int](x.numerator*y.denominator, x.denominator*y.numerator)
  49.  
  50.         def mul(x: Fraction[Int], y: Fraction[Int]): Fraction[Int] =
  51.             new Fraction[Int](x.numerator*y.numerator, x.denominator*y.denominator)
  52.  
  53.         def sub(x: Fraction[Int], y: Fraction[Int]): Fraction[Int] =
  54.             new Fraction[Int](x.numerator*y.denominator - y.numerator*x.denominator, x.denominator*y.denominator)
  55.     }
  56.  
  57.     implicit object FractionLongIsCustomNumeric extends customNumeric[Fraction[Long]] {
  58.         def div(x: Fraction[Long], y: Fraction[Long]): Fraction[Long] =
  59.             new Fraction[Long](x.numerator*y.denominator, x.denominator*y.numerator)
  60.  
  61.         def mul(x: Fraction[Long], y: Fraction[Long]): Fraction[Long] =
  62.             new Fraction[Long](x.numerator*y.numerator, x.denominator*y.denominator)
  63.  
  64.         def sub(x: Fraction[Long], y: Fraction[Long]): Fraction[Long] =
  65.             new Fraction[Long](x.numerator*y.denominator - y.numerator*x.denominator, x.denominator*y.denominator)
  66.     }
  67. }
  68.  
  69.  
  70.  
  71.  
  72. class Fraction[T] (n: T, d: T)(implicit num: customNumeric[T]) {
  73.     val numerator : T = num.div(n, gcd(n, d))
  74.     val denominator : T = num.div(d, gcd(n, d))
  75.  
  76.     def gcd (x: T, y: T): T = if (0 == y) x else gcd(y, num.sub(x, num.mul(y, num.div(x, y))))
  77.  
  78.     override def toString = {
  79.         if (0 == numerator) "{0}"
  80.         else if (1 == denominator) "{" + numerator + "}"
  81.         else if (numerator == denominator) "{1}"
  82.         else "{" + numerator + "}/{" + denominator + "}"
  83.     }
  84. }
  85.  
  86. object Fraction {
  87.     implicit def int2Fraction(x: Int): Fraction[Int] = new Fraction[Int](x, 1)
  88.     implicit def long2Fraction(x: Long): Fraction[Long] = new Fraction[Long](x, 1)
  89. }
  90.  
  91.  
  92.  
  93.  
  94. class EquationSystem[T] (val a: T, val b: T, val c: T,
  95.                                  val d: T, val e: T, val f: T) {
  96.  
  97.     def solve(implicit num : customNumeric[T]): Option[(T, T)] = {
  98.         Option(
  99.             num.div(
  100.                 num.sub(num.mul(c, e), num.mul(b, f)),
  101.                 num.sub(num.mul(a, e), num.mul(b, d))),
  102.  
  103.             num.div(
  104.                 num.sub(num.mul(f, a), num.mul(c, d)),
  105.                 num.sub(num.mul(a, e), num.mul(b, d)))
  106.         )
  107.     }
  108.  
  109.     override def toString = "\n" +
  110.       "(" + a + ")*X + (" + b + ")*Y = " + c + "\n" +
  111.       "(" + d + ")*X + (" + e + ")*Y = " + f
  112. }
  113.  
  114. object Main {
  115.     def main(args: Array[String]): Unit = {
  116.  
  117.         val esf = new EquationSystem[Float](2,-1,7,2,3,1)
  118.         println(esf)
  119.         println("float: " + esf.solve)
  120.  
  121.         val esd = new EquationSystem[Double](2,-1,7,2,3,1)
  122.         println("double: " + esd.solve)
  123.  
  124.         val esi = new EquationSystem[Fraction[Int]](2,-1,7,2,3,1)
  125.         println("intFrac: " + esi.solve)
  126.  
  127.         val esl = new EquationSystem[Fraction[Long]](2l,-1l,7l,2l,3l,1l)
  128.         println("longFrac: " + esl.solve)
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment