如何不在计算器应用程序中重复两次数学运算? [英] How not to repeat math operations two times in calculator app?

查看:56
本文介绍了如何不在计算器应用程序中重复两次数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在科特林"中制作了一个简单的计算器,使用Android Studio 我遇到的问题并且我没有解决的方法是在键入数字后如何不重复数学运算..也许是因为我是Android应用程序开发领域的新手,我不知道应该怎么做避免这个问题.

I have made a simple calculator in "Kotlin" using an android studio the problem I got and I don't have a way to fix it is how not to repeat the math operations after typing a number .. Perhaps because I am new to the world of Android application development and I do not know the way I should avoid this problem.

我的意思的例子,他两次输入了加/减:

Example of what I mean, He entered addition/subtraction twice:

我的代码: Main.kt

  package com.iosmostafa.calculator
    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import com.iosmostafa.calculator.R
    import kotlinx.android.synthetic.main.activity_main.*
    import net.objecthunter.exp4j.ExpressionBuilder
    
    class Main : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            //Numbers
            tvOne.setOnClickListener { appendOnExpresstion("1", true) }
            tvTwo.setOnClickListener { appendOnExpresstion("2", true) }
            tvThree.setOnClickListener { appendOnExpresstion("3", true) }
            tvFour.setOnClickListener { appendOnExpresstion("4", true) }
            tvFive.setOnClickListener { appendOnExpresstion("5", true) }
            tvSix.setOnClickListener { appendOnExpresstion("6", true) }
            tvSeven.setOnClickListener { appendOnExpresstion("7", true) }
            tvEight.setOnClickListener { appendOnExpresstion("8", true) }
            tvNine.setOnClickListener { appendOnExpresstion("9", true) }
            tvZero.setOnClickListener { appendOnExpresstion("0", true) }
            tvDot.setOnClickListener { appendOnExpresstion(".", true) }
    
    
    
            //Operators
            tvPlus.setOnClickListener { appendOnExpresstion("+", false) }
            tvMinus.setOnClickListener { appendOnExpresstion("-", false) }
            tvMul.setOnClickListener { appendOnExpresstion("*", false) }
            tvDivide.setOnClickListener { appendOnExpresstion("/", false) }
            tvOpen.setOnClickListener { appendOnExpresstion("(", false) }
            tvClose.setOnClickListener { appendOnExpresstion(")", false) }
            tvNew1.setOnClickListener{(appendOnExpresstion("%",false))}
    
            tvClear.setOnClickListener {
                tvExpression.text = ""
                tvResult.text = ""
            }
    
            tvBack.setOnClickListener {
                val string = tvExpression.text.toString()
                if(string.isNotEmpty()){
                    tvExpression.text = string.substring(0,string.length-1)
                }
                tvResult.text = ""
            }
    
    
    
            tvEquals.setOnClickListener {
                try {
    
                    val expression = ExpressionBuilder(tvExpression.text.toString()).build()
                    val result = expression.evaluate()
                    val longResult = result.toLong()
                    if(result == longResult.toDouble())
                        tvResult.text = longResult.toString()
                    else
                        tvResult.text = result.toString()
    
                }catch (e:Exception){
                    Log.d("Exception"," message : " + e.message )
                }
            }
    
    
        }
    
        fun appendOnExpresstion(string: String, canClear: Boolean) {
    
            if(tvResult.text.isNotEmpty()){
                tvExpression.text = ""
            }
    
            if (canClear) {
                tvResult.text = ""
                tvExpression.append(string)
            } else {
                tvExpression.append(tvResult.text)
                tvExpression.append(string)
                tvResult.text = ""
            }
        }
    }

activity_main.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvExpression"
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:ellipsize="start"
        android:gravity="end"
        android:singleLine="true"
        android:textColor="@color/numberButton"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="100sp"
        android:ellipsize="end"
        android:gravity="end"
        android:singleLine="true"
        android:textColor="@color/numberButton"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvClear"
                style="@style/ActionButtonStyle"
                android:text="حذف" />

            <TextView
                android:id="@+id/tvOpen"
                style="@style/ActionButtonStyle"
                android:text="(" />

            <TextView
                android:id="@+id/tvClose"
                style="@style/ActionButtonStyle"
                android:text=")" />

            <TextView
                android:id="@+id/tvNew1"
                style="@style/ActionButtonStyle"
                android:text="%" />

            <TextView
                android:id="@+id/tvDivide"
                style="@style/ActionButtonStyle"
                android:text="/" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvSeven"
                style="@style/NumberButtonStyle"
                android:text="7" />

            <TextView
                android:id="@+id/tvEight"
                style="@style/NumberButtonStyle"
                android:text="8" />

            <TextView
                android:id="@+id/tvNine"
                style="@style/NumberButtonStyle"
                android:text="9" />

            <TextView
                android:id="@+id/tvMul"
                style="@style/ActionButtonStyle"
                android:text="X" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/tvFour"
                style="@style/NumberButtonStyle"
                android:text="4" />

            <TextView
                android:id="@+id/tvFive"
                style="@style/NumberButtonStyle"
                android:text="5" />

            <TextView
                android:id="@+id/tvSix"
                style="@style/NumberButtonStyle"
                android:text="6" />

            <TextView
                android:id="@+id/tvMinus"
                style="@style/ActionButtonStyle"
                android:text="-" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/tvOne"
                style="@style/NumberButtonStyle"
                android:text="1" />

            <TextView
                android:id="@+id/tvTwo"
                style="@style/NumberButtonStyle"
                android:text="2" />

            <TextView
                android:id="@+id/tvThree"
                style="@style/NumberButtonStyle"
                android:text="3" />

            <TextView
                android:id="@+id/tvPlus"
                style="@style/ActionButtonStyle"
                android:text="+" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvDot"
                style="@style/NumberButtonStyle"
                android:text="." />

            <TextView
                android:id="@+id/tvZero"
                style="@style/NumberButtonStyle"
                android:text="0" />

            <ImageView
                android:id="@+id/tvBack"
                style="@style/NumberButtonStyle"
                android:scaleType="center"
                android:src="@drawable/backspace" />

            <TextView
                android:id="@+id/tvEquals"
                style="@style/ActionButtonStyle"
                android:text="=" />

        </LinearLayout>


    </LinearLayout>


</LinearLayout>

推荐答案

如果您不想彼此相邻添加两个运算符,则必须检查最后一个字符是否为运算符.看起来像这样:

If You don't want to add two operators next to each other You have to check if last character is the operator. It will look something like this:

fun appendOnExpresstion(string: String, canClear: Boolean)
{
    if (tvResult.text.isNotEmpty())
    {
        tvExpression.text = ""
    }
    if (canClear)
    {
        tvResult.text = ""
        tvExpression.append(string)
    }
    else
    {
        if (tvExpression.text.lastOrNull() !in arrayOf('+', '-', '*', '/')) // You are adding operator so You have to check if last char is oparetor
        {
            tvExpression.append(tvResult.text)
            tvExpression.append(string)
        }
        tvResult.text = ""
    }
}

这篇关于如何不在计算器应用程序中重复两次数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆