戻る

MainActivity.kt

package net.lufimia.caltest0002

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.math.BigDecimal

var mainstack: MutableList = mutableListOf()
var mainstackp: Int = -1

class MainActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button_enter.setOnClickListener {
            var tmptext = textView0.text.toString()
            if (tmptext != "") {
                tmptext = tmptext.trim()
                mainstack.add(0, tmptext)
                mainstackp = mainstackp + 1
                textinputclear();
                ldisp()
            }
        }

        button_c.setOnClickListener {
            textinputclear()
        }

        button_clear.setOnClickListener {
            mainstack.clear()
            mainstackp = -1
            textinputclear()
            ldisp()
        }

        button_drop.setOnClickListener {
            if (mainstackp >= 0) {
                mainstack.removeAt(0)
                mainstackp = mainstackp - 1
                ldisp()

            }
        }

        button_swap.setOnClickListener {
            if (mainstackp >= 1) {
                var tmp = mainstack.get(0)
                mainstack.set(0, mainstack.get(1))
                mainstack.set(1, tmp)
                ldisp()

            }
        }

        button_add.setOnClickListener {
            if (mainstackp >= 1) {
                var tmp0: Int? = mainstack[0].toIntOrNull()
                var tmp1: Int? = mainstack[1].toIntOrNull()
                if ((tmp0 != null) && (tmp1 != null)) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = mainstack[1].toDouble()
                    mainstack.removeAt(0)
                    mainstack.removeAt(0)
                    tmp10 = tmp11 + tmp10
                    mainstack.add(0, tmp10.toString())
                    mainstackp = mainstackp - 1
                    ldisp()
                }
            }
        }

        button_sub.setOnClickListener {
            if (mainstackp >= 1) {
                var tmp0: Double? = mainstack[0].toDoubleOrNull()
                var tmp1: Double? = mainstack[1].toDoubleOrNull()
                if ((tmp0 != null) && (tmp1 != null)) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = mainstack[1].toDouble()
                    mainstack.removeAt(0)
                    mainstack.removeAt(0)
                    tmp10 = tmp11 - tmp10
                    mainstack.add(0, tmp10.toString())
                    mainstackp = mainstackp - 1
                    ldisp()
                }
            }
        }

        button_multi.setOnClickListener {
            if (mainstackp >= 1) {
                var tmp0: Double? = mainstack[0].toDoubleOrNull()
                var tmp1: Double? = mainstack[1].toDoubleOrNull()
                if ((tmp0 != null) && (tmp1 != null)) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = mainstack[1].toDouble()
                    mainstack.removeAt(0)
                    mainstack.removeAt(0)
                    tmp10 = tmp11 * tmp10
                    mainstack.add(0, tmp10.toString())
                    mainstackp = mainstackp - 1
                    ldisp()
                }
            }
        }

        button_div.setOnClickListener {
            if (mainstackp >= 1) {
                var tmp0: Double? = mainstack[0].toDoubleOrNull()
                var tmp1: Double? = mainstack[1].toDoubleOrNull()
                var tmp2: Int? = mainstack[0].toIntOrNull()
                if ((tmp0 != null) && (tmp1 != null) && (tmp2 != 0)) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = mainstack[1].toDouble()
                    mainstack.removeAt(0)
                    mainstack.removeAt(0)
                    tmp10 = tmp11 / tmp10
                    mainstack.add(0, tmp10.toString())
                    mainstackp = mainstackp - 1
                    ldisp()

                }
            }
        }

        button_keigen.setOnClickListener {
            if (mainstackp >= 0) {
                var tmp0: Double? = mainstack[0].toDoubleOrNull()
                if (tmp0 != null) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = tmp10 * 100
                    var tmp12: Int = tmp11.toInt()
                    var tmp13: Int = tmp12 / 108
                    var tmp14: Int = tmp12 / 100 - tmp13
                    mainstack.removeAt(0)
                    mainstack.add(0, tmp14.toString())
                    mainstack.add(0, tmp13.toString())
                    mainstackp = mainstackp + 1
                    ldisp()

                }
            }
        }

        button_hyoujun.setOnClickListener {
            if (mainstackp >= 0) {
                var tmp0: Double? = mainstack[0].toDoubleOrNull()
                if (tmp0 != null) {
                    var tmp10: Double = mainstack[0].toDouble()
                    var tmp11: Double = tmp10 * 100
                    var tmp12: Int = tmp11.toInt()
                    var tmp13: Int = tmp12 / 110
                    var tmp14: Int = tmp12 / 100 - tmp13
                    mainstack.removeAt(0)
                    mainstack.add(0, tmp14.toString())
                    mainstack.add(0, tmp13.toString())
                    mainstackp = mainstackp + 1
                    ldisp()

                }
            }
        }

        button_bs.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext.dropLast(1))
        }


        button_1.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "1")
        }

        button_2.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "2")
        }

        button_3.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "3")
        }

        button_4.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "4")
        }

        button_5.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "5")
        }

        button_6.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "6")
        }

        button_7.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "7")
        }

        button_8.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "8")
        }

        button_9.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "9")
        }

        button_0.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + "0")
        }

        button_dot.setOnClickListener {
            var tmptext = textView0.text.toString()
            textView0.setText(tmptext + ".")
        }

        button_eval.setOnClickListener {
            var opecode = textView0.text.toString()
            if (opecode != "") {
                opecode = opecode.trim()
            }
        }


    }




    fun textinputclear(){
        textView0.setText("")
    }

    fun ldisp(){
        if (mainstackp >= 3){
            textView4.setText(mainstack.get(3))
        } else {
            textView4.setText("")
        }
        if (mainstackp >= 2){
            textView3.setText(mainstack.get(2))
        } else {
            textView3.setText("")
        }
        if (mainstackp >= 1){
            textView2.setText(mainstack.get(1))
        } else {
            textView2.setText("")
        }
        if (mainstackp >= 0){
            textView1.setText(mainstack.get(0))
        } else {
            textView1.setText("")
        }
    }

}


(2021.10.20. 初版)

戻る