-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.kt.txt
51 lines (42 loc) · 1.52 KB
/
calculator.kt.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.calculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import org.w3c.dom.Text
class MainActivity : AppCompatActivity() {
private var text: TextView? = null
private var value: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text = findViewById(R.id.text)
}
fun onDigit(view: View) {
text?.append((view as Button).text)
}
fun onClear(view: View) {
text?.text = ""
}
fun equal(view: View) {
var value = (text?.text).toString()
if (value.contains("-")) {
var splitvalue = value.split("-")
text?.text = (splitvalue[0].toDouble() - splitvalue[1].toDouble()).toString()
}
if (value.contains("+")) {
var splitvalue = value.split("+")
text?.text = (splitvalue[0].toDouble() + splitvalue[1].toDouble()).toString()
}
if (value.contains("*")) {
var splitvalue = value.split("*")
text?.text = (splitvalue[0].toDouble() * splitvalue[1].toDouble()).toString()
}
if (value.contains("/")) {
var splitvalue = value.split("/")
text?.text = (splitvalue[0].toDouble() / splitvalue[1].toDouble()).toString()
}
}
}