Android实现计算器(计算表达式/计算小数点以及括号)

本文实例为大家分享了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:id="@+id/content_box"
 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:id="@+id/btn_c"
 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:id="@+id/btn_ce"
 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:id="@+id/btn_aliquot"
 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:id="@+id/btn_divide"
 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:id="@+id/btn_leftBracket"
 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:id="@+id/btn_rightBracket"
 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:id="@+id/btn_seven"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="7"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_eight"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="8"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_nine"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="9"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_multiply"
 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:id="@+id/btn_four"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="4"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_five"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="5"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_six"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="6"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_subtract"
 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:id="@+id/btn_one"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="1"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_two"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="2"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_three"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="2"
 android:layout_weight="1"
 android:text="3"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_add"
 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:id="@+id/btn_point"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="0"
 android:layout_weight="1"
 android:text="."
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_zero"
 android:layout_width="0dp"
 android:layout_height="70dp"
 android:layout_column="1"
 android:layout_weight="1"
 android:text="0"
 android:textSize="12pt" />

 <Button
 android:id="@+id/btn_equal"
 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;
 }
}

关于计算器的精彩文章请查看《计算器专题》 ,更多精彩等你来发现!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android计算器简单实现代码

    本文实例为大家分享了android计算器的具体实现代码,供大家参考,具体内容如下 java代码: package com.itheima74.simplecalculator4; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; imp

  • android studio实现简单的计算器功能

    本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下 先来个效果图: 功能: 满足加减乘除四则运算规则,有回退.清除功能. 下面的代码只是完成基本功能,若添加背景图先看看下面的方法:Android Studio App设置背景图片 1.本地准备好图片,复制它,粘贴进mipmap(drawable)文件夹. 2.在activity_main.xml里添加下面代码,注意添加位置. 3.完成,效果图: content_main.xml文件(页面布局,conte

  • android简易计算器的制作

    之前有好好完成老师留过的C++大作业,使用MFC制作通讯录.所以用AS写一个安卓的计算器并不是很难,但还是想上手操作一下,写一个只有简单加减乘除运算的小计算器,后面可能会考虑加一些其他的稍微复杂的计算功能.下面是步骤. 1.首先创建一个empty activity,取名为MyStudyCalculator. 2.打开activity_main.xml文件,创建两个编辑框(EditText).四个按钮(Button).一个文本框(TextView),并设置相应的id.其中编辑框作用是让用户填入两个

  • Android计算器编写代码

    其实这个安卓计算机,所有的后台思想与<C#计算器编写代码>是一模一样的.Win窗体程序移植到安卓,从C#到Java其实很简单的,因为两者的基本语法都很相像,唯一的难点是安卓的xml布局部分,不像C#窗体能够直接拖.  还是如下图一个能够完成基本四则运算的计算器: 先在res\values\strings.xml设置按钮相应的字体,以免布局文件警告满天飞: <?xml version="1.0" encoding="utf-8"?> <r

  • Android开发学习实现简单计算器

    这里是用线性布局实现的计算器,为防止以后再回顾知识代码找不到,特将代码贴在这里: xml文件的布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-

  • android studio实现简单的计算器(无bug)

    本文实例为大家分享了android studio实现简单计算器的具体代码,供大家参考,具体内容如下 1.效果图 2布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent&qu

  • 从零开始学android实现计算器功能示例分享(计算器源码)

    下面是效果展示: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match

  • android计时器,时间计算器的实现方法

    需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34.点击停止的时候停止计时.问题:使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效.在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果.尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符

  • Android入门计算器编写代码

    这个简易计算器是我按照一本android开发入门书学的,书上的第一个例子就是计算器的编写.计算器的编写主要涉及到按键的布局和按键输入要点. 一个总的Lnearlayout的布局下orientation设置为vertical垂直分布,然后此布局下再设置1给我Edittext的一个文本框4个Lnearlayout子布局(第4个布局里可以嵌套另外3个Lnearlayout的布局来实现按钮排版)这4个子布局在你的界面上肯定是垂直分布的,因为你的总布局设置vertical.第一个子布局放置4个Button

  • Android开发实现的简单计算器功能【附完整demo源码下载】

    本文实例讲述了Android开发实现的简单计算器功能.分享给大家供大家参考,具体如下: 这个Android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦: 先上图看效果 比较简单,所以我就没怎么写注释,应该一看就能明白的 有不明白的可以发信问我 先贴MainActivity.java代码 package com.example.calculator; import android.app.Activity; import android.os.Bundle; import andro

随机推荐