Advertisement
Ladies_Man

#SCALA Lab2 COMPLETE

Mar 19th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.80 KB | None | 0 0
  1. // Элемент кольца вычетов по модулю m с операциями сложения и умножения
  2.  
  3.  
  4. class Elem(num: Int, mod: Int) {
  5.     val x = num % mod
  6.     val m = mod
  7.  
  8.     def + (k: Int) = new Elem(x + k, m)
  9.  
  10.     def + (q: Elem): Elem = {
  11.         if (m != q.m)
  12.             throw new Exception("modules not equal")
  13.         else
  14.             new Elem(x + q.x, m)
  15.     }
  16.  
  17.     def * (k: Int) = new Elem(x * k, m)
  18.  
  19.     def * (q: Elem): Elem = {
  20.         if (m != q.m)
  21.             throw new Exception("modules not equal")
  22.         else
  23.             new Elem(x * q.x, m)
  24.     }
  25.  
  26.     def this(mod: Int) = this(1, mod)
  27. }
  28.  
  29.  
  30. class ElemFactor(x: Int) {
  31.     def + (q: Elem) = q + x
  32.     def * (q: Elem) = q * x
  33. }
  34.  
  35. implicit def intToFactor(i: Int) = new ElemFactor(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement