|
1 |
| -import java.util.Locale.getDefault |
2 |
| -import kotlin.math.pow |
3 | 1 | import javax.swing.JOptionPane
|
| 2 | +import kotlin.math.pow |
| 3 | +import kotlin.system.exitProcess |
4 | 4 |
|
5 | 5 | fun add(a: Double, b: Double) = a + b
|
6 | 6 | fun subtract(a: Double, b: Double) = a - b
|
7 | 7 | fun multiply(a: Double, b: Double) = a * b
|
8 | 8 | fun divide(a: Double, b: Double): Double {
|
9 |
| - if (b == 0.0) { |
10 |
| - JOptionPane.showMessageDialog(null, "Cannot divide by zero") |
11 |
| - } |
12 | 9 | return a / b
|
13 | 10 | }
|
| 11 | + |
14 | 12 | fun power(a: Double, b: Double) = a.pow(b)
|
15 | 13 |
|
| 14 | +val numberWords = mapOf( |
| 15 | + "zero" to "0", |
| 16 | + "one" to "1", |
| 17 | + "two" to "2", |
| 18 | + "three" to "3", |
| 19 | + "four" to "4", |
| 20 | + "five" to "5", |
| 21 | + "six" to "6", |
| 22 | + "seven" to "7", |
| 23 | + "eight" to "8", |
| 24 | + "nine" to "9", |
| 25 | + "ten" to "10", |
| 26 | + "eleven" to "11", |
| 27 | + "twelve" to "12", |
| 28 | + "thirteen" to "13", |
| 29 | + "fourteen" to "14", |
| 30 | + "fifteen" to "15", |
| 31 | + "sixteen" to "16", |
| 32 | + "seventeen" to "17", |
| 33 | + "eighteen" to "18", |
| 34 | + "nineteen" to "19", |
| 35 | + "twenty" to "20" |
| 36 | +) |
16 | 37 |
|
17 |
| -fun main() { |
18 |
| - JOptionPane.showMessageDialog(null, "Welcome to the calculator!") |
19 |
| - val calculation = JOptionPane.showInputDialog("Please write your calculation (max 2 numbers):") |
20 |
| - calculation.lowercase(getDefault()) |
21 |
| - if (calculation == null || calculation.isEmpty()) { |
22 |
| - JOptionPane.showMessageDialog(null, "No input provided. Exiting.") |
23 |
| - return |
| 38 | +val operatorWords = mapOf( |
| 39 | + "plus" to "+", |
| 40 | + "add" to "+", |
| 41 | + "minus" to "-", |
| 42 | + "subtract" to "-", |
| 43 | + "x" to "*", |
| 44 | + "times" to "*", |
| 45 | + "multiplied by" to "*", |
| 46 | + "multiply" to "*", |
| 47 | + "divided by" to "/", |
| 48 | + "divide" to "/", |
| 49 | + "over" to "/", |
| 50 | + "raised to" to "^", |
| 51 | + "raised to the power of" to "^", |
| 52 | + "to the power of" to "^", |
| 53 | + "power" to "^" |
| 54 | +) |
| 55 | + |
| 56 | +val operators = listOf("^", "/", "*", "+", "s") |
| 57 | + |
| 58 | +fun translateInput(input: String): String { |
| 59 | + var input = input.lowercase() |
| 60 | + for (i in numberWords.keys) { |
| 61 | + if (input.contains(i)) { |
| 62 | + input = input.replace(i, numberWords[i]!!) |
| 63 | + } |
24 | 64 | }
|
| 65 | + for (i in operatorWords.keys) { |
| 66 | + if (input.contains(i)) { |
| 67 | + input = input.replace(i, operatorWords[i]!!) |
| 68 | + } |
| 69 | + } |
| 70 | + if (input.contains(Regex("""/ *0(\.0*)?"""))) { |
| 71 | + JOptionPane.showMessageDialog(null, "Cannot divide by zero") |
| 72 | + return "EXIT" |
| 73 | + } |
| 74 | + return input |
| 75 | +} |
| 76 | + |
| 77 | +fun calculate(calculation: String): Double { |
| 78 | + var result = 0.0 |
25 | 79 | when {
|
26 | 80 | calculation.contains("+") -> {
|
27 | 81 | val numbers = calculation.split("+").map { it.trim().toDouble() }
|
28 |
| - JOptionPane.showMessageDialog(null, "Result: ${add(numbers[0], numbers[1])}") |
| 82 | + result = add(numbers[0], numbers[1]) |
29 | 83 | }
|
| 84 | + |
30 | 85 | calculation.contains("-") -> {
|
31 | 86 | val numbers = calculation.split("-").map { it.trim().toDouble() }
|
32 |
| - JOptionPane.showMessageDialog(null, "Result: ${subtract(numbers[0], numbers[1])}") |
| 87 | + result = subtract(numbers[0], numbers[1]) |
33 | 88 | }
|
| 89 | + |
34 | 90 | calculation.contains("*") -> {
|
35 | 91 | val numbers = calculation.split("*").map { it.trim().toDouble() }
|
36 |
| - JOptionPane.showMessageDialog(null, "Result: ${multiply(numbers[0], numbers[1])}") |
37 |
| - } |
38 |
| - calculation.contains("x") -> { |
39 |
| - val numbers = calculation.split("x").map { it.trim().toDouble() } |
40 |
| - JOptionPane.showMessageDialog(null, "Result: ${multiply(numbers[0], numbers[1])}") |
| 92 | + result = multiply(numbers[0], numbers[1]) |
41 | 93 | }
|
| 94 | + |
42 | 95 | calculation.contains("/") -> {
|
43 | 96 | val numbers = calculation.split("/").map { it.trim().toDouble() }
|
44 |
| - JOptionPane.showMessageDialog(null, "Result: ${divide(numbers[0], numbers[1])}") |
| 97 | + result = divide(numbers[0], numbers[1]) |
45 | 98 | }
|
| 99 | + |
46 | 100 | calculation.contains("^") -> {
|
47 | 101 | val numbers = calculation.split("^").map { it.trim().toDouble() }
|
48 |
| - JOptionPane.showMessageDialog(null, "Result: ${power(numbers[0], numbers[1])}") |
| 102 | + result = power(numbers[0], numbers[1]) |
49 | 103 | }
|
| 104 | + |
50 | 105 | else -> {
|
51 | 106 | JOptionPane.showMessageDialog(null, "Invalid operation")
|
52 | 107 | }
|
53 | 108 | }
|
| 109 | + |
| 110 | + return result |
| 111 | +} |
| 112 | + |
| 113 | +fun askRetry() : String { |
| 114 | + val retry = JOptionPane.showConfirmDialog( |
| 115 | + null, |
| 116 | + "Do you want to perform another calculation?", |
| 117 | + "Retry", |
| 118 | + JOptionPane.YES_NO_OPTION |
| 119 | + ) |
| 120 | + return if (retry == JOptionPane.YES_OPTION) { |
| 121 | + "Retry" |
| 122 | + } |
| 123 | + else { |
| 124 | + "EXIT" |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fun calculator(): String { |
| 129 | + JOptionPane.showMessageDialog(null, "Welcome to the calculator!") |
| 130 | + var calculation = JOptionPane.showInputDialog("Please write your calculation (max 2 numbers):").lowercase() |
| 131 | + if (calculation.isEmpty()) { |
| 132 | + JOptionPane.showMessageDialog(null, "No input provided. Exiting.") |
| 133 | + return "EXIT" |
| 134 | + } |
| 135 | + calculation = translateInput(calculation) |
| 136 | + if (calculation == "EXIT") { |
| 137 | + return "EXIT" |
| 138 | + } |
| 139 | + else if (calculation == "Retry") { |
| 140 | + return "Retry" |
| 141 | + } |
| 142 | + val operatorCount = operators.sumOf { op -> |
| 143 | + calculation.count { it.toString() == op } |
| 144 | + } |
| 145 | + calculation = calculation.replace(" ", "") |
| 146 | + if (operatorCount == 1) { |
| 147 | + calculation = calculate(calculation).toString() |
| 148 | + } |
| 149 | + else if (operatorCount >= 2) { |
| 150 | + var multipleOperators = true |
| 151 | + var calculationShortened: String |
| 152 | + var calculationIndex: Int |
| 153 | + var result: Double |
| 154 | + while (multipleOperators) { |
| 155 | + for ( i in operators) { |
| 156 | + if (calculation.contains(i)) { |
| 157 | + calculationIndex = calculation.indexOf(i) |
| 158 | + val leftEnd = calculationIndex |
| 159 | + var leftStart = leftEnd - 1 |
| 160 | + println("Operator: $i, Index: $calculationIndex, Calculation: $calculation") |
| 161 | + println(calculation[leftStart]) |
| 162 | + while (leftStart >= 0 && calculation[leftStart].isDigit()) { |
| 163 | + leftStart-- |
| 164 | + } |
| 165 | + leftStart++ // adjust to real start |
| 166 | + println("Left index: $leftStart") |
| 167 | + val leftOperand = calculation.substring(leftStart, leftEnd) |
| 168 | + val rightStart = calculationIndex + 1 |
| 169 | + var rightEnd = rightStart |
| 170 | + while (rightEnd < calculation.length && calculation[rightEnd].isDigit()) { |
| 171 | + rightEnd++ |
| 172 | + } |
| 173 | + val rightOperand = calculation.substring(rightStart, rightEnd) |
| 174 | + println("Rightend $rightEnd") |
| 175 | + println("Left Operand: $leftOperand, Right Operand: $rightOperand") |
| 176 | + calculationShortened = leftOperand + i + rightOperand |
| 177 | + println(calculationShortened) |
| 178 | + result = calculate(calculationShortened) |
| 179 | + calculation = calculation.replace(calculationShortened, result.toString()) |
| 180 | + print("---------------------------------------------------------------------------\nCalculation: $calculation") |
| 181 | + val operatorCount = operators.sumOf { op -> |
| 182 | + calculation.count { it.toString() == op } |
| 183 | + } |
| 184 | + if (operatorCount == 1) { |
| 185 | + calculation = calculate(calculation).toString() |
| 186 | + break |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + val operatorCount = operators.sumOf { op -> |
| 191 | + calculation.count { it.toString() == op } |
| 192 | + } |
| 193 | + multipleOperators = operatorCount > 0 |
| 194 | + } |
| 195 | + } |
| 196 | + JOptionPane.showMessageDialog(null, "Result $calculation") |
| 197 | + |
| 198 | + return askRetry() |
| 199 | +} |
| 200 | + |
| 201 | + |
| 202 | +fun main() { |
| 203 | + var result = calculator() |
| 204 | + while (result != "EXIT") { |
| 205 | + if (result == "Retry") { |
| 206 | + result = calculator() |
| 207 | + } |
| 208 | + } |
| 209 | + // Exit the program |
54 | 210 | JOptionPane.showMessageDialog(null, "Thank you for using the calculator!")
|
| 211 | + exitProcess(0) |
55 | 212 | }
|
56 | 213 |
|
0 commit comments