电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))

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

本文實(shí)例為大家分享了Android實(shí)現(xiàn)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

布局代碼:

<?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> <!--計(jì)算表達(dá)式輸入框--> <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; //除號(hào) 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); }}

計(jì)算表達(dá)式代碼:

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

關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來(lái)發(fā)現(xiàn)!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 电镀整流器_微弧氧化电源_高频电解电源_微弧氧化设备厂家_深圳开瑞节能 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 河南不锈钢水箱_地埋水箱_镀锌板水箱_消防水箱厂家-河南联固供水设备有限公司 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 深圳成考网-深圳成人高考报名网 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 温州在线网| 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 广州各区危化证办理_危险化学品经营许可证代办 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 亿诺千企网-企业核心产品贸易| 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 南方珠江-南方一线电缆-南方珠江科技电缆-南方珠江科技有限公司 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 金现代信息产业股份有限公司--数字化解决方案供应商 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 广东成考网-广东成人高考网| 锤式粉碎机,医药粉碎机,锥式粉碎机-无锡市迪麦森机械制造有限公司 | 南京办公用品网-办公文具用品批发-打印机耗材采购 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 空气能采暖,热泵烘干机,空气源热水机组|设备|厂家,东莞高温热泵_正旭新能源 | 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 深圳南财多媒体有限公司介绍| 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 |