Last active
February 3, 2022 17:50
-
-
Save Ninayd/7885689e600f6010783d9e2636c6da50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Complex(internal var real: Int, internal var image: Int) { | |
| operator fun plus(C: Complex): Complex { | |
| return Complex(this.real + C.real, this.image + C.image) | |
| } | |
| operator fun minus(C: Complex): Complex { | |
| return Complex(this.real - C.real, this.image - C.image) | |
| } | |
| operator fun times(C: Complex): Complex { | |
| return Complex((this.real * C.real)-(this.image*C.image), (this.image * C.real)+(this.real*C.image)) | |
| } | |
| override fun toString(): String { | |
| return "${this.real}+${this.image}i" | |
| return "${this.real}-${this.image}i" | |
| return "${this.real}*${this.image}i" | |
| } | |
| } | |
| fun main(args: Array<String>) { | |
| val C1 = Complex(2, 3) | |
| val C2 = Complex(4, 5) | |
| println("Complex plus operator used on C1,C2:${C1.plus(C2)}") | |
| println("Complex minus operator used on C1,C2:${C1.minus(C2)}") | |
| println("Complex times operator used on C1,C2:${C1.times(C2)}") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment