KleveR_

Untitled

Dec 6th, 2020
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import scala.math._
  2.  
  3. case class C(val re: Double, val im: Double) {
  4.  
  5.   def this(re: Double) = this(re, 0)
  6.  
  7.   override def toString = s"$re${if (im < 0) s"${im}i"else if (im == 0) "" else s"+${im}i"}"
  8.  
  9.   def unary_- = new C(-re, -im)
  10.  
  11.   def +(that: C) = C(this.re + that.re, this.im + that.im)
  12.   def -(that: C) = this + -that
  13.   def *(that: C) = C(re * that.re - im * that.im, im * that.re + re * that.im)
  14.   def /(that: C) = {
  15.     require(that.re != 0 || that.im != 0)
  16.     val poweSum = pow(that.re, 2) + pow(that.im, 2)
  17.     new C((re * that.re + im * that.im) / poweSum, (im * that.re - re * that.im) / poweSum)
  18.   }
  19. }
  20.  
  21. object C {
  22.   def apply(re: Double, im: Double) = new C(re, im)
  23.   def apply(l: Double) = new C(l)
  24. }
  25.  
  26. object Zad extends App {
  27.  
  28.   implicit def doubleToC(r: Double): C = new C(r)
  29.   implicit def intToC(r: Int): C = new C(r)
  30.  
  31.   val test = C(2, 4);
  32.   val test1 = C(2, -4);
  33.   val test2 = C(2, 0);
  34.   val test3 = C(2);
  35.  
  36.   // TEST
  37.   println(test);
  38.   println(-test1);
  39.   println(test2);
  40.   println(test+test1)
  41.   println(test-test1)
  42.   println(test*test1)
  43.   println(test/test1)
  44.   println(test3);
  45.    println(C(2, 3) * 4)
  46.  
  47.  
  48.  
  49. }
  50.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×