Android?Studio開發(fā)實現簡單計算器功能
更新時間:2022年05月20日 11:07:46 作者:小和同學
這篇文章主要為大家詳細介紹了Android?Studio開發(fā)實現簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android Studio開發(fā)實現簡單計算器的具體代碼,供大家參考,具體內容如下
代碼:
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<>();
? ? }
?
? ? //此方法用于計算,操作數分別為a和b,運算符為opr
? ? protected String operation(String a, String b, String opr){
? ? ? ? String result = null; ? //存放運算結果
? ? ? ? BigDecimal ope1 = new BigDecimal(a); ? ?//為了有較好的精度,選擇BigDecimal對象進行計算
? ? ? ? BigDecimal ope2 = new BigDecimal(b);
? ? ? ? switch (opr){
? ? ? ? ? ? case "+": result = ope1.add(ope2).stripTrailingZeros().toPlainString(); break; ? //stripTrailingZeros每一種運算都把小數點后多余的0去除
? ? ? ? ? ? case "-": result = ope1.subtract(ope2).stripTrailingZeros().toPlainString(); break; ? ? //toPlainString不用科學計數法顯示
? ? ? ? ? ? case "×": result = ope1.multiply(ope2).stripTrailingZeros().toPlainString(); break;
? ? ? ? ? ? case "÷":
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? result = ope1.divide(ope2, 8, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString(); ? ?//保留8位小數,捕獲異常
? ? ? ? ? ? ? ? }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中無窮大和負數的開根號并不會出現錯誤,但為了方便操作,顯示提醒用戶錯誤
? ? ? ? return replaces(result); ? ?//replaces方法去除多余的0
? ? }
?
? ? //方法重載
? ? //由于BigDecimal對象并沒有提供以下函數,所以重載方法,采用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沒有提供棧的清空函數,需要自己定義
? ? 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數組內容
? ? ? ? while(it.hasNext()) { ? //遍歷數組
? ? ? ? ? ? op = it.next().toString();
? ? ? ? ? ? /*
? ? ? ? ? ? 求后綴表達式過程:
? ? ? ? ? ? 1 如果是數字,直接加入后綴表達式
? ? ? ? ? ? 2 如果是運算符:
? ? ? ? ? ? ? a.若為'(',入棧;
? ? ? ? ? ? ? b.若為')',依次把棧中的運算符加入后綴表達式,知道出現'(',并把'('出棧;
? ? ? ? ? ? ? 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();
? ? }
?
? ? //判斷字符是運算符還是數字的函數
? ? protected boolean isOperator(char c) {
? ? ? ? boolean flag = false;
? ? ? ? switch(c) {
? ? ? ? ? ? case '+': case '-': case '×': case '÷': case '(': case ')': case '^': case '`':flag = true; break;
? ? ? ? }
? ? ? ? return flag;
? ? }
?
? ? //把字符串切分到數組中
? ? 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的內容作為整體作為List的一項
? ? ? ? ? ? ? ? sbf.delete(0, sbf.length()); ? ?//清空sbf的內容
? ? ? ? ? ? ? ? list.add(String.valueOf(str.charAt(i)));
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? sbf.append(str.charAt(i)); //如果當前的字符不是運算符,就加入到sbf中,到下一個運算符出現為止,可以保證多位數的數字不被分割
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(sbf.length() > 0) list.add(sbf.toString()); //此時的sbf保存了最后一個操作數,應該加入List數組
? ? ? ? return list;
? ? }
?
? ? //所有Button的綁定函數
? ? public void onClick(View view){
? ? ? ? Button btn = (Button) view; ? ? //接收到的參數轉換為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("錯誤")){ ? ? ?//出現除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,說明當前還沒輸入或上一次運算已經結束,不需要再保存當前顯示的數字
? ? ? ? ? ? ? ? ? ? result = str;
? ? ? ? ? ? ? ? ? ? if(str.equals("0")){
? ? ? ? ? ? ? ? ? ? ? ? flag = 1; ? ? ? //如果點擊0,則flag保持不變,防止當前顯示0的情況下點擊0會出現多個0的情況
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? flag = 0; ? ? ? //如果點擊的不是0,flag改為0
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }else if(flag == 0){ ? ?//flag為0時,當前點擊的數字直接加到文本顯示的數字之后
? ? ? ? ? ? ? ? ? ? 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(")")){ //以)結尾,點擊運算符后直接加入到表達式后面
? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str;
? ? ? ? ? ? ? ? }else totalstr = totalstr + str; ? ?//點擊了數字后又點了運算符,需要把數字和運算符都加入到表達式
? ? ? ? ? ? ? ? tv1.setText(totalstr);
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "-":
? ? ? ? ? ? ? ? if(totalstr.endsWith("(") && flag == 1){ ? ?//負數的輸入需要在負號前加上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{ ?//負數的輸入需要在負號前加上0,方便操作的統(tǒng)一
? ? ? ? ? ? ? ? ? ? ? ? ? ? totalstr = "0" + str;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }else if(!totalstr.endsWith(")")){ ?//輸入完了右括號,點擊負號直接加入表達式,否則把當前輸入的數字和負號一起加入表達式
? ? ? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text + str;
? ? ? ? ? ? ? ? ? ? }else totalstr = totalstr + str;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? tv1.setText(totalstr);
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "=":
? ? ? ? ? ? ? ? //點擊等號后把表達式轉化成后綴表達式,并求值
? ? ? ? ? ? ? ? clearStack(operator); ? //求值之前先清空棧,防止出現錯誤
? ? ? ? ? ? ? ? if(!totalstr.endsWith(")")){ ? ?//如果不是以右括號結尾,說明當前輸入的數并沒有加入到表達式中,需要加入后再進行操作
? ? ? ? ? ? ? ? ? ? totalstr = totalstr + text;
? ? ? ? ? ? ? ? ? ? tv1.setText(totalstr);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? //運算過程中出現任何錯誤都在顯示框中顯示“錯誤”
? ? ? ? ? ? ? ? ? ? for(String z:bolan(Split(totalstr)).split(" ")) { //z為后綴表達式的每一項
? ? ? ? ? ? ? ? ? ? ? ? /*
? ? ? ? ? ? ? ? ? ? ? ? 計算過程: 遇到運算符,則從棧中彈出兩個數進行運算,并把結果入棧; 遇到數字,入棧
? ? ? ? ? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? ? ? ? ? if(isOperator(z.charAt(0))) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? b = operator.pop();
? ? ? ? ? ? ? ? ? ? ? ? ? ? a = operator.pop();
? ? ? ? ? ? ? ? ? ? ? ? ? ? result = replaces(operation(a, b, z)); //出棧兩個數進行運算
? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(result); ? ? ?//運算結果入棧
? ? ? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? operator.push(z); ? //數字直接入棧
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }catch (Exception e){
? ? ? ? ? ? ? ? ? ? tv.setText("錯誤");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? flag = 1; ? ? ?//點擊等號后下次點擊其他按鈕需要清空表達式
? ? ? ? ? ? ? ? clearstr = true; break;
? ? ? ? ? ? case "(":
? ? ? ? ? ? ? ? if(totalstr.length() > 0 && !Character.isDigit(totalstr.charAt(totalstr.length() - 1))){ //表達式非空且以運算符結尾直接加入
? ? ? ? ? ? ? ? ? ? totalstr = totalstr + str;
? ? ? ? ? ? ? ? }else if(totalstr.length() == 0) totalstr = str; ?//如果表達式為空,直接加入
? ? ? ? ? ? ? ? tv1.setText(totalstr);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case ")":
? ? ? ? ? ? ? ? totalstr = totalstr + text + str; //把當前數字加上右括號加入到表達式
? ? ? ? ? ? ? ? tv1.setText(totalstr);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "CE":
? ? ? ? ? ? ? ? tv.setText("0"); ?//請零當前輸入的數字
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "C":
? ? ? ? ? ? ? ? tv1.setText(""); //重置
? ? ? ? ? ? ? ? tv.setText("0");
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case ".":
? ? ? ? ? ? ? ? if(flag == 1){ ? ? ?//flag為1時,按下數字需要更改文本框顯示按下的數字,按下小數點則顯示0加小數點
? ? ? ? ? ? ? ? ? ? result = "0.";
? ? ? ? ? ? ? ? ? ? flag = 0;
? ? ? ? ? ? ? ? }else if(!text.contains(".")){ ?//當前顯示數字已經含有小數點,不進行任何操作,否則加上小數點
? ? ? ? ? ? ? ? ? ? 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 = "錯誤"; ?//當出現1/0時結果為Infinity(無窮大),讓計算器顯示錯誤
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? result = replaces(result);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "%":
? ? ? ? ? ? ? ? result = operation(text, "100", "÷");
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "MR":
? ? ? ? ? ? ? ? tv.setText(memorry); ? ? ? ?//顯示內存里存儲的數
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "MC":
? ? ? ? ? ? ? ? memorry = "0"; ? ? ?//內存清零
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "M+":
? ? ? ? ? ? ? ? memorry = operation(memorry, text, "+"); ? ?//內從中的數加上當前顯示的數
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "M-":
? ? ? ? ? ? ? ? memorry = operation(memorry, text, "-"); ? ?//內從中的數減去當前顯示的數
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "sin": case "cos": case "tan": case "n!":
? ? ? ? ? ? ? ? result = operation(text, str);
? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if(result != null) ?tv.setText(result);
? ? }
}結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android ViewPager實現無限循環(huán)的實例
這篇文章主要介紹了Android ViewPager實現無限循環(huán)的實例的相關資料,需要的朋友可以參考下2017-07-07
Android選擇與上傳圖片之PictureSelector教程
這篇文章主要介紹了在Android中對于圖片的選擇與上傳方法,本文介紹了PictureSelector的相關使用教程,學習Android的同學進來看看吧2021-08-08
Android 開發(fā)仿簡書登錄框可刪除內容或顯示密碼框的內容
本文通過實例代碼給大家分享android開發(fā)中模仿簡書登錄框可刪除內容或顯示密碼框的內容,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2016-12-12
Android使用acoco統(tǒng)計代碼行覆蓋率介紹
大家好,本篇文章主要講的是Android使用acoco統(tǒng)計代碼行覆蓋率介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android使用setCustomTitle()方法自定義對話框標題
Android有自帶的對話框標題,但是不太美觀,如果要給彈出的對話框設置一個自定義的標題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來通過本文給大家介紹Android使用setCustomTitle()方法自定義對話框標題,感興趣的朋友一起學習吧2016-02-02

