日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android實現計算器(計算表達式/計算小數點以及括號)

瀏覽:24日期:2022-09-22 11:52:58

本文實例為大家分享了Android實現計算器的具體代碼,供大家參考,具體內容如下

布局代碼:

<?xml version='1.0' encoding='utf-8'?><TableLayout 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='wrap_content' android:stretchColumns='4' android:focusable='true' android:focusableInTouchMode='true' tools:context='.MainActivity'> <TableRow android:layout_width='match_parent' android:layout_height='wrap_content' android:background='#aaaaaa'> <LinearLayout android:layout_width='0dp' android:layout_height='200dp' android:layout_column='0' android:gravity='right' android:layout_weight='1'> <EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_gravity='bottom|right' android:background='@null' android:hint='0' android:textSize='20pt' /> </LinearLayout> </TableRow> <!--計算表達式輸入框--> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_weight='1' android:text='C' android:textColor='#ff0000' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='1' android:layout_weight='1' android:text='CE' android:textColor='#ff0000' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='2' android:layout_weight='1' android:text='%' android:textColor='#ff0000' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='3' android:layout_weight='1' android:text='/' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_span='1' android:layout_weight='1' android:text='(' android:textColor='#ff0000' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_span='1' android:layout_weight='1' android:text=')' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_weight='1' android:text='7' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='1' android:layout_weight='1' android:text='8' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='2' android:layout_weight='1' android:text='9' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='3' android:layout_weight='1' android:text='*' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_weight='1' android:text='4' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='1' android:layout_weight='1' android:text='5' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='2' android:layout_weight='1' android:text='6' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='3' android:layout_weight='1' android:text='-' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_weight='1' android:text='1' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='1' android:layout_weight='1' android:text='2' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='2' android:layout_weight='1' android:text='3' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='3' android:layout_weight='1' android:text='+' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> <TableRow> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='0' android:layout_weight='1' android:text='.' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='1' android:layout_weight='1' android:text='0' android:textSize='12pt' /> <Button android: android:layout_width='0dp' android:layout_height='70dp' android:layout_column='2' android:layout_weight='2' android:text='=' android:textColor='#ff0000' android:textSize='12pt' /> </TableRow> </TableLayout>

Activicy代碼:

package com.example.newcalculator; import androidx.appcompat.app.AppCompatActivity; import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast; import java.lang.reflect.Type; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ /*按鈕定義*/ Button btn_one; //1 Button btn_two; //2 Button btn_three; //3 Button btn_four; //4 Button btn_five; //5 Button btn_six; //6 Button btn_seven; //7 Button btn_eight; //8 Button btn_nine; //9 Button btn_zero; //0 Button btn_c; //c Button btn_ce; //ce Button btn_aliquot; //% Button btn_divide; //除號 Button btn_multiply;//x Button btn_subtract;//- Button btn_add; //+ Button btn_point; //. Button btn_equal; //= Button btn_leftBracket;//( Button btn_rightBracket;//) EditText contentBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); initClickEvent(); } //找到控件 private void initControls() { btn_one = findViewById(R.id.btn_one); btn_two = findViewById(R.id.btn_two); btn_three = findViewById(R.id.btn_three); btn_four = findViewById(R.id.btn_four); btn_five = findViewById(R.id.btn_five); btn_six = findViewById(R.id.btn_six); btn_seven = findViewById(R.id.btn_seven); btn_eight = findViewById(R.id.btn_eight); btn_nine = findViewById(R.id.btn_nine); btn_zero = findViewById(R.id.btn_zero); btn_c = findViewById(R.id.btn_c); btn_ce = findViewById(R.id.btn_ce); btn_aliquot = findViewById(R.id.btn_aliquot); btn_divide = findViewById(R.id.btn_divide); btn_multiply = findViewById(R.id.btn_multiply); btn_subtract = findViewById(R.id.btn_subtract); btn_add = findViewById(R.id.btn_add); btn_point = findViewById(R.id.btn_point); btn_equal = findViewById(R.id.btn_equal); contentBox = findViewById(R.id.content_box); btn_leftBracket = findViewById(R.id.btn_leftBracket); btn_rightBracket = findViewById(R.id.btn_rightBracket); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_one:{ changeVal('1'); }break; case R.id.btn_two:{ changeVal('2'); }break; case R.id.btn_three:{ changeVal('3'); }break; case R.id.btn_four:{ changeVal('4'); }break; case R.id.btn_five:{ changeVal('5'); }break; case R.id.btn_six:{ changeVal('6'); }break; case R.id.btn_seven:{ changeVal('7'); }break; case R.id.btn_eight:{ changeVal('8'); }break; case R.id.btn_nine:{ changeVal('9'); }break; case R.id.btn_zero:{ changeVal('0'); }break; case R.id.btn_aliquot:{ changeVal('%'); }break; case R.id.btn_divide:{ changeVal('/'); }break; case R.id.btn_multiply:{ changeVal('*'); }break; case R.id.btn_subtract:{ changeVal('-'); }break; case R.id.btn_add:{ changeVal('+'); }break; case R.id.btn_rightBracket:{ changeVal(')'); }break; case R.id.btn_leftBracket:{ changeVal('('); }break; case R.id.btn_c:{ funC(); }break; case R.id.btn_ce:{ funClearAll(); }break; case R.id.btn_point:{ changeVal('.'); }break; case R.id.btn_equal:{ String str = contentBox.getText().toString(); Calculator calculator = new Calculator(); Double result = calculator.Eval(str); contentBox.setText(result.toString()); }break; } } private void changeVal(String flag){ String str = contentBox.getText().toString(); contentBox.setText(str+flag); } private void funC(){ String str = contentBox.getText().toString(); str = str.substring(0,str.length()-1); contentBox.setText(str); } private void funClearAll(){ contentBox.setText(''); } private void initClickEvent(){ btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); btn_four.setOnClickListener(this); btn_five.setOnClickListener(this); btn_six.setOnClickListener(this); btn_seven.setOnClickListener(this); btn_eight.setOnClickListener(this); btn_nine.setOnClickListener(this); btn_zero.setOnClickListener(this); btn_aliquot.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_subtract.setOnClickListener(this); btn_point.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_add.setOnClickListener(this); btn_c.setOnClickListener(this); btn_ce.setOnClickListener(this); btn_rightBracket.setOnClickListener(this); btn_leftBracket.setOnClickListener(this); }}

計算表達式代碼:

package com.example.newcalculator; import java.util.*; //計算 2*(4+(88-86)/2) //計算 2*(4+(88-86)/2) class Caculater { private String[] sArry;//存分割后的字符串 private Stack<String> houx = new Stack<String>(); private Stack<String> fuhao = new Stack<String>(); //結構初始化 Caculater(String str) { int i = str.length() - 1; String temp = ''; int j = 0; Boolean bool = true; //在符號左右各添加一個#字符劃分 while (true) { if (!bool) break; if (i == j) { bool = false; } if (str.charAt(j) == ’+’ || str.charAt(j) == ’-’ || str.charAt(j) == ’*’ || str.charAt(j) == ’/’ || str.charAt(j) == ’(’ || str.charAt(j) == ’)’) { temp += ’#’; temp += str.charAt(j); temp += ’#’; //填完后是2#*##(#4#+##(#88#-#86#)##/#32#)# } else { temp += str.charAt(j); } j++; } sArry = temp.split('#+');//用正則表達式分割成字符串,#+表示一個或多個#字符//結果:[2,*,(,4,+,(,88,-,85,),/,2,)] } //后序排列 public void backsort() { //循環sArry for (int i = 0; i < sArry.length; i++) { //如果不是字符,就直接push入houx棧 if (!sArry[i].equals('+') && !sArry[i].equals('-') && !sArry[i].equals('*') && !sArry[i].equals('/') && !sArry[i].equals('(') && !sArry[i].equals(')')) { houx.push(sArry[i]); continue; //否則是字符,若符號棧為空,直接入棧 } else if (fuhao.isEmpty()) { fuhao.push(sArry[i]); continue; //如果為(括號,直接入符號棧 } else if (sArry[i].equals('(')) { fuhao.push(sArry[i]); continue; //如果為)括號 } else if (sArry[i].equals(')')) { /** * 不斷出棧直到(括號出現 * */ while (!fuhao.peek().equals('(')) { houx.push(fuhao.pop()); } fuhao.pop();//清掉(括號 //如果不為空,且要入的符號比符號棧頂的符號優先級高,則直接push入符號棧 } else if (!fuhao.isEmpty() && check(sArry[i], fuhao.peek())) { // fuhao.push(sArry[i]); continue; //否則,將符號棧內優先級高的符號出棧,push入houx棧,再將符號存進符號棧 } else { houx.push(fuhao.pop()); fuhao.push(sArry[i]); continue; } } //遍歷完后,直接將符號棧內的依次出棧,push入houx棧 while (!fuhao.isEmpty()) { houx.push(fuhao.pop()); }//結果是:2 4 88 86 - 2 / + * 棧內順序 } //check對比優先級 private boolean check(String a, String b) { //如果符號棧內是(括號,直接返true if (b.equals('(')) { return true; } //如果符號棧內的優先級比要入的高,返回false if ((b.equals('*') || b.equals('/')) && (a.equals('+') || a.equals('-'))) { //b>a return false; } //。。。。。。。。。。。。。的低,返回true if ((b.equals('+') || b.equals('-')) && (a.equals('*') || a.equals('/'))) { //b<a return true; } return false; } //出棧計算 public Double suan() { backsort();//后序排列 //結果棧end Stack<Double> end = new Stack<Double>(); //遍歷houx棧 for (int i = 0; i < houx.size(); i++) { //如果是加號,end pop出來兩個數字,計算后結果入棧 if (houx.get(i).equals('+')) { Double b = end.pop(); Double a = end.pop(); end.push(a + b); continue; //如果是減號,end pop出棧兩個數字,計算后結果入棧 } else if (houx.get(i).equals('-')) { Double b = end.pop(); Double a = end.pop(); end.push(a - b); continue; //如果是乘號,end pop出棧兩個數字,計算后結果入棧 } else if (houx.get(i).equals('*')) { Double b = end.pop(); Double a = end.pop(); end.push(a * b); continue; //如果是除號,end pop出棧兩個數字,計算后結果入棧 } else if (houx.get(i).equals('/')) { Double b = end.pop(); Double a = end.pop(); end.push(a / b); continue; } else if (houx.get(i).isEmpty()) { continue; } else { //不是符號,也就是數字的情況,Integer.parseInt轉int型, push入棧 end.push(Double.parseDouble(houx.get(i))); } } //輸出結果 return end.pop(); } } public class Calculator { static String str=''; public Double Eval(String str){ //結構化 Caculater cl = new Caculater(str); //計算 Double result = cl.suan(); return result; }}

關于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發現!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲三区欧美一区国产二区| 久久成人一区| 亚洲精品观看| 免费不卡在线视频| 亚洲一区二区三区在线免费| 久久亚洲风情| 中文一区一区三区免费在线观| 一区在线免费| 亚洲免费影视| 免费精品视频最新在线| 91精品精品| 四虎884aa成人精品最新| 欧美va天堂在线| 日本中文字幕不卡| 国产精品一区二区美女视频免费看| 91成人在线精品视频| 国产精品午夜av| 成人午夜在线| 久久精品动漫| 先锋影音国产一区| 日韩激情网站| 伊人网在线播放| 亚洲精品一区二区妖精| 日产欧产美韩系列久久99| 国产精品传媒麻豆hd| 精品三级国产| 国产婷婷精品| 国产精品久av福利在线观看| 激情黄产视频在线免费观看| 蜜臀久久99精品久久久久久9| 国产精品www.| 女同性一区二区三区人了人一 | 中文一区在线| 国产精品久久久久久久久久白浆| 久久婷婷一区| 国产精品地址| 综合激情视频| 99热国内精品| 精品久久美女| 老牛影视一区二区三区| 精品久久影院| 国产精品九九| 人人爱人人干婷婷丁香亚洲| 黑丝美女一区二区| 久久伊人亚洲| 日韩av网站在线免费观看| 99精品视频在线| 91精品国产经典在线观看| 亚洲电影在线一区二区三区| 久久精品国产久精国产| 一级欧美视频| 国产一区视频在线观看免费| 精品视频高潮| 国产探花一区在线观看| 午夜欧美视频| 欧美色图一区| 日韩欧美一区二区三区在线观看| 韩国女主播一区二区三区| 国产欧美日韩一级| 国产精品多人| 国产精品亚洲综合色区韩国| 亚洲综合激情在线| 亚洲日产国产精品| 免费成人在线影院| 欧美在线资源| 91国语精品自产拍| 久久麻豆精品| 婷婷综合在线| 久久av在线| 在线亚洲精品| 午夜一级在线看亚洲| 亚洲欧洲一区| 亚洲欧美日本视频在线观看| 91精品一区二区三区综合在线爱| 97精品视频在线看| 国产一区二区三区视频在线| 成人午夜亚洲| 精品国产亚洲一区二区在线观看| 国产精品宾馆| 91麻豆国产自产在线观看亚洲| 美腿丝袜在线亚洲一区| 久久这里只有| 中文在线免费视频| www.九色在线| 成人欧美一区二区三区的电影| 精品国产乱码久久久| 精品视频国内| 久久免费黄色| 亚洲欧美日韩精品一区二区| 日韩中出av| 久久精品福利| 欧美日韩一二三四| 蜜桃视频第一区免费观看| 国产精品一页| 亚洲精品小说| 另类综合日韩欧美亚洲| 日韩精品中文字幕第1页| 国产亚洲亚洲| 国产精品一区二区三区av麻| 精品视频网站| 欧美综合二区| 麻豆成人91精品二区三区| 91成人精品视频| 啪啪亚洲精品| 欧美福利在线| 久久丁香四色| 一区在线视频观看| 黄色精品视频| 蜜桃视频在线观看一区二区| 国产伊人久久| 日本中文字幕视频一区| 91精品啪在线观看国产18| 国产精品大片免费观看| 每日更新成人在线视频| 成人在线超碰| 欧美精品中文字幕亚洲专区| 在线一区免费| 国产亚洲一区二区手机在线观看| 啪啪亚洲精品| 午夜精品福利影院| 国产在线日韩| 色偷偷偷在线视频播放| 国产精品久久久免费| 亚洲日本欧美| 五月天综合网站| 91精品国产乱码久久久久久久| 国产精品mv在线观看| 日韩国产一区二| 亚洲欧洲av| 免费黄色成人| 亚洲午夜黄色| 亚洲精品小说| 五月天久久网站| 久久中文字幕二区| 中文另类视频| 欧美一区二区三区高清视频| 亚洲天堂av影院| 日韩福利一区| 激情综合网址| 亚洲精品1区| 免费成人av在线播放| 亚洲精品观看| 欧美1区2区3| 亚洲黄色免费av| 欧美激情国产在线| 日韩国产综合| 激情综合网址| 亚洲综合精品| 国产亚洲一区| 成人在线超碰| 日韩亚洲一区在线| 不卡av一区二区| 蜜桃伊人久久| 免费在线亚洲| 欧产日产国产精品视频| 狠狠爱成人网| 欧美日韩精品一区二区三区视频| 久久不见久久见免费视频7| 国产成人免费| 亚洲福利专区| 黄色成人91| 97精品资源在线观看| 精品一区二区三区四区五区| 成人羞羞在线观看网站| 亚洲专区一区| 国产一区调教| 蜜桃av一区二区| 精品视频黄色| 视频精品一区二区| 精品三级在线| 亚洲我射av| 日韩不卡在线| 国产精品一区二区av交换 | 精品国产一区二区三区噜噜噜| 亚洲午夜黄色| 久久久免费人体| 在线综合亚洲| 欧美激情另类| 欧美自拍一区| 99riav1国产精品视频| 成人免费一区| 欧美一区二区三区久久精品| 国内激情久久| 色爱综合网欧美| 国产精品一级| 日本久久一区| 爽爽淫人综合网网站| zzzwww在线看片免费| 日韩国产欧美在线视频| 亚洲综合不卡| 一区二区亚洲精品| 色爱av综合网| 国产中文在线播放| 国产精品久久国产愉拍| 97精品国产99久久久久久免费| 91久久亚洲| 在线亚洲免费| 蜜臀久久99精品久久久画质超高清| 欧美一区三区| 欧美日韩国产高清电影|