fork download
  1. class ComplexInt(val real: Int, val imag: Int) {
  2. override def toString =
  3. real + (if (imag >= 0) "+" else "") + imag + "i"
  4. def +(other: ComplexInt) =
  5. new ComplexInt(real + other.real, imag + other.imag)
  6. def -(other: ComplexInt) =
  7. new ComplexInt(real - other.real, imag - other.imag)
  8. }
  9.  
  10. object ComplexInt {
  11. implicit class ComplexIntReal(real: Int) extends ComplexInt(real, 0) {}
  12. }
  13.  
  14. object Main {
  15. def main(args: Array[String]) {
  16. val a = new ComplexInt(2, 3)
  17. println(a)
  18. println(a - 1) // (2 + 3i) - 1
  19. println(1 - a) // 1 - (2 + 3i)
  20. }
  21. }
  22.  
Success #stdin #stdout 0.34s 4382720KB
stdin
Standard input is empty
stdout
2+3i
1+3i
-1-3i