| |
| fun main() { |
| while (true){ |
| println("=========请输入你的表达式=========") |
| val input = readLine() |
| try { |
| input?.let { |
| val ret = calculate(it) |
| |
| println("${input}=${ret}") |
| println("是否继续使用(y/n)") |
| val cmd = readLine() |
| cmd?.let { |
| if (it == "n") { |
| exitProcess(-1) |
| } else { |
| |
| } |
| } |
| } |
| } catch (ex: Exception) { |
| ex.printStackTrace() |
| } |
| } |
| |
| } |
| |
| fun calculate(input: String): String { |
| |
| |
| |
| |
| |
| if (input.contains("+")) { |
| val nums = input.trim().split("+") |
| return operate(nums[0].toDouble(), nums[1].toDouble(), "+").toString() |
| } else if (input.contains("-")) { |
| val nums = input.trim().split("-") |
| return operate(nums[0].toDouble(), nums[1].toDouble(), "-").toString() |
| } else if (input.contains("/")) { |
| val nums = input.trim().split("/") |
| return operate(nums[0].toDouble(), nums[1].toDouble(), "/").toString() |
| } else if (input.contains("*")) { |
| val nums = input.trim().split("*") |
| return operate(nums[0].toDouble(), nums[1].toDouble(), "*").toString() |
| } else { |
| return "error:你输入的表达式有误" |
| } |
| } |
| |
| fun operate(num: Double, num2: Double, operate: String): Double { |
| return when (operate) { |
| "+" -> num + num2 |
| "-" -> num - num2 |
| "/" -> num / num2 |
| "*" -> num * num2 |
| else -> 0.0 |
| } |
| } |