Ladies_Man

scalab 4 slomst done (net)

May 28th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.57 KB | None | 0 0
  1. trait MyNumeric[T] {
  2.  
  3. }
  4.  
  5.  
  6. trait MyIntNumeric[T] extends MyNumeric[T]{
  7.     def sub(x: T, y: T): T
  8.     def div(x: T, y: T): T
  9.     def mul(x: T, y: T): T
  10. }
  11.  
  12. object MyIntNumeric {
  13.     implicit object IntIsMyNumeric extends MyIntNumeric[Int] {
  14.         def sub(x: Int, y: Int) = x - y
  15.         def div(x: Int, y: Int) = x / y
  16.         def mul(x: Int, y: Int) = x * y
  17.     }
  18.     implicit object LongIsMyNumeric extends MyIntNumeric[Long] {
  19.         def sub(x: Long, y: Long) = x - y
  20.         def div(x: Long, y: Long) = x / y
  21.         def mul(x: Long, y: Long) = x * y
  22.     }
  23. }
  24.  
  25.  
  26.  
  27. trait MyFloatNumeric[T] extends MyNumeric[T]{
  28.     def sub(x: T, y: T): T
  29.     def div(x: T, y: T): T
  30.     def mul(x: T, y: T): T
  31. }
  32.  
  33. object MyNumeric {
  34.     implicit object FloatIsMyNumeric extends MyFloatNumeric[Float] {
  35.         def sub(x: Float, y: Float) = x - y
  36.         def div(x: Float, y: Float) = x / y
  37.         def mul(x: Float, y: Float) = x * y
  38.     }
  39.     implicit object DoubleIsMyNumeric extends MyFloatNumeric[Double] {
  40.         def sub(x: Double, y: Double) = x - y
  41.         def div(x: Double, y: Double) = x / y
  42.         def mul(x: Double, y: Double) = x * y
  43.     }
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. class Fraction[T] (n: T, d: T)(implicit num: MyIntNumeric[T]) {
  51.     val numerator : T = num.div(n, gcd(n, d))
  52.     val denominator : T = num.div(d, gcd(n, d))
  53.  
  54.     def gcd (x: T, y: T): T =
  55.         if (0 == y) x else gcd(y, num.sub(x, num.mul(y, num.div(x, y))))
  56.  
  57.     override def toString = {
  58.         if (0 == numerator) "{0}"
  59.         else if (1 == denominator) "{" + numerator + "}"
  60.         else if (numerator == denominator) "{1}"
  61.         else "{" + numerator + "}/{" + denominator + "}"
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. class EquationSystem[T] (a: T, b: T, c: T, d: T, e: T, f: T)(implicit num: MyNumeric[T]) {
  68.     //x = (c*e-a*b) / (a*e - b*d)
  69.     //a*f - c*d / a*e - b*d
  70.  
  71.     //num.div(num.sub(num.times(c,e), num.times(a,b)),
  72.     //num.sub(num.times(a,e), num.times(b,d)))
  73.  
  74.     def solve[T]()(implicit num: MyIntNumeric[T]): (Fraction[T], Fraction[T]) = {
  75.  
  76.         (new Fraction[T](
  77.             num.sub(num.mul(c, e), num.mul(a, b)),
  78.             num.sub(num.mul(a, e), num.mul(b, d))),
  79.  
  80.           new Fraction[T](
  81.               num.sub(num.mul(c, e), num.mul(a, b)),
  82.               num.sub(num.mul(a, e), num.mul(b, d))))
  83.     }
  84.  
  85.     def solve[T]()(implicit num: MyFloatNumeric[T]): (T, T) = {
  86.  
  87.         (num.div(
  88.             num.sub(num.mul(c, e), num.mul(a, b)),
  89.             num.sub(num.mul(a, e), num.mul(b, d))),
  90.  
  91.           num.div(
  92.               num.sub(num.mul(c, e), num.mul(a, b)),
  93.               num.sub(num.mul(a, e), num.mul(b, d)))
  94.         )
  95.     }
  96.  
  97.     override def toString = "\n" +
  98.       a + "*X + " + b + "*Y = " + c + "\n" +
  99.       d + "*X + " + e + "*Y = " + f
  100. }
  101.  
  102.  
  103.  
  104. object Main {
  105.     def main(args: Array[String]): Unit = {
  106.         val e  = new Fraction[Int](26, 74)
  107.         println(e)
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment