《Android 计算器》

1,res/layout/activity_calculator.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="2">

    <TextView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/et_input"
    android:textSize="40sp"/>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4">

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="C"
    android:textSize="40sp"
    android:id="@+id/btn_clear"
    android:layout_weight="1"
    android:background="#ff0000"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="/"
    android:textSize="40sp"
    android:id="@+id/btn_divide"
    android:layout_weight="1"
    android:background="#00ffff"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="X"
    android:textSize="40sp"
    android:id="@+id/btn_multiply"
    android:layout_weight="1"
    android:background="#00ffff"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="T"
    android:textSize="40sp"
    android:id="@+id/btn_del"
    android:layout_weight="1"
    android:background="#ff8c00"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:weightSum="4">

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="7"
    android:textSize="30sp"
    android:id="@+id/btn_7"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="8"
    android:textSize="30sp"
    android:id="@+id/btn_8"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="9"
    android:textSize="30sp"
    android:id="@+id/btn_9"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="-"
    android:textSize="50sp"
    android:id="@+id/btn_minus"
    android:layout_weight="1"
    android:background="#00ffff"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="4"
    android:textSize="30sp"
    android:id="@+id/btn_4"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="5"
    android:textSize="30sp"
    android:id="@+id/btn_5"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="6"
    android:textSize="30sp"
    android:id="@+id/btn_6"
    android:layout_weight="1"
    android:background="#99CCFF"/>

        7b68ee<Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="+"
    android:textSize="40sp"
    android:id="@+id/btn_plus"
    android:layout_weight="1"
    android:background="#00ffff"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="1"
    android:textSize="30sp"
    android:id="@+id/btn_1"
    android:layout_weight="1"
    android:background="#99CCFF"/>

        <Button
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#99CCFF"
            android:text="2"
            android:textSize="30sp" />

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="3"
    android:textSize="30sp"
    android:id="@+id/btn_3"
    android:layout_weight="1"
    android:background="#99CCFF"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="."
    android:textSize="40sp"
    android:id="@+id/btn_point"
    android:layout_weight="1"
    android:background="#7fffd4"/>
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="4">

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="("
    android:textSize="30sp"
    android:id="@+id/btn_left"
    android:layout_weight="1"
    android:background="#7fffd4"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="0"
    android:textSize="30sp"
    android:id="@+id/btn_0"
    android:background="#99CCFF"
    android:layout_weight="1" />

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text=")"
    android:textSize="30sp"
    android:id="@+id/btn_right"
    android:layout_weight="1"
    android:background="#7fffd4"/>

    <Button android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="="
    android:textSize="30sp"
    android:id="@+id/btn_equal"
    android:background="#1e90ff"
    android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

2,java/calculator 代码

package com.example.a1.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.a1.calculator.R;

import java.util.Arrays;

public class calculator extends AppCompatActivity implements View.OnClickListener {
    Button btn_0;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_point; //小数点
    Button btn_clear; //清除
    Button btn_del;   //删除
    Button btn_plus;
    Button btn_minus;
    Button btn_multiply;
    Button btn_divide;
    Button btn_equal;
    Button btn_left;
    Button btn_right;

    private TextView et_input;
    private StringBuilder pending = new StringBuilder();
    private void initView() {
    btn_0 = (Button) findViewById(R.id.btn_0);
    btn_1 = (Button) findViewById(R.id.btn_1);
    btn_2 = (Button) findViewById(R.id.btn_2);
    btn_3 = (Button) findViewById(R.id.btn_3);
    btn_4 = (Button) findViewById(R.id.btn_4);
    btn_5 = (Button) findViewById(R.id.btn_5);
    btn_6 = (Button) findViewById(R.id.btn_6);
    btn_7 = (Button) findViewById(R.id.btn_7);
    btn_8 = (Button) findViewById(R.id.btn_8);
    btn_9 = (Button) findViewById(R.id.btn_9);
    btn_point = (Button) findViewById(R.id.btn_point);
    btn_clear = (Button) findViewById(R.id.btn_clear);
    btn_del = (Button) findViewById(R.id.btn_del);
    btn_plus = (Button) findViewById(R.id.btn_plus);
    btn_minus = (Button) findViewById(R.id.btn_minus);
    btn_multiply = (Button) findViewById(R.id.btn_multiply);
    btn_divide = (Button) findViewById(R.id.btn_divide);
    btn_equal = (Button) findViewById(R.id.btn_equal);
    et_input = (TextView) findViewById(R.id.et_input);
    btn_left = (Button) findViewById(R.id.btn_left);
    btn_right = (Button) findViewById(R.id.btn_right);
    btn_0.setOnClickListener(this);
    btn_1.setOnClickListener(this);
    btn_2.setOnClickListener(this);
    btn_3.setOnClickListener(this);
    btn_4.setOnClickListener(this);
    btn_5.setOnClickListener(this);
    btn_6.setOnClickListener(this);
    btn_7.setOnClickListener(this);
    btn_8.setOnClickListener(this);
    btn_9.setOnClickListener(this);
    btn_point.setOnClickListener(this);
    btn_plus.setOnClickListener(this);
    btn_equal.setOnClickListener(this);
    btn_minus.setOnClickListener(this);
    btn_multiply.setOnClickListener(this);
    btn_del.setOnClickListener(this);
    btn_divide.setOnClickListener(this);
    btn_clear.setOnClickListener(this);
    btn_divide.setOnClickListener(this);
    btn_left.setOnClickListener(this);
    btn_right.setOnClickListener(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        initView();
    }

    public void onClick(View v) {
        int last = 0;
        if(pending.length()!=0) {
            last = pending.codePointAt(pending.length()-1);
        } switch (v.getId()) {
            case R.id.btn_0: pending = pending.append("0");
            et_input.setText(pending);
            break;
            case R.id.btn_1: pending = pending.append("1");
            et_input.setText(pending);
            break;
            case R.id.btn_2: pending = pending.append("2");
            et_input.setText(pending);
            break;
            case R.id.btn_3: pending = pending.append("3");
            et_input.setText(pending);
            break;
            case R.id.btn_4: pending = pending.append("4");
            et_input.setText(pending);
            break;
            case R.id.btn_5: pending = pending.append("5");
            et_input.setText(pending);
            break;
            case R.id.btn_6: pending = pending.append("6");
            et_input.setText(pending);
            break;
            case R.id.btn_7: pending = pending.append("7");
            et_input.setText(pending);
            break;
            case R.id.btn_8: pending = pending.append("8");
            et_input.setText(pending);
            break;
            case R.id.btn_9: pending = pending.append("9");
            et_input.setText(pending);
            break;
            case R.id.btn_plus: if (last >= '0' && last <= '9' ) {
            pending = pending.append("+");
            }
            et_input.setText(pending);
            break;
            case R.id.btn_minus: if (last >= '0' && last <= '9') {
                pending = pending.append("-");
            }
            et_input.setText(pending);
            break;
            case R.id.btn_multiply: if (last >= '0' && last <= '9' ) {
                    pending = pending.append("*");
                }
                et_input.setText(pending);
                break;
                case R.id.btn_divide: if (last >= '0' && last <= '9' ) {
                        pending = pending.append("/");
                    }
                    et_input.setText(pending);
                    break;
                    case R.id.btn_point: if (judje1()) {
                        pending = pending.append(".");
                        et_input.setText(pending);
                    }
                        break;
                        case R.id.btn_right:// )右括号
                        if((last>='0' &&last<='9'||last==')')&&judje2()==1) {
                            pending = pending.append(")");
                            et_input.setText(pending);
                        }
                        break;
                        case R.id.btn_left:// (左括号
                            if((last!='(')||(last<='0' &&last>='9')){
                                pending = pending.append("(");
                                et_input.setText(pending);
                            }
                            break;
                            case R.id.btn_del: //删除
                                if (pending.length() != 0) {
                                    pending = pending.delete(pending.length() - 1, pending.length());
                                    et_input.setText(pending);
                                }
                                break;
                                case R.id.btn_clear: //清空
                                    pending = pending.delete(0, pending.length());
                                    et_input.setText(pending);
                                    break;
                                    case R.id.btn_equal: // =等于
                                        if ((pending.length() > 1)) {
                                            InfixInToDuffix inf = new InfixInToDuffix();
                                        String jieguo;
                                        try { String a = inf.toSuffix(pending);
                                            jieguo = inf.dealEquation(a);
                                        } catch (Exception ex) {
                                            jieguo = "出错";
                                        }
                                        et_input.setText(pending + "=" + jieguo);
                                        pending = pending.delete(0, pending.length());
                                        if (Character.isDigit(jieguo.charAt(0))) {
                                            pending = pending.append(jieguo);
                                            }
                                        }
                                        break;
                                        default: break;
        }
    }
    private boolean judje1() { String a = "+-*/.";
    int[] b = new int[a.length()];
    int max;
    for (int i = 0; i < a.length(); i++) {
        String c = "" + a.charAt(i);
        b[i] = pending.lastIndexOf(c);
    }
    Arrays.sort(b);
    if (b[a.length() - 1] == -1) {
        max = 0;
    } else {
        max = b[a.length() - 1];
    } if (pending.indexOf(".", max) == -1) {
        return true;
    } else {
        return false;
    }
    } private int judje2(){
        int a=0,b=0;
        for(int i = 0 ; i < pending.length() ;i++){
            if(pending.charAt(i)=='(' ) {
                a++;
            } if(pending.charAt(i)==')' ) {
                b++;
            }
        }
        if(a == b)
            return 0;
        if(a > b)
            return 1;
        return 2;
    }
}

3,java/InfixInToDuffix (使用集合定义好符号的运算优先级别)

package com.example.a1.calculator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.*;
import java.util.ArrayList;
import java.util.*;

public class InfixInToDuffix { //使用集合定义好符号的运算优先级别
    private static final Map<Character,Integer>basic =new HashMap<Character, Integer>();
    static {
        basic.put('-',1);
        basic.put('+', 1);
        basic.put('*', 2);
        basic.put('/', 2);
        basic.put('(', 0);//在运算中  ()的优先级最高,但是此处因程序中需要 故设置为0
        } //将中缀表达式转换为后缀表达式
    public String toSuffix(StringBuilder infix){
        List<String> queue = new ArrayList<String>(); //定义队列  用于存储 数字  以及最后的  后缀表达式
        List<Character> stack = new ArrayList<Character>(); //定义栈    用于存储  运算符  最后stack中会被 弹空
        char[] charArr = infix.substring(0,infix.length()).trim().toCharArray(); //字符数组  用于拆分数字或符号
        String standard = "*/+-()"; //判定标准 将表达式中会出现的运算符写出来
        char ch = '&'; //在循环中用来保存 字符数组的当前循环变量的  这里仅仅是初始化一个值  没有意义
        int len = 0; //用于记录字符长度 【例如100*2,则记录的len为3 到时候截取字符串的前三位就是数字】
        for (int i = 0; i < charArr.length; i++) { //开始迭代
            ch = charArr[i]; //保存当前迭代变量
            if(Character.isDigit(ch)) { //如果当前变量为 数字
                len++;
            }else if(ch == '.'){ //如果当前变量为  .  会出现在小数里面
                len++;
            }else if(standard.indexOf(ch) != -1) { //如果是上面标准中的 任意一个符号
                if(len > 0) { //长度也有
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i))); //说明符号之前的可以截取下来做数字
                    len = 0; //长度置空
                    }
                    if(ch == '(') { //如果是左括号
                    stack.add(ch); //将左括号 放入栈中
                    continue; //跳出本次循环  继续找下一个位置
                    }
                    if (!stack.isEmpty()) { //如果栈不为empty
                        int size = stack.size() - 1; //获取栈的大小-1  即代表栈最后一个元素的下标
                    boolean flag = false; //设置标志位
                    while (size >= 0 && ch == ')' && stack.get(size) != '(') { //若当前ch为右括号,则 栈里元素从栈顶一直弹出,直到弹出到 左括号
                        queue.add(String.valueOf(stack.remove(size))); //注意此处条件:ch并未入栈,所以并未插入队列中;同样直到找到左括号的时候,循环结束了,所以左括号也不会放入队列中【也就是:后缀表达式中不会出现括号】
                        size--; //size-- 保证下标永远在栈最后一个元素【栈中概念:指针永远指在栈顶元素】
                        flag = true; //设置标志位为true  表明一直在取()中的元素
                        }
                        if(ch==')'&&stack.get(size) == '('){
                        flag = true;
                        }
                        while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch)) { //若取得不是()内的元素,并且当前栈顶元素的优先级>=对比元素 那就出栈插入队列
                        queue.add(String.valueOf(stack.remove(size))); //同样  此处也是remove()方法,既能得到要获取的元素,也能将栈中元素移除掉
                            size--;
                        }
                }
                if(ch != ')') { //若当前元素不是右括号
                    stack.add(ch); //就要保证这个符号 入栈
                    } else { //否则就要出栈 栈内符号
                    stack.remove(stack.size() - 1);
                }
            } if(i == charArr.length - 1) { //如果已经走到了  中缀表达式的最后一位
                if(len > 0) { //如果len>0  就截取数字
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
                }
                int size = stack.size() - 1; //size表示栈内最后一个元素下标
                while (size >= 0) { //一直将栈内  符号全部出栈 并且加入队列中  【最终的后缀表达式是存放在队列中的,而栈内最后会被弹空】
                    queue.add(String.valueOf(stack.remove(size)));
                    size--;
                }
            }
        }
        String a = queue.toString();
        return a.substring(1,a.length()-1);
    }
    public String dealEquation(String equation){
        String [] arr = equation.split(", "); //根据, 拆分字符串
        List<String> list = new ArrayList<String>(); //用于计算时  存储运算过程的集合【例如list中当前放置  100   20  5  /  则取出20/5 最终将结果4存入list   此时list中结果为  100  4 】
        for (int i = 0; i < arr.length; i++) { //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数  都是size-2
            int size = list.size();
            switch (arr[i]) { case "+": double a = Double.parseDouble(list.remove(size-2))+ Double.parseDouble(list.remove(size-2));
            list.add(String.valueOf(a));
            break;
            case "-": double b = Double.parseDouble(list.remove(size-2))- Double.parseDouble(list.remove(size-2));
            list.add(String.valueOf(b));
            break;
            case "*": double c = Double.parseDouble(list.remove(size-2))* Double.parseDouble(list.remove(size-2));
            list.add(String.valueOf(c));
            break;
            case "/": double d = Double.parseDouble(list.remove(size-2))/ Double.parseDouble(list.remove(size-2));
            list.add(String.valueOf(d));
            break;
            default: list.add(arr[i]);
            break; //如果是数字  直接放进list中
                }
        }
        return list.size() == 1 ? list.get(0) : "格式错误运算失败" ; //最终list中仅有一个结果,否则就是算错了
    }
}

4,manifests/AndroidManifest.xml 设置APP图片及名字

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a1.calculator">

    <application
        android:allowBackup="true"
        android:icon="@drawable/gtr"   //图片(图片要放在 res/drawable/目录下)
        android:label="ZY计算器"   //名字
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".calculator">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值