Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ComplexNumber(private val re: Double, private val im: Double) {
- def +(otherNumber: ComplexNumber) = new ComplexNumber(re + otherNumber.re, im + otherNumber.im)
- def -(otherNumber: ComplexNumber) = new ComplexNumber(re - otherNumber.re, im - otherNumber.im)
- def *(otherNumber: ComplexNumber): ComplexNumber = {
- new ComplexNumber(re * otherNumber.re - im * otherNumber.im, im * otherNumber.re + re * otherNumber.im)
- }
- def printComplex(): Unit = {
- println(s"${re} + ${im}i")
- }
- }
- object ComplexNumbersCalculator extends App {
- private def createComplexNumberFromConsole() = {
- println("Enter real part:")
- val re = scala.io.StdIn.readDouble()
- println("Enter imaginary part:")
- val im = scala.io.StdIn.readDouble()
- new ComplexNumber(re, im)
- }
- private var continue = true
- while (continue) {
- val num1: ComplexNumber = createComplexNumberFromConsole()
- val num2: ComplexNumber = createComplexNumberFromConsole()
- println("Choose operation ['+', '-', '*']:")
- val operation = scala.io.StdIn.readChar()
- if (operation == '+') {
- (num1 + num2).printComplex()
- } else if (operation == '-') {
- (num1 - num2).printComplex()
- } else if (operation == '*') {
- (num1 * num2).printComplex()
- } else {
- println("Unknown operation!")
- }
- println("You want continue? (Y/n)")
- val continueRequest = scala.io.StdIn.readChar()
- continue = if (continueRequest == 'Y') true else false
- }
- println("Bye bye!")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement