欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)簡單計算器實現(xiàn)代碼

 更新時間:2021年06月23日 10:33:44   作者:智能云  
這篇文章主要介紹了Android開發(fā)簡單計算器實現(xiàn),本文放置了完整的Android開發(fā)電腦,通過部署項目可以直接按到效果,希望本篇文章可以對你有所幫助

計算器項目,要求實現(xiàn)加、減、乘、除、求倒數(shù)、求平方根等簡單運算。

真機調(diào)試結(jié)果如下圖:

布局文件:main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:padding="3dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="簡單計算器"
                android:textColor="#000000"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_result"
                    android:background="#4E4B4B"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right|bottom"
                    android:lines="3"
                    android:maxLines="3"
                    android:scrollbars="vertical"
                    android:text="0"
                    android:textColor="#FFFFFF"
                    android:textSize="35sp" />
            </LinearLayout>

            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:columnCount="4"
                >

                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:shadowColor="@color/purple_500"
                    android:text="CE"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_divide"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="÷"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_multiply"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="×"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_clear"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="C"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_seven"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="7"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_eight"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="8"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp" />

                <Button
                    android:id="@+id/btn_nine"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="9"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_plus"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="+"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_four"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="4"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_five"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="5"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_six"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="6"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_minus"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_one"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_two"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_three"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <ImageButton
                    android:id="@+id/ib_sqrt"
                    android:layout_width="90dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:scaleType="centerInside"
                    android:src="@drawable/sqrt"
                    android:gravity="center"
                    android:layout_marginStart="4dp"/>

                <Button
                    android:id="@+id/btn_reciprocal"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="1/x"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_zero"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="0"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_dot"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="."
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>

                <Button
                    android:id="@+id/btn_equal"
                    android:layout_width="85dp"
                    android:layout_height="70dp"
                    android:backgroundTint="@color/gray"
                    android:gravity="center"
                    android:text="="
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:layout_marginStart="5dp"/>
            </GridLayout>

        </LinearLayout>
    </ScrollView>


</LinearLayout>

活動頁面:MainActivity.java

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.calculator.cal.CoreAlgorithm;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

        private TextView tv_result; // 聲明一個文本視圖對象
        private double result =0; // 當(dāng)前的計算結(jié)果
        private String showText = ""; // 顯示的文本內(nèi)容
        private final StringBuilder builder=new StringBuilder();  //存儲運算式子的字符串構(gòu)造器


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv_result = findViewById(R.id.tv_result);

            int[] buttonSeq={
                    R.id.btn_cancel, // “取消”按鈕
                    R.id.btn_divide,// “除法”按鈕
                    R.id.btn_multiply , // “乘法”按鈕
                    R.id.btn_clear , // “清除”按鈕
                    R.id.btn_seven , // 數(shù)字7
                    R.id.btn_eight , // 數(shù)字8
                    R.id.btn_nine , // 數(shù)字9
                    R.id.btn_plus , // “加法”按鈕
                    R.id.btn_four , // 數(shù)字4
                    R.id.btn_five , // 數(shù)字5
                    R.id.btn_six , // 數(shù)字6
                    R.id.btn_minus , // “減法”按鈕
                    R.id.btn_one , // 數(shù)字1
                    R.id.btn_two , // 數(shù)字2
                    R.id.btn_three , // 數(shù)字3
                    R.id.btn_reciprocal , // 求倒數(shù)按鈕
                    R.id.btn_zero , // 數(shù)字0
                    R.id.btn_dot , // “小數(shù)點”按鈕
                    R.id.btn_equal , // “等號”按鈕
                    R.id.ib_sqrt // “開平方”按鈕
            };

            for(int buttonId:buttonSeq)
                findViewById(buttonId).setOnClickListener(this);

            builder.append(0);
        }

//符號分類
        //數(shù)字
        //四則運算符
        //小數(shù)點
        //根號,倒數(shù),等號,直接出結(jié)果
        //CE C


        @Override
        public void onClick(View v) {
            int id=v.getId();
            String inputText="";

//        if(result!=0){          //如果結(jié)果不為0,則按照結(jié)果進行運算
//            clear();
//            builder.append(result);
//        }


            //若不為根號,因為根號按鈕無法取值
            if(id!=R.id.ib_sqrt) {
                inputText = ((TextView) v).getText().toString();

                //判斷是否為數(shù)字、小數(shù)點以及四則運算符

                if (inputText.matches("\\d|\\.")) {         //輸入是否為數(shù)字或點號
                    resultCheck();
                    if(builder.toString().equals("0")){
                        builder.deleteCharAt(builder.length()-1);}

                    builder.append(inputText);
                    if(verifyExp(builder.toString())){
                        refreshText(builder.toString());      //表達式正確刷新
                    }else{
                        builder.deleteCharAt(builder.length() - 1);  //表達式不正確刪除最后一位字符
                    }


                } else if (inputText.matches("\\+|-|×|÷")) {        //輸入為四則運算符

                    resultCheck();

                    builder.append(inputText);
                    if(verifyExp(builder.toString())){
                        refreshText(builder.toString());
                    }else{                                               //更替運算符操作
                        builder.deleteCharAt(builder.length() - 1);
                        builder.deleteCharAt(builder.length() - 1);
                        builder.append(inputText);
                        refreshText(builder.toString());
                    }

                }
                else {                          //點擊了CE C 1/X =

                    switch (inputText) {
                        case "CE":
                            resultCheck();
                            //有字符才能刪除
                            if (builder.length() > 0) {
                                builder.deleteCharAt(builder.length() - 1);
                                refreshText(builder.toString());
                            } else {

                                Toast.makeText(this, "沒有數(shù)字可刪了", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "C":
                            refreshText("");
                            result=0.0;
                            builder.delete(0, builder.length());    //builder清空
                            builder.append(0);
                            break;
                        case "1/x":
                            resultCheck();
                            result=1/(CoreAlgorithm.calExp(builder.toString()));
                            refreshText("1/("+builder.toString()+")=\n"+result);
                            break;
                        case "=":
                            resultCheck();
                            if(result==0.0) {
                                result = CoreAlgorithm.calExp(builder.toString());
//                                builder.append("=");//容易出錯 ,按等號會把這個式子進行運算
                                refreshText(builder.toString() + "=\n" + result);
                            }
                            break;
                        default:
                            Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
                    }

                }
            }else{      // 點擊了開根號,由于算法不支持有運算符在前,目前已經(jīng)支持
                resultCheck();  //經(jīng)過一次檢查結(jié)果被清零,結(jié)果存于builder中
                 result = Math.sqrt(CoreAlgorithm.calExp(builder.toString()));
                 refreshText("√(" + builder.toString() + ")=\n" + result);

            }

        }

        //檢查整個表達式
        public boolean verifyExp(String exp){       //驗證整個表達式是否合法
            String lastNum="";
            String[] sp=exp.split("\\+|-|×|÷");      //將操作數(shù)分割出來
            char lastChar=exp.charAt(exp.length()-1);       //獲得最后一個字符

            lastNum=sp[sp.length-1];     //取得最后一位操作數(shù)

            if(String.valueOf(lastChar).matches("\\+|-|×|÷"))  //如果當(dāng)前符號為四則運算符
            {
                lastNum="";
                return exp.matches(".*(\\d[+-×÷])|.*(\\.[+-×÷])");      //驗證最后的運算符是否符合只有一個原則

            }else{                                                          //最后一位為運算數(shù)
                return  lastNum.matches("^[-]?\\d*\\.?\\d*");   //驗證最后一位運算數(shù)是否合法

            }
        }


        // 刷新文本顯示
        private void refreshText(String text) {
            showText = text;
            tv_result.setText(showText);
        }

        // 清空并初始化
        private void clear() {
            builder.delete(0, builder.length());
            showText="";
        }

        //基本每個出結(jié)果的按鈕都要執(zhí)行一次這個函數(shù)
        public void resultCheck(){      //運算結(jié)果檢查,有結(jié)果用結(jié)果,結(jié)果不為數(shù)字進行處理
            if(result!=0){          //如果結(jié)果不為0,則按照結(jié)果進行運算
                String res=String.valueOf(result);
                if(res.matches("^[-]?\\d*\\.?\\d*")){   //若為浮點數(shù)字
                    clear();
                    builder.append(result);
                    result=0;    //結(jié)果不清零,檢查的時候就會一直重復(fù)放入結(jié)果
                }else{      //若結(jié)果為字母,分母為0會返回Infinity,以及負數(shù)開方
                    clear();
                    builder.append("0");
                    result=0;
                }
            }

            if(builder.length()==0){
                builder.append(0);
            }

        }

}

核心算法:CoreAlgorithm.java

package com.example.calculator.cal;


import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoreAlgorithm {

        private static final Stack<Double> st = new Stack<>();      //原始數(shù)字棧
        private static final Queue<Double> qu = new ArrayDeque<>();  //序列化數(shù)字隊列,
        private static final Queue<String> queOp = new ArrayDeque<>();        //符號隊列
        private static final Queue<String> newQueOp = new ArrayDeque<>();

        public static double calExp(String exp) {


//       String str="1+3*2-8/2+6";
//        String str="1+3*2/1";

            //本算法似乎不支持符號在前面,目前算法已經(jīng)改進


            //處理運算符在式子最后
            if (exp.matches(".*[\\+\\-×÷]")) {
                exp = exp.substring(0, exp.length() - 1);
            }

            String[] sp; //存放分割數(shù)組

            //運算符在式子最前面
            if (exp.matches("[\\+\\-×÷].*")) {
                String fistElem=exp.substring(0,1); //截取首個字符
                exp=exp.substring(1);       //舍去首個字符
                //分割字符,提取數(shù)字
                sp = exp.split("\\+|-|×|÷");
                if(fistElem.equals("-")){       //首個字符為負號
                    sp[0]="-"+sp[0];        //添加負號
                }
            }else{  //沒有符號在前
                sp = exp.split("\\+|-|×|÷");
            }

            //之前直接分割字符會導(dǎo)致,數(shù)組第一位為空,導(dǎo)致程序無法運行

            for (int i = sp.length - 1; i >= 0; i--) {
                if (sp[i].equals(".")) {
                    st.push(0.0);       //替換點號
                } else {
                    st.push(Double.parseDouble(sp[i]));
                }

            }

            //尋找匹配字符串
            Pattern p = Pattern.compile("\\+|-|×|÷");
            Matcher m = p.matcher(exp);
            while (m.find()) {

                queOp.add(m.group());
            }

//        for(int i=sp.length-1;i>=0;i--){
//           System.out.println(st.pop());
//        }

//       int size=queNum.size();
//       for(int i=0;i<size;i++)
//           System.out.println(queNum.poll());

            //運算降級序列化
            while (st.size() > 0) {

                String currOp;

                if (queOp.size() > 0) {
                    currOp = queOp.poll();
                } else {
                    currOp = "0";
                }

                switch (currOp) {
                    case "×":
                        st.push(st.pop() * st.pop());
                        break;
                    case "÷":
                        st.push(st.pop() / st.pop());
                        break;
                    case "+":
                        qu.add(st.pop());
                        newQueOp.add("+");
                        break;
                    case "-":
                        qu.add(st.pop());
                        newQueOp.add("-");
                        break;
                    default:
                        qu.add(st.pop());

                }
            }

            //正常運算
            if (qu.size() > 0) {
                double res = qu.poll();
                while (qu.size() > 0) {

                    String op = "";
                    if (newQueOp.size() > 0) {
                        op = newQueOp.poll();
                    } else {
                        op = "none";
                    }

                    switch (op) {
                        case "+":
                            res += qu.poll();
                            break;
                        case "-":
                            res -= qu.poll();
                            break;
                        default:
                            System.out.println("none");
                    }

                }

                return res;

            }

            return 0.0;
        }
}

到此這篇關(guān)于Android開發(fā)簡單計算器實現(xiàn)的文章就介紹到這了,更多相關(guān)Android開發(fā)計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot調(diào)用service層的三種方法

    SpringBoot調(diào)用service層的三種方法

    在Spring?Boot中,我們可以通過注入Service層對象來調(diào)用Service層的方法,Service層是業(yè)務(wù)邏輯的處理層,它通常包含了對數(shù)據(jù)的增刪改查操作,本文給大家介紹了SpringBoot調(diào)用service層的三種方法,需要的朋友可以參考下
    2024-05-05
  • InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    這篇文章主要介紹了InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • java實現(xiàn)坦克大戰(zhàn)小游戲

    java實現(xiàn)坦克大戰(zhàn)小游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)坦克大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • SpringBoot 配置文件加密的步驟

    SpringBoot 配置文件加密的步驟

    這篇文章主要介紹了SpringBoot 配置文件加密的步驟,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下
    2021-03-03
  • MapStruct到底是什么?

    MapStruct到底是什么?

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文中圍繞MapStruct到底是什么展開,文中有非常詳細的解釋及代碼示例,需要的朋友可以參考下
    2021-06-06
  • SpringMVC數(shù)據(jù)響應(yīng)詳細介紹

    SpringMVC數(shù)據(jù)響應(yīng)詳細介紹

    Spring MVC 是 Spring 提供的一個基于 MVC 設(shè)計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細,本章來講解SpringMVC數(shù)據(jù)響應(yīng)
    2023-02-02
  • SpringBoot在Controller層接收參數(shù)的n種姿勢(超詳細)

    SpringBoot在Controller層接收參數(shù)的n種姿勢(超詳細)

    這篇文章主要介紹了SpringBoot在Controller層接收參數(shù)的常用方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • SpringBoot整合MongoDB實現(xiàn)事務(wù)管理

    SpringBoot整合MongoDB實現(xiàn)事務(wù)管理

    Spring Boot是一種快速開發(fā)Spring應(yīng)用的方式,它提供了大量的自動配置和默認設(shè)置,以簡化開發(fā)流程,MongoDB是一個基于文檔的NoSQL數(shù)據(jù)庫,本文將介紹如何在Spring Boot應(yīng)用中整合MongoDB,并實現(xiàn)事務(wù)管理,需要的朋友可以參考下
    2024-07-07
  • Java連接Oracle數(shù)據(jù)庫并查詢

    Java連接Oracle數(shù)據(jù)庫并查詢

    這篇文章主要介紹了Java連接Oracle數(shù)據(jù)庫并查詢的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 在SpringBoot中無縫整合Dubbo的實現(xiàn)過程

    在SpringBoot中無縫整合Dubbo的實現(xiàn)過程

    微服務(wù)架構(gòu)已經(jīng)成為現(xiàn)代應(yīng)用開發(fā)的熱門趨勢,而Dubbo作為一款強大的分布式服務(wù)框架,與Spring?Boot的結(jié)合是構(gòu)建高性能微服務(wù)應(yīng)用的理想選擇,本文將詳細介紹如何在SpringBoot中無縫整合Dubbo,需要的朋友可以參考下
    2024-01-01

最新評論