Skip to content

Commit 44929ce

Browse files
I have added natural language support, and multiple number support and updated the README with new features!
1 parent 3b9a0d1 commit 44929ce

File tree

3 files changed

+198
-27
lines changed

3 files changed

+198
-27
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ bin/
3333
.DS_Store
3434

3535
### Config Files ###
36-
*.iml
36+
*.iml
37+
38+
### My compiling script ###
39+
/build.sh
40+
41+
### These are the files generated by the build process ###
42+
/output/
43+
/input/

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
A simple calculator! All it does is open a Java GUI and allows you to input a simple operation and get an answer. Simple, easy and quick!
55

66
## 🕔 What's to come?
7-
I plan to add natural language so you can just type 1 plus 1 and get 2!
8-
I also plan to support bigger calculations rather than just 2 numbers ( which it is currently limited to ).
7+
- Add more language! So more cases are covered.
8+
- I also plan support for decimals and more complex operations like square roots, modulus, etc.
9+
- I plan to add support for negative numbers and fractions as well.
10+
- I also plan to add brackets/parentheses supports
11+
12+
## ✅ What's been added?
13+
- Basic operations: `+`, `-`, `*`, `/`, `^`
14+
- Natural language so you can just type 1 plus 1 and get 2!
15+
- I hav added multi-number support so you can do 1 + 2 + 3 + 4 and get 10! (BIDMAS/BODMAS is supported, however, brackets are not supported yet)
916

1017
## 🔽 How to download?
11-
You can download it by clicking [here](https://github.com/Spacexplorer11/SuperCalculator/archive/refs/heads/main.zip).
12-
Then run the SuperCalculator.app file under the output folder.
13-
However, if you already have Java installed, I **strongly recommend** you [download](https://www.mediafire.com/file/jlu7tydnk3n05z9/SuperCalculator.jar/file) the [.jar](https://www.mediafire.com/file/jlu7tydnk3n05z9/SuperCalculator.jar/file) file instead, as this will save **a lot of space**
18+
You can download it by going to the [releases](https://github.com/Spacexplorer11/SuperCalculator/releases) page and downloading the latest version for your OS.
19+
However, if you already have Java installed, I **strongly recommend** you download the .jar file instead, as this will save **a lot of space**
20+
Just go to [releases](https://github.com/Spacexplorer11/SuperCalculator/releases) and find the latest .jar file!
1421

1522
## Thanks! If you like this project consider giving my repository a ⭐! :D

src/Main.kt

Lines changed: 178 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,213 @@
1-
import java.util.Locale.getDefault
2-
import kotlin.math.pow
31
import javax.swing.JOptionPane
2+
import kotlin.math.pow
3+
import kotlin.system.exitProcess
44

55
fun add(a: Double, b: Double) = a + b
66
fun subtract(a: Double, b: Double) = a - b
77
fun multiply(a: Double, b: Double) = a * b
88
fun divide(a: Double, b: Double): Double {
9-
if (b == 0.0) {
10-
JOptionPane.showMessageDialog(null, "Cannot divide by zero")
11-
}
129
return a / b
1310
}
11+
1412
fun power(a: Double, b: Double) = a.pow(b)
1513

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+
)
1637

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+
}
2464
}
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
2579
when {
2680
calculation.contains("+") -> {
2781
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])
2983
}
84+
3085
calculation.contains("-") -> {
3186
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])
3388
}
89+
3490
calculation.contains("*") -> {
3591
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])
4193
}
94+
4295
calculation.contains("/") -> {
4396
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])
4598
}
99+
46100
calculation.contains("^") -> {
47101
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])
49103
}
104+
50105
else -> {
51106
JOptionPane.showMessageDialog(null, "Invalid operation")
52107
}
53108
}
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
54210
JOptionPane.showMessageDialog(null, "Thank you for using the calculator!")
211+
exitProcess(0)
55212
}
56213

0 commit comments

Comments
 (0)