Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ fun main(vararg arg: String){

/* Inputs from user */
val operator: String = values[1]
val lhs = values[0].toDoubleOrNull() ?: throw IllegalArgumentException("Invalid input: ${values[0]}")
val rhs = values[2].toDoubleOrNull() ?: throw IllegalArgumentException("Invalid input: ${values[1]}")
val left_operand = values[0].toDoubleOrNull() ?: throw IllegalArgumentException("Invalid input: ${values[0]}")
val right_operand = values[2].toDoubleOrNull() ?: throw IllegalArgumentException("Invalid input: ${values[1]}")

/* Checking operator and performing arithmetic operation */
val result = when (operator) {
"+" -> lhs + rhs
"-" -> lhs - rhs
"*" -> lhs * rhs
"/" -> lhs / rhs
"%" -> lhs % rhs
"+" -> left_operand + right_operand
"-" -> left_operand - right_operand
"*" -> left_operand * right_operand
"/" -> left_operand / right_operand
"%" -> left_operand % right_operand
"^" -> left_operand.pow(right_operand)

else -> throw IllegalArgumentException("Invalid operator: $operator")
}
Expand Down