日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android简单的计算器

發(fā)布時(shí)間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android简单的计算器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

所使用的算法:表達(dá)式求值(中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式,后綴表達(dá)式求值值)

不如何設(shè)計(jì)接口,有時(shí)間來美化!

MainActivity.java

package com.example.calculator;import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map;import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends Activity {String mid=null;TextView textView=null;//操作符棧static LinkedList<String> opStack=new LinkedList<String>();//優(yōu)先級映射static Map<String, Integer> priority=new HashMap<String, Integer>(){{put("(", 0);put(")", 0);put("+", 1);put("-", 1);put("×", 2);put("÷",2);}};public void init(){textView=(TextView) findViewById(R.id.textView);textView.setTextSize(30);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}public void delete(View view){String text = (String) textView.getText();if(text!=null&&text.length()!=0){text=text.substring(0, text.length()-1);textView.setText(text);}}public void clear(View view){textView.setText("");}public void showText(View view){Button bt=(Button)view;String s = (String) bt.getText();//Toast.makeText(this, s, Toast.LENGTH_LONG).show();String text = (String) textView.getText();textView.setText(text+s);}public void calc(View view){mid = (String) textView.getText();String[] midSplit=goToSplit(mid);Double ans=0.;try {List<String> after = midToAfter(midSplit);ans = afterValue(after);textView.setText(ans.toString());} catch (Exception e) {Toast.makeText(this, "輸入不合法,請檢查", Toast.LENGTH_LONG).show();}}public String[] goToSplit(String s){int pre=-1;//上一個(gè)符號的位置,當(dāng)兩個(gè)符號一起時(shí):)* 應(yīng)分成:*# 否則分成:#*#StringBuffer sb=new StringBuffer();for(int i=0;i<s.length();i++){if(s.charAt(i)!='.'&&(s.charAt(i)<'0'||s.charAt(i)>'9')){if(i-1==pre){ //上一個(gè)也是操作符號sb.append(s.charAt(i)+"#"); }else sb.append("#"+s.charAt(i)+"#");pre=i;//更新pre}else{sb.append(s.charAt(i));}}String[] split = sb.toString().split("#");return split;}/*** 中綴轉(zhuǎn)后綴:* 從左到右掃描表達(dá)式* a:若是數(shù)字直接輸出* b:若是(直接入棧* c:若是)將棧中操作符依次退棧輸出,直到遇到(為止,將(出棧丟棄* d其它:將當(dāng)前操作符的優(yōu)先級小于等于棧頂操作符優(yōu)先級,則將棧頂操作出棧輸出。直到不小于或棧空為止;將當(dāng)前操作符入棧*/public static List<String> midToAfter(String [] mid) throws Exception{LinkedList<String> after=new LinkedList<String>();int index=0;for(String ss:mid){if(ss.equals("=")) continue;if(priority.get(ss)==null){//說明是操作數(shù)after.add(ss);}else if(ss.equals("(")){opStack.push(ss);}else if(ss.equals(")")){while(!opStack.peek().equals("(")){//不是“(”,則輸出,after.add(opStack.pop());}opStack.pop();//去除(}else {while(!opStack.isEmpty()&&priority.get(ss)<=priority.get(opStack.peek())){after.add(opStack.pop());}opStack.push(ss);}}while(!opStack.isEmpty()) after.add(opStack.pop());return after;}/*** 后綴求值:從左到右掃描后綴表達(dá)式* a:若為數(shù)字,直接入棧* b:若為操作符,從棧中出棧兩個(gè)數(shù)字,按操作符計(jì)算,再把結(jié)果入棧,注意兩個(gè)操作數(shù)運(yùn)算順序* 結(jié)果:最后棧中僅僅有一個(gè)數(shù)字,出棧即為答案* @param after* @return*/public static double afterValue(List<String> after) throws Exception{LinkedList<Double> number=new LinkedList<Double>();for(String ss:after){if(priority.get(ss)!=null){//是操作符,取出兩個(gè)數(shù)。按操作符計(jì)算后入數(shù)字棧Double y=number.pop();Double x=number.pop();if(ss.equals("+")) number.push(x+y);else if(ss.equals("-")) number.push(x-y);else if(ss.equals("×")) number.push(x*y);else if(ss.equals("÷")) number.push(x/y);}else{number.push(Double.valueOf(ss));}}return number.pop();}}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="100dp"android:hint="0"/><TableLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TableRow android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="60dp"android:layout_height="60dp"android:text="("android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text=")"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="C"android:onClick="clear"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="DEL"android:onClick="delete"/> </TableRow><TableRow android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="60dp"android:layout_height="60dp"android:text="7"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="8"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="9"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="÷"android:onClick="showText"/> </TableRow><TableRow android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="60dp"android:layout_height="60dp"android:text="4"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="5"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="6"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="×"android:onClick="showText"/> </TableRow><TableRow android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="60dp"android:layout_height="60dp"android:text="1"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="2"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="3"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="-"android:onClick="showText"/> </TableRow><TableRow android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="60dp"android:layout_height="60dp"android:text="."android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="0"android:onClick="showText"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="="android:onClick="calc"/><Button android:layout_width="60dp"android:layout_height="60dp"android:text="+"android:onClick="showText"/> </TableRow></TableLayout></LinearLayout>

效果圖:




版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。

轉(zhuǎn)載于:https://www.cnblogs.com/mengfanrong/p/4655434.html

總結(jié)

以上是生活随笔為你收集整理的android简单的计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。