Android?Studio開發(fā)實現(xiàn)簡單計算器功能
更新時間:2022年05月20日 11:07:46 作者:小和同學
這篇文章主要為大家詳細介紹了Android?Studio開發(fā)實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android Studio開發(fā)實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
activity_3.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".Activity3"> ? ? <RelativeLayout ? ? ? ? android:id="@+id/r1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="100dp" ? ? ? ? android:background="#1000" ? ? ? ? tools:ignore="MissingConstraints"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/text1" ? ? ? ? ? ? android:layout_margin="6dp" ? ? ? ? ? ? android:textSize="24sp" ? ? ? ? ? ? android:text=""> ? ? ? ? ? </TextView> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/textv" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? ? ? android:text="0" ? ? ? ? ? ? android:padding="20dp" ? ? ? ? ? ? android:textSize="30sp"/> ? ? </RelativeLayout> ? ? <RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:layout_marginTop="100dp" > ? ? ? ? <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"> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="22dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="%" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#A67D3D" ? ? ? ? ? ? ? ? ? ? android:textColor="#171616"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="22dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClicka" ? ? ? ? ? ? ? ? ? ? android:text="CE" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#A67D3D" ? ? ? ? ? ? ? ? ? ? android:textColor="#171616" ? ? ? ? ? ? ? ? ? ? tools:ignore="OnClick" /> ? ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="22dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? ? ? android:text="Back" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#A67D3D" ? ? ? ? ? ? ? ? ? ? android:textColor="#171616"/> ? ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:background="#E91E63" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#8E2323" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="÷" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? app:backgroundTint="#2196F3" /> ? ? ? ? ? ? </LinearLayout> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="0dp" ? ? ? ? ? ? ? ? android:layout_weight="1"> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="1" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="2" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="3" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#8E2323" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="×" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? app:backgroundTint="#9C27B0" /> ? ? ? ? ? ? ? </LinearLayout> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="0dp" ? ? ? ? ? ? ? ? android:layout_weight="1"> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="4" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="5" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="6" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="-" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#8E2323"/> ? ? ? ? ? ? </LinearLayout> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="0dp" ? ? ? ? ? ? ? ? android:layout_weight="1"> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="7" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="8" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="9" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="+" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#8E2323"/> ? ? ? ? ? ? </LinearLayout> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="0dp" ? ? ? ? ? ? ? ? android:layout_weight="1"> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="2" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="0" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:textSize="24dp" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="." ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#0000FF"/> ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? ? ? ? ? android:textSize="30dp" ? ? ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? ? ? android:onClick="onClick" ? ? ? ? ? ? ? ? ? ? android:text="=" ? ? ? ? ? ? ? ? ? ? android:backgroundTint="#8E2323"/> ? ? ? ? ? ? </LinearLayout> ? ? ? ? </LinearLayout> ? ? </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Activity3.java
package com.example.myapplication; ? import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.math.BigDecimal; import java.util.Stack; import java.util.ArrayList; import java.util.Iterator; import java.util.regex.Pattern; ? public class Activity3 extends AppCompatActivity { ? ? Stack<String> operator; ? ? //用于求解后綴表達式和計算 ? ? String memorry = "0"; ? ? ? //計算器M(R C + -)操作 ? ? int flag = 1; ? ? ? //判斷是否要完全修改顯示的值 ? ? boolean clearstr = false; ? //是否清除表達式的標志位 ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_3); ? ? ? ? operator = new Stack<>(); ? ? } ? ? ? //此方法用于計算,操作數(shù)分別為a和b,運算符為opr ? ? protected String operation(String a, String b, String opr){ ? ? ? ? String result = null; ? //存放運算結(jié)果 ? ? ? ? BigDecimal ope1 = new BigDecimal(a); ? ?//為了有較好的精度,選擇BigDecimal對象進行計算 ? ? ? ? BigDecimal ope2 = new BigDecimal(b); ? ? ? ? switch (opr){ ? ? ? ? ? ? case "+": result = ope1.add(ope2).stripTrailingZeros().toPlainString(); break; ? //stripTrailingZeros每一種運算都把小數(shù)點后多余的0去除 ? ? ? ? ? ? case "-": result = ope1.subtract(ope2).stripTrailingZeros().toPlainString(); break; ? ? //toPlainString不用科學計數(shù)法顯示 ? ? ? ? ? ? case "×": result = ope1.multiply(ope2).stripTrailingZeros().toPlainString(); break; ? ? ? ? ? ? case "÷": ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? result = ope1.divide(ope2, 8, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString(); ? ?//保留8位小數(shù),捕獲異常 ? ? ? ? ? ? ? ? }catch (Exception e){ ? ? ? ? ? ? ? ? ? ? result = "錯誤"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "^": result = String.valueOf(Math.pow(Double.parseDouble(a), Double.parseDouble(b))); break; //計算X的Y次方 ? ? ? ? ? ? case "`": result = String.valueOf(Math.pow(Double.parseDouble(a), (1 / Double.parseDouble(b)))) ; break; ? ?//計算X的1/Y次方 ? ? ? ? } ? ? ? ? if(result.equals("NaN") || result.equals("Infinity")) result = "錯誤"; ? //雖然java中無窮大和負數(shù)的開根號并不會出現(xiàn)錯誤,但為了方便操作,顯示提醒用戶錯誤 ? ? ? ? return replaces(result); ? ?//replaces方法去除多余的0 ? ? } ? ? ? //方法重載 ? ? //由于BigDecimal對象并沒有提供以下函數(shù),所以重載方法,采用double類型計算,但會丟失精度 ? ? protected String operation(String a, String str){ ? ? ? ? double result = 1; ? ? ? ? switch (str){ ? ? ? ? ? ? case "sin": result = Math.sin(Double.parseDouble(a)); break; ? ? ? ? ? ? case "cos": result = Math.cos(Double.parseDouble(a)); break; ? ? ? ? ? ? case "tan": result = Math.tan(Double.parseDouble(a)); break; ? ? ? ? ? ? case "n!": ? ? ? ? ? ? ? ? if(!a.contains(".")) ? ? ? ? ? ? ? ? ? ? for(int i = 2; i <= Integer.parseInt(a); i++){ ? ? ? ? ? ? ? ? ? ? ? ? result *= i; ? ? ? ? ? ? ? ? ? ? } break; ? ? ? ? } ? ? ? ? return replaces(String.valueOf(result)); ? ?//replaces方法去除多余的0 ? ? } ? ? ? //去掉多余的0 ? ? protected String replaces(String s){ ? ? ? ? if(s != null && s.indexOf(".") > 0){ ? ? ? ? ? ? s = s.replaceAll("0+?$", "");//去掉多余的0 ? ? ? ? ? ? s = s.replaceAll("[.]$", "");//如最后一位是.則去掉 ? ? ? ? } ? ? ? ? return s; ? ? } ? ? ? //Java沒有提供棧的清空函數(shù),需要自己定義 ? ? protected void clearStack(Stack<String> operator){ ? ? ? ? while(!operator.isEmpty()){ ? ? ? ? ? ? operator.pop(); ? ? ? ? } ? ? } ? ? //求兩個運算符的優(yōu)先級的相對大小 ? ? protected int priority(String a, String b) { ? ? ? ? int x = 0, y = 0; ? ? ? ? switch(a) { ? ? ? ? ? ? case "(":?? ?case ")": ? ? ? ? ? ? ? ? x = 3; break; ? ? ? ? ? ? case "^":?? ?case "`": ? ? ? ? ? ? ? ? x = 2; break; ? ? ? ? ? ? case "×":?? ?case "÷": ? ? ? ? ? ? ? ? x = 1; break; ? ? ? ? ? ? case "+":?? ?case "-": ? ? ? ? ? ? ? ? x = 0; break; ? ? ? ? } ? ? ? ? switch(b) { ? ? ? ? ? ? case "(":?? ?case ")": ? ? ? ? ? ? ? ? y = 3; break; ? ? ? ? ? ? case "^":?? ?case "`": ? ? ? ? ? ? ? ? y = 2; break; ? ? ? ? ? ? case "×":?? ?case "÷": ? ? ? ? ? ? ? ? y = 1; break; ? ? ? ? ? ? case "+":?? ?case "-": ? ? ? ? ? ? ? ? y = 0; break; ? ? ? ? } ? ? ? ? return x - y; ? ? } ? ? ? //求后綴表達式 ? ? protected String bolan(ArrayList<String> array) { ? ? ? ? StringBuffer stb = new StringBuffer(); ?//存放后綴表達式,需要頻繁修改字符串,使用StringBuffer ? ? ? ? Iterator<String> it = array.iterator(); //迭代器 ? ? ? ? Pattern pattern = Pattern.compile("[0-9]+.*[0-9]*"); ? ?//匹配非運算符的正則表達式 ? ? ? ? String op = null; ? //臨時存放array數(shù)組內(nèi)容 ? ? ? ? while(it.hasNext()) { ? //遍歷數(shù)組 ? ? ? ? ? ? op = it.next().toString(); ? ? ? ? ? ? /* ? ? ? ? ? ? 求后綴表達式過程: ? ? ? ? ? ? 1 如果是數(shù)字,直接加入后綴表達式 ? ? ? ? ? ? 2 如果是運算符: ? ? ? ? ? ? ? a.若為'(',入棧; ? ? ? ? ? ? ? b.若為')',依次把棧中的運算符加入后綴表達式,知道出現(xiàn)'(',并把'('出棧; ? ? ? ? ? ? ? c.若為'+' '-' '*' '/': ? ? ? ? ? ? ? ? 1)??眨霔?; ? ? ? ? ? ? ? ? 2)棧頂元素為'(',入棧; ? ? ? ? ? ? ? ? 3)高于棧頂元素優(yōu)先級,入棧; ? ? ? ? ? ? ? ? 4)否則,依次彈出棧頂運算符,直到一個優(yōu)先級比它低的運算符或'('為止 ? ? ? ? ? ? ? d.遍歷完成,若棧非空依次彈出所有元素 ? ? ? ? ? ? ?*/ ? ? ? ? ? ? switch(op) { ? ? ? ? ? ? ? ? case "(": operator.push(op); break; ? ? ? ? ? ? ? ? case ")": ? ? ? ? ? ? ? ? ? ? while(!operator.peek().equals("(")) { ? ? ? ? ? ? ? ? ? ? ? ? stb.append(operator.pop() + " ");//pop()移除一個元素并將該元素返回 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? operator.pop(); break; ? ? ? ? ? ? ? ? case "+": case "-": case "×": case "÷": case "^": case "`": ? ? ? ? ? ? ? ? ? ? if(operator.isEmpty() || operator.peek().equals("(") || priority(op, operator.peek())>0) { ? ? ? ? ? ? ? ? ? ? ? ? operator.push(op); ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? while(!operator.isEmpty() && priority(op, operator.peek()) <= 0 && !operator.peek().equals("(")) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? stb.append(operator.pop() + " "); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if(!operator.isEmpty() && operator.peek().equals("(")) operator.pop(); ? ? ? ? ? ? ? ? ? ? ? ? operator.push(op); ? ? ? ? ? ? ? ? ? ? }?? ?break; ? ? ? ? ? ? } ? ? ? ? ? ? if(pattern.matcher(op).matches()) { ? ? ? ? ? ? ? ? stb.append(op + " "); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? while(!operator.isEmpty()) { ? ? ? ? ? ? stb.append(operator.pop() + " "); ? ? ? ? } ? ? ? ? return stb.toString(); ? ? } ? ? ? //判斷字符是運算符還是數(shù)字的函數(shù) ? ? protected boolean isOperator(char c) { ? ? ? ? boolean flag = false; ? ? ? ? switch(c) { ? ? ? ? ? ? case '+': case '-': case '×': case '÷': case '(': case ')': case '^': case '`':flag = true; break; ? ? ? ? } ? ? ? ? return flag; ? ? } ? ? ? //把字符串切分到數(shù)組中 ? ? protected ArrayList<String> Split(String str) { ? ? ? ? ArrayList<String> list = new ArrayList<>(); ? ? ? ? StringBuffer sbf = new StringBuffer(); ? ? ? ? if(str.charAt(0) == '-') str = '0' + str; //如果字符以負號開始,則在字符串頭加入0 ? ? ? ? for(int i = 0; i < str.length(); i++) { //遍歷所有字符 ? ? ? ? ? ? if(isOperator(str.charAt(i))) { ? ? ? ? ? ? ? ? if(sbf.length()>0) list.add(sbf.toString()); //如果當前字符是運算符,把sbf的內(nèi)容作為整體作為List的一項 ? ? ? ? ? ? ? ? sbf.delete(0, sbf.length()); ? ?//清空sbf的內(nèi)容 ? ? ? ? ? ? ? ? list.add(String.valueOf(str.charAt(i))); ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? sbf.append(str.charAt(i)); //如果當前的字符不是運算符,就加入到sbf中,到下一個運算符出現(xiàn)為止,可以保證多位數(shù)的數(shù)字不被分割 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? if(sbf.length() > 0) list.add(sbf.toString()); //此時的sbf保存了最后一個操作數(shù),應(yīng)該加入List數(shù)組 ? ? ? ? return list; ? ? } ? ? ? //所有Button的綁定函數(shù) ? ? public void onClick(View view){ ? ? ? ? Button btn = (Button) view; ? ? //接收到的參數(shù)轉(zhuǎn)換為Button對象 ? ? ? ? TextView tv = (TextView) this.findViewById(R.id.textv); ? ? //獲取TextView組件 ? ? ? ? TextView tv1 = (TextView) this.findViewById(R.id.text1); ? ? ? ? String str = btn.getText().toString(); ? ? ?//獲取按鈕的文本以確定按下的是哪個按鈕 ? ? ? ? String text = tv.getText().toString(); ? ? ?//獲取顯示框的文本 ? ? ? ? if(clearstr){ ? ? ? ? ? ? clearstr = false; //此位為真,需要把輸入的表達式清空 ? ? ? ? ? ? tv1.setText(""); ? ? ? ? } ? ? ? ? String totalstr = tv1.getText().toString(); ? ? //獲取顯示在左上角的表達式字符串 ? ? ? ? String result = null, a, b; ? ? ? ? if(text.equals("錯誤")){ ? ? ?//出現(xiàn)除0錯誤后,無論點擊哪個按鈕都只做顯示文本0的操作 ? ? ? ? ? ? clearStack(operator); ? //所有的點擊操作都使計算器重置 ? ? ? ? ? ? tv.setText("0"); ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? if(str.equals("x^y")){ ? ? ?//計算器按鈕上的文本是多位字符,修改為一位方便操作 ? ? ? ? ? ? str = "^"; ? ? ? ? }else if(str.equals("x^1/y")){ ?//計算器按鈕上的文本是多位字符,修改為一位方便操作 ? ? ? ? ? ? str = "`"; ? ? ? ? } ? ? ? ? switch (str){ ? ? ? ? ? ? case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//flag為1,說明當前還沒輸入或上一次運算已經(jīng)結(jié)束,不需要再保存當前顯示的數(shù)字 ? ? ? ? ? ? ? ? ? ? result = str; ? ? ? ? ? ? ? ? ? ? if(str.equals("0")){ ? ? ? ? ? ? ? ? ? ? ? ? flag = 1; ? ? ? //如果點擊0,則flag保持不變,防止當前顯示0的情況下點擊0會出現(xiàn)多個0的情況 ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? flag = 0; ? ? ? //如果點擊的不是0,flag改為0 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else if(flag == 0){ ? ?//flag為0時,當前點擊的數(shù)字直接加到文本顯示的數(shù)字之后 ? ? ? ? ? ? ? ? ? ? result = text + str; ? ? ? ? ? ? ? ? } break; ? ? ? ? ? ? case "+": case "×": case "÷": case "^": case "`": ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//連續(xù)多次點擊符號按鈕,僅保留最后一次點擊的符號 ? ? ? ? ? ? ? ? ? ? if(totalstr.length() > 0 && !totalstr.endsWith("(")){ ?//把上一次點擊的符號去除后添加當前符號 ? ? ? ? ? ? ? ? ? ? ? ? int temp = totalstr.length(); ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr.substring(0, temp - 1) + str; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else if(!totalstr.endsWith(")")){ //以)結(jié)尾,點擊運算符后直接加入到表達式后面 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; ? ? ? ? ? ? ? ? }else totalstr = totalstr + str; ? ?//點擊了數(shù)字后又點了運算符,需要把數(shù)字和運算符都加入到表達式 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? if(totalstr.endsWith("(") && flag == 1){ ? ?//負數(shù)的輸入需要在負號前加上0,方便操作的統(tǒng)一 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + "0" + str; ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ? ? ? ? ? ? ? ? ? ? if(totalstr.length() > 0){ ?// 重復點擊了運算符,把上一次點擊的符號去除后添加當前符號 ? ? ? ? ? ? ? ? ? ? ? ? ? ? int temp = totalstr.length(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr.substring(0, temp - 1) + str; ? ? ? ? ? ? ? ? ? ? ? ? }else{ ?//負數(shù)的輸入需要在負號前加上0,方便操作的統(tǒng)一 ? ? ? ? ? ? ? ? ? ? ? ? ? ? totalstr = "0" + str; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }else if(!totalstr.endsWith(")")){ ?//輸入完了右括號,點擊負號直接加入表達式,否則把當前輸入的數(shù)字和負號一起加入表達式 ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; ? ? ? ? ? ? ? ? ? ? }else totalstr = totalstr + str; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "=": ? ? ? ? ? ? ? ? //點擊等號后把表達式轉(zhuǎn)化成后綴表達式,并求值 ? ? ? ? ? ? ? ? clearStack(operator); ? //求值之前先清空棧,防止出現(xiàn)錯誤 ? ? ? ? ? ? ? ? if(!totalstr.endsWith(")")){ ? ?//如果不是以右括號結(jié)尾,說明當前輸入的數(shù)并沒有加入到表達式中,需要加入后再進行操作 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text; ? ? ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? //運算過程中出現(xiàn)任何錯誤都在顯示框中顯示“錯誤” ? ? ? ? ? ? ? ? ? ? for(String z:bolan(Split(totalstr)).split(" ")) { //z為后綴表達式的每一項 ? ? ? ? ? ? ? ? ? ? ? ? /* ? ? ? ? ? ? ? ? ? ? ? ? 計算過程: 遇到運算符,則從棧中彈出兩個數(shù)進行運算,并把結(jié)果入棧; 遇到數(shù)字,入棧 ? ? ? ? ? ? ? ? ? ? ? ? ?*/ ? ? ? ? ? ? ? ? ? ? ? ? if(isOperator(z.charAt(0))) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? b = operator.pop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? a = operator.pop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = replaces(operation(a, b, z)); //出棧兩個數(shù)進行運算 ? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(result); ? ? ?//運算結(jié)果入棧 ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(z); ? //數(shù)字直接入棧 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }catch (Exception e){ ? ? ? ? ? ? ? ? ? ? tv.setText("錯誤"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? flag = 1; ? ? ?//點擊等號后下次點擊其他按鈕需要清空表達式 ? ? ? ? ? ? ? ? clearstr = true; break; ? ? ? ? ? ? case "(": ? ? ? ? ? ? ? ? if(totalstr.length() > 0 && !Character.isDigit(totalstr.charAt(totalstr.length() - 1))){ //表達式非空且以運算符結(jié)尾直接加入 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + str; ? ? ? ? ? ? ? ? }else if(totalstr.length() == 0) totalstr = str; ?//如果表達式為空,直接加入 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case ")": ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; //把當前數(shù)字加上右括號加入到表達式 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "CE": ? ? ? ? ? ? ? ? tv.setText("0"); ?//請零當前輸入的數(shù)字 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "C": ? ? ? ? ? ? ? ? tv1.setText(""); //重置 ? ? ? ? ? ? ? ? tv.setText("0"); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case ".": ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//flag為1時,按下數(shù)字需要更改文本框顯示按下的數(shù)字,按下小數(shù)點則顯示0加小數(shù)點 ? ? ? ? ? ? ? ? ? ? result = "0."; ? ? ? ? ? ? ? ? ? ? flag = 0; ? ? ? ? ? ? ? ? }else if(!text.contains(".")){ ?//當前顯示數(shù)字已經(jīng)含有小數(shù)點,不進行任何操作,否則加上小數(shù)點 ? ? ? ? ? ? ? ? ? ? result = text + str; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "Back": ? ? ? ? ? ? ? ? if(text.length() == 1 || flag == 1){ ? ?//如果只有1位或運算完成了,點擊Back直接顯示0 ? ? ? ? ? ? ? ? ? ? result = "0"; ? ? ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? int lenth = text.length(); ? ? ? ? ? ? ? ? ? ? result = text.substring(0, lenth - 1); ?//截取前n-1個字符 ? ? ? ? ? ? ? ? } break; ? ? ? ? ? ? case "√X": ? ? ? ? ? ? ? ? result = String.format("%f", Math.sqrt(Double.parseDouble(text))); ?//計算x平方根 ? ? ? ? ? ? ? ? if(result.equals("NaN")) result = "錯誤"; ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? result = replaces(result); ?//清除多余的0 ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "x2": ? ? ? ? ? ? ? ? result = String.format("%f",Math.pow(Double.parseDouble(text), 2)); //計算x平方 ? ? ? ? ? ? ? ? result = replaces(result); ?//清除多余的0 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "1/x": ? ? ? ? ? ? ? ? result = String.format("%f", 1 / Double.parseDouble(text)); ? ? ? ? ? ? ? ? if(result.equals("Infinity")) result = "錯誤"; ?//當出現(xiàn)1/0時結(jié)果為Infinity(無窮大),讓計算器顯示錯誤 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? result = replaces(result); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "%": ? ? ? ? ? ? ? ? result = operation(text, "100", "÷"); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "MR": ? ? ? ? ? ? ? ? tv.setText(memorry); ? ? ? ?//顯示內(nèi)存里存儲的數(shù) ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "MC": ? ? ? ? ? ? ? ? memorry = "0"; ? ? ?//內(nèi)存清零 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "M+": ? ? ? ? ? ? ? ? memorry = operation(memorry, text, "+"); ? ?//內(nèi)從中的數(shù)加上當前顯示的數(shù) ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "M-": ? ? ? ? ? ? ? ? memorry = operation(memorry, text, "-"); ? ?//內(nèi)從中的數(shù)減去當前顯示的數(shù) ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "sin": case "cos": case "tan": case "n!": ? ? ? ? ? ? ? ? result = operation(text, str); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if(result != null) ?tv.setText(result); ? ? } }
結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android Studio實現(xiàn)簡易進制轉(zhuǎn)換計算器
- Android studio制作簡易計算器功能
- android?studio實現(xiàn)簡單的計算器小功能
- Android studio實現(xiàn)簡易計算器App功能
- 用Android?studio實現(xiàn)簡易計算器功能
- Android?studio開發(fā)實現(xiàn)計算器功能
- Android?Studio實現(xiàn)簡易計算器設(shè)計
- android?studio?項目?:UI設(shè)計高精度實現(xiàn)簡單計算器
- Android studio設(shè)計簡易計算器
- Android?Studio實現(xiàn)簡單計算器開發(fā)
相關(guān)文章
Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實例詳解
這篇文章主要介紹了Android編程之絕對布局AbsoluteLayout和相對布局RelativeLayout實現(xiàn)方法,結(jié)合實例形式詳細分析了Android絕對布局AbsoluteLayout和相對布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下2015-12-12Android中使用OkHttp包處理HTTP的get和post請求的方法
OkHttp包為安卓開發(fā)中的HTTP協(xié)議網(wǎng)絡(luò)編程帶來了很大的便利,這里我們就來看一下最基本的、Android中使用OkHttp包處理HTTP的get和post請求的方法:2016-07-07Android中asset文件夾與raw文件夾的區(qū)別深入解析
本篇文章是對Android中的asset文件夾與raw文件夾區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06Android三方依賴沖突Gradle中exclude的使用
這篇文章主要介紹了Android三方依賴沖突Gradle中exclude的使用,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09