Android?Studio開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
本文實(shí)例為大家分享了Android Studio開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(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; ? ? //用于求解后綴表達(dá)式和計(jì)算 ? ? String memorry = "0"; ? ? ? //計(jì)算器M(R C + -)操作 ? ? int flag = 1; ? ? ? //判斷是否要完全修改顯示的值 ? ? boolean clearstr = false; ? //是否清除表達(dá)式的標(biāo)志位 ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_3); ? ? ? ? operator = new Stack<>(); ? ? } ? ? ? //此方法用于計(jì)算,操作數(shù)分別為a和b,運(yùn)算符為opr ? ? protected String operation(String a, String b, String opr){ ? ? ? ? String result = null; ? //存放運(yùn)算結(jié)果 ? ? ? ? BigDecimal ope1 = new BigDecimal(a); ? ?//為了有較好的精度,選擇BigDecimal對(duì)象進(jìn)行計(jì)算 ? ? ? ? BigDecimal ope2 = new BigDecimal(b); ? ? ? ? switch (opr){ ? ? ? ? ? ? case "+": result = ope1.add(ope2).stripTrailingZeros().toPlainString(); break; ? //stripTrailingZeros每一種運(yùn)算都把小數(shù)點(diǎn)后多余的0去除 ? ? ? ? ? ? case "-": result = ope1.subtract(ope2).stripTrailingZeros().toPlainString(); break; ? ? //toPlainString不用科學(xué)計(jì)數(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 = "錯(cuò)誤"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "^": result = String.valueOf(Math.pow(Double.parseDouble(a), Double.parseDouble(b))); break; //計(jì)算X的Y次方 ? ? ? ? ? ? case "`": result = String.valueOf(Math.pow(Double.parseDouble(a), (1 / Double.parseDouble(b)))) ; break; ? ?//計(jì)算X的1/Y次方 ? ? ? ? } ? ? ? ? if(result.equals("NaN") || result.equals("Infinity")) result = "錯(cuò)誤"; ? //雖然java中無(wú)窮大和負(fù)數(shù)的開(kāi)根號(hào)并不會(huì)出現(xiàn)錯(cuò)誤,但為了方便操作,顯示提醒用戶錯(cuò)誤 ? ? ? ? return replaces(result); ? ?//replaces方法去除多余的0 ? ? } ? ? ? //方法重載 ? ? //由于BigDecimal對(duì)象并沒(méi)有提供以下函數(shù),所以重載方法,采用double類(lèi)型計(jì)算,但會(huì)丟失精度 ? ? 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沒(méi)有提供棧的清空函數(shù),需要自己定義 ? ? protected void clearStack(Stack<String> operator){ ? ? ? ? while(!operator.isEmpty()){ ? ? ? ? ? ? operator.pop(); ? ? ? ? } ? ? } ? ? //求兩個(gè)運(yùn)算符的優(yōu)先級(jí)的相對(duì)大小 ? ? 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; ? ? } ? ? ? //求后綴表達(dá)式 ? ? protected String bolan(ArrayList<String> array) { ? ? ? ? StringBuffer stb = new StringBuffer(); ?//存放后綴表達(dá)式,需要頻繁修改字符串,使用StringBuffer ? ? ? ? Iterator<String> it = array.iterator(); //迭代器 ? ? ? ? Pattern pattern = Pattern.compile("[0-9]+.*[0-9]*"); ? ?//匹配非運(yùn)算符的正則表達(dá)式 ? ? ? ? String op = null; ? //臨時(shí)存放array數(shù)組內(nèi)容 ? ? ? ? while(it.hasNext()) { ? //遍歷數(shù)組 ? ? ? ? ? ? op = it.next().toString(); ? ? ? ? ? ? /* ? ? ? ? ? ? 求后綴表達(dá)式過(guò)程: ? ? ? ? ? ? 1 如果是數(shù)字,直接加入后綴表達(dá)式 ? ? ? ? ? ? 2 如果是運(yùn)算符: ? ? ? ? ? ? ? a.若為'(',入棧; ? ? ? ? ? ? ? b.若為')',依次把棧中的運(yùn)算符加入后綴表達(dá)式,知道出現(xiàn)'(',并把'('出棧; ? ? ? ? ? ? ? c.若為'+' '-' '*' '/': ? ? ? ? ? ? ? ? 1)???,入棧; ? ? ? ? ? ? ? ? 2)棧頂元素為'(',入棧; ? ? ? ? ? ? ? ? 3)高于棧頂元素優(yōu)先級(jí),入棧; ? ? ? ? ? ? ? ? 4)否則,依次彈出棧頂運(yùn)算符,直到一個(gè)優(yōu)先級(jí)比它低的運(yùn)算符或'('為止 ? ? ? ? ? ? ? d.遍歷完成,若棧非空依次彈出所有元素 ? ? ? ? ? ? ?*/ ? ? ? ? ? ? switch(op) { ? ? ? ? ? ? ? ? case "(": operator.push(op); break; ? ? ? ? ? ? ? ? case ")": ? ? ? ? ? ? ? ? ? ? while(!operator.peek().equals("(")) { ? ? ? ? ? ? ? ? ? ? ? ? stb.append(operator.pop() + " ");//pop()移除一個(gè)元素并將該元素返回 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? 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(); ? ? } ? ? ? //判斷字符是運(yùn)算符還是數(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; //如果字符以負(fù)號(hào)開(kāi)始,則在字符串頭加入0 ? ? ? ? for(int i = 0; i < str.length(); i++) { //遍歷所有字符 ? ? ? ? ? ? if(isOperator(str.charAt(i))) { ? ? ? ? ? ? ? ? if(sbf.length()>0) list.add(sbf.toString()); //如果當(dāng)前字符是運(yùn)算符,把sbf的內(nèi)容作為整體作為L(zhǎng)ist的一項(xiàng) ? ? ? ? ? ? ? ? sbf.delete(0, sbf.length()); ? ?//清空sbf的內(nèi)容 ? ? ? ? ? ? ? ? list.add(String.valueOf(str.charAt(i))); ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? sbf.append(str.charAt(i)); //如果當(dāng)前的字符不是運(yùn)算符,就加入到sbf中,到下一個(gè)運(yùn)算符出現(xiàn)為止,可以保證多位數(shù)的數(shù)字不被分割 ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? if(sbf.length() > 0) list.add(sbf.toString()); //此時(shí)的sbf保存了最后一個(gè)操作數(shù),應(yīng)該加入List數(shù)組 ? ? ? ? return list; ? ? } ? ? ? //所有Button的綁定函數(shù) ? ? public void onClick(View view){ ? ? ? ? Button btn = (Button) view; ? ? //接收到的參數(shù)轉(zhuǎn)換為Button對(duì)象 ? ? ? ? TextView tv = (TextView) this.findViewById(R.id.textv); ? ? //獲取TextView組件 ? ? ? ? TextView tv1 = (TextView) this.findViewById(R.id.text1); ? ? ? ? String str = btn.getText().toString(); ? ? ?//獲取按鈕的文本以確定按下的是哪個(gè)按鈕 ? ? ? ? String text = tv.getText().toString(); ? ? ?//獲取顯示框的文本 ? ? ? ? if(clearstr){ ? ? ? ? ? ? clearstr = false; //此位為真,需要把輸入的表達(dá)式清空 ? ? ? ? ? ? tv1.setText(""); ? ? ? ? } ? ? ? ? String totalstr = tv1.getText().toString(); ? ? //獲取顯示在左上角的表達(dá)式字符串 ? ? ? ? String result = null, a, b; ? ? ? ? if(text.equals("錯(cuò)誤")){ ? ? ?//出現(xiàn)除0錯(cuò)誤后,無(wú)論點(diǎn)擊哪個(gè)按鈕都只做顯示文本0的操作 ? ? ? ? ? ? clearStack(operator); ? //所有的點(diǎn)擊操作都使計(jì)算器重置 ? ? ? ? ? ? tv.setText("0"); ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? if(str.equals("x^y")){ ? ? ?//計(jì)算器按鈕上的文本是多位字符,修改為一位方便操作 ? ? ? ? ? ? str = "^"; ? ? ? ? }else if(str.equals("x^1/y")){ ?//計(jì)算器按鈕上的文本是多位字符,修改為一位方便操作 ? ? ? ? ? ? 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,說(shuō)明當(dāng)前還沒(méi)輸入或上一次運(yùn)算已經(jīng)結(jié)束,不需要再保存當(dāng)前顯示的數(shù)字 ? ? ? ? ? ? ? ? ? ? result = str; ? ? ? ? ? ? ? ? ? ? if(str.equals("0")){ ? ? ? ? ? ? ? ? ? ? ? ? flag = 1; ? ? ? //如果點(diǎn)擊0,則flag保持不變,防止當(dāng)前顯示0的情況下點(diǎn)擊0會(huì)出現(xiàn)多個(gè)0的情況 ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? flag = 0; ? ? ? //如果點(diǎn)擊的不是0,flag改為0 ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else if(flag == 0){ ? ?//flag為0時(shí),當(dāng)前點(diǎn)擊的數(shù)字直接加到文本顯示的數(shù)字之后 ? ? ? ? ? ? ? ? ? ? result = text + str; ? ? ? ? ? ? ? ? } break; ? ? ? ? ? ? case "+": case "×": case "÷": case "^": case "`": ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//連續(xù)多次點(diǎn)擊符號(hào)按鈕,僅保留最后一次點(diǎn)擊的符號(hào) ? ? ? ? ? ? ? ? ? ? if(totalstr.length() > 0 && !totalstr.endsWith("(")){ ?//把上一次點(diǎn)擊的符號(hào)去除后添加當(dāng)前符號(hào) ? ? ? ? ? ? ? ? ? ? ? ? int temp = totalstr.length(); ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr.substring(0, temp - 1) + str; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else if(!totalstr.endsWith(")")){ //以)結(jié)尾,點(diǎn)擊運(yùn)算符后直接加入到表達(dá)式后面 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; ? ? ? ? ? ? ? ? }else totalstr = totalstr + str; ? ?//點(diǎn)擊了數(shù)字后又點(diǎn)了運(yùn)算符,需要把數(shù)字和運(yùn)算符都加入到表達(dá)式 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? if(totalstr.endsWith("(") && flag == 1){ ? ?//負(fù)數(shù)的輸入需要在負(fù)號(hào)前加上0,方便操作的統(tǒng)一 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + "0" + str; ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ? ? ? ? ? ? ? ? ? ? if(totalstr.length() > 0){ ?// 重復(fù)點(diǎn)擊了運(yùn)算符,把上一次點(diǎn)擊的符號(hào)去除后添加當(dāng)前符號(hào) ? ? ? ? ? ? ? ? ? ? ? ? ? ? int temp = totalstr.length(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr.substring(0, temp - 1) + str; ? ? ? ? ? ? ? ? ? ? ? ? }else{ ?//負(fù)數(shù)的輸入需要在負(fù)號(hào)前加上0,方便操作的統(tǒng)一 ? ? ? ? ? ? ? ? ? ? ? ? ? ? totalstr = "0" + str; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }else if(!totalstr.endsWith(")")){ ?//輸入完了右括號(hào),點(diǎn)擊負(fù)號(hào)直接加入表達(dá)式,否則把當(dāng)前輸入的數(shù)字和負(fù)號(hào)一起加入表達(dá)式 ? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; ? ? ? ? ? ? ? ? ? ? }else totalstr = totalstr + str; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "=": ? ? ? ? ? ? ? ? //點(diǎn)擊等號(hào)后把表達(dá)式轉(zhuǎn)化成后綴表達(dá)式,并求值 ? ? ? ? ? ? ? ? clearStack(operator); ? //求值之前先清空棧,防止出現(xiàn)錯(cuò)誤 ? ? ? ? ? ? ? ? if(!totalstr.endsWith(")")){ ? ?//如果不是以右括號(hào)結(jié)尾,說(shuō)明當(dāng)前輸入的數(shù)并沒(méi)有加入到表達(dá)式中,需要加入后再進(jìn)行操作 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text; ? ? ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? //運(yùn)算過(guò)程中出現(xiàn)任何錯(cuò)誤都在顯示框中顯示“錯(cuò)誤” ? ? ? ? ? ? ? ? ? ? for(String z:bolan(Split(totalstr)).split(" ")) { //z為后綴表達(dá)式的每一項(xiàng) ? ? ? ? ? ? ? ? ? ? ? ? /* ? ? ? ? ? ? ? ? ? ? ? ? 計(jì)算過(guò)程: 遇到運(yùn)算符,則從棧中彈出兩個(gè)數(shù)進(jìn)行運(yùn)算,并把結(jié)果入棧; 遇到數(shù)字,入棧 ? ? ? ? ? ? ? ? ? ? ? ? ?*/ ? ? ? ? ? ? ? ? ? ? ? ? if(isOperator(z.charAt(0))) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? b = operator.pop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? a = operator.pop(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = replaces(operation(a, b, z)); //出棧兩個(gè)數(shù)進(jìn)行運(yùn)算 ? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(result); ? ? ?//運(yùn)算結(jié)果入棧 ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(z); ? //數(shù)字直接入棧 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }catch (Exception e){ ? ? ? ? ? ? ? ? ? ? tv.setText("錯(cuò)誤"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? flag = 1; ? ? ?//點(diǎn)擊等號(hào)后下次點(diǎn)擊其他按鈕需要清空表達(dá)式 ? ? ? ? ? ? ? ? clearstr = true; break; ? ? ? ? ? ? case "(": ? ? ? ? ? ? ? ? if(totalstr.length() > 0 && !Character.isDigit(totalstr.charAt(totalstr.length() - 1))){ //表達(dá)式非空且以運(yùn)算符結(jié)尾直接加入 ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + str; ? ? ? ? ? ? ? ? }else if(totalstr.length() == 0) totalstr = str; ?//如果表達(dá)式為空,直接加入 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case ")": ? ? ? ? ? ? ? ? totalstr = totalstr + text + str; //把當(dāng)前數(shù)字加上右括號(hào)加入到表達(dá)式 ? ? ? ? ? ? ? ? tv1.setText(totalstr); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "CE": ? ? ? ? ? ? ? ? tv.setText("0"); ?//請(qǐng)零當(dāng)前輸入的數(shù)字 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "C": ? ? ? ? ? ? ? ? tv1.setText(""); //重置 ? ? ? ? ? ? ? ? tv.setText("0"); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case ".": ? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//flag為1時(shí),按下數(shù)字需要更改文本框顯示按下的數(shù)字,按下小數(shù)點(diǎn)則顯示0加小數(shù)點(diǎn) ? ? ? ? ? ? ? ? ? ? result = "0."; ? ? ? ? ? ? ? ? ? ? flag = 0; ? ? ? ? ? ? ? ? }else if(!text.contains(".")){ ?//當(dāng)前顯示數(shù)字已經(jīng)含有小數(shù)點(diǎn),不進(jìn)行任何操作,否則加上小數(shù)點(diǎn) ? ? ? ? ? ? ? ? ? ? result = text + str; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "Back": ? ? ? ? ? ? ? ? if(text.length() == 1 || flag == 1){ ? ?//如果只有1位或運(yùn)算完成了,點(diǎn)擊Back直接顯示0 ? ? ? ? ? ? ? ? ? ? result = "0"; ? ? ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? int lenth = text.length(); ? ? ? ? ? ? ? ? ? ? result = text.substring(0, lenth - 1); ?//截取前n-1個(gè)字符 ? ? ? ? ? ? ? ? } break; ? ? ? ? ? ? case "√X": ? ? ? ? ? ? ? ? result = String.format("%f", Math.sqrt(Double.parseDouble(text))); ?//計(jì)算x平方根 ? ? ? ? ? ? ? ? if(result.equals("NaN")) result = "錯(cuò)誤"; ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? result = replaces(result); ?//清除多余的0 ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "x2": ? ? ? ? ? ? ? ? result = String.format("%f",Math.pow(Double.parseDouble(text), 2)); //計(jì)算x平方 ? ? ? ? ? ? ? ? result = replaces(result); ?//清除多余的0 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "1/x": ? ? ? ? ? ? ? ? result = String.format("%f", 1 / Double.parseDouble(text)); ? ? ? ? ? ? ? ? if(result.equals("Infinity")) result = "錯(cuò)誤"; ?//當(dāng)出現(xiàn)1/0時(shí)結(jié)果為Infinity(無(wú)窮大),讓計(jì)算器顯示錯(cuò)誤 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? result = replaces(result); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "%": ? ? ? ? ? ? ? ? result = operation(text, "100", "÷"); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "MR": ? ? ? ? ? ? ? ? tv.setText(memorry); ? ? ? ?//顯示內(nèi)存里存儲(chǔ)的數(shù) ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "MC": ? ? ? ? ? ? ? ? memorry = "0"; ? ? ?//內(nèi)存清零 ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "M+": ? ? ? ? ? ? ? ? memorry = operation(memorry, text, "+"); ? ?//內(nèi)從中的數(shù)加上當(dāng)前顯示的數(shù) ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "M-": ? ? ? ? ? ? ? ? memorry = operation(memorry, text, "-"); ? ?//內(nèi)從中的數(shù)減去當(dāng)前顯示的數(shù) ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case "sin": case "cos": case "tan": case "n!": ? ? ? ? ? ? ? ? result = operation(text, str); ? ? ? ? ? ? ? ? flag = 1; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if(result != null) ?tv.setText(result); ? ? } }
結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)易進(jìn)制轉(zhuǎn)換計(jì)算器
- Android studio制作簡(jiǎn)易計(jì)算器功能
- android?studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小功能
- Android studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App功能
- 用Android?studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Android?studio開(kāi)發(fā)實(shí)現(xiàn)計(jì)算器功能
- Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì)
- android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android?Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器開(kāi)發(fā)
相關(guān)文章
Android自定義StepView仿外賣(mài)配送進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義StepView仿外賣(mài)配送進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05android通過(guò)led實(shí)現(xiàn)手電筒功能
這篇文章主要為大家詳細(xì)介紹了android通過(guò)led實(shí)現(xiàn)手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)例詳解
這篇文章主要介紹了Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下2015-12-12Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法
OkHttp包為安卓開(kāi)發(fā)中的HTTP協(xié)議網(wǎng)絡(luò)編程帶來(lái)了很大的便利,這里我們就來(lái)看一下最基本的、Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法:2016-07-07Android手機(jī)衛(wèi)士之設(shè)置密碼對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)衛(wèi)士之設(shè)置密碼對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android中asset文件夾與raw文件夾的區(qū)別深入解析
本篇文章是對(duì)Android中的asset文件夾與raw文件夾區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android三方依賴(lài)沖突Gradle中exclude的使用
這篇文章主要介紹了Android三方依賴(lài)沖突Gradle中exclude的使用,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Android實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07