【无标题】

第一个android项目-计算器

项目概述

这个项目按照自己手机上的计算器进行了复刻,实现了计算器功能,包括加减乘除运算,小数运算,清零,删除功能。

详细设计

布局设计

说明:

这里我运用的是大的LinearLayout,里面嵌套着六个小的LinearLayout,分别存放着输入框和五行按钮

注意:

为了防止手机横放布局坍塌,我在文本输入框中的LinearLayout中仅指定了Android:layout_weight属性,并将下面的按钮布局中的高度指定为wrap_content,这表示Button的高度仍然按照Wrap_content来计算,而上面的输入框将会填满屏幕的剩余空间。

以下是我的代码

<?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-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@color/design_default_color_on_primary">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edittext_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom|right"
            android:hint="0"
            android:background="@color/black"
            android:textStyle="bold"
            android:textSize="50sp"
            android:textColor="@color/white" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button_C"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="C"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_quyu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="%"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_del"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="Del"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_chu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="÷"
            android:textColor="@color/white"
            android:textSize="40sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="7"
            android:textColor="@color/white"
            android:textSize="40sp"
        />
        <Button
            android:id="@+id/button_8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="8"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="9"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_cheng"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="×"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="4"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="5"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="6"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_jian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="-"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="2"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="3"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_jia"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="+"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_00"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="0"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_dian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="."
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_dengyu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="="
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>

</LinearLayout>
页面展示
image-20220731152137406

核心部分

原理介绍
一.逆波兰表达式

逆波兰表达式又叫做后缀表达式,是一种设有括号,并严格遵循“从左到右”运算且人看来很复杂但计算机很喜欢的后缀式表达方法(因作者手机上计算器没有括号显示,所以本文没有括号运算的)

二.逆波兰计算器

实现逆波兰计算器要分为三个步骤:

1.将表达式(字符串)转换为LIST:

遍历字符串(charAt),将其存储进LIST中,要注意的是:多位数的表达式转换成中缀式时不能将数字分开,否则含义变化

private List<String> expList(String exp) {
        List<String> list=new ArrayList<String>();
        int i=0;//指针
        char c = '0';
        String str;
        while(i<exp.length()){
            if((c=exp.charAt(i))<48||(c=exp.charAt(i))>57){//运算符
                list.add(c+"");
                i++;
            }
            else{
                str="";
                while(i<exp.length()&&(c=exp.charAt(i))>=48&&((c=exp.charAt(i))<=57)||c=='.'){
                    str+=c;
                    i++;
                }
                list.add(str);
            }
        }
        return list;
    }

2.将中缀表达式转化成后缀表达式:

1.初始化两个栈:栈s1和存储中间结果的s2

2.从左至右扫描中缀表达式

3.遇到操作数时,将其add入s2

4.遇到运算符时,比较其与s1栈顶运算符的优先级

(1)如果s1为空,或者栈顶运算符为左括号,则直接将此运算符入栈

(2)否则,若优先级比栈顶元素的运算符高,也将运算符压入s1

(3)否则,将s1栈顶的运算符弹出并add入s2中,再次转到(4-1)与s1中新的栈顶运算符比较

5.遇到括号时

(1)如果是左括号(。则直接压入s1

(2) 如果是有括号,则依次弹出s1 栈顶的运算符,并add入s2,直到遇到左括号为止,此时将这一对括号丢弃

6.重复步骤二至5,直到表达式的最右边

7.将s1中剩余的运算符依次弹出并add入s2

8.依次弹出s2

private List<String> expbList(List<String> mexp) {
        Stack<String> s1=new Stack<String>();//运算符栈s1
        List<String> s2=new ArrayList<String>();//存储中间结果的栈
        for(String i :mexp){
            if(i.matches("\\d+")||(i.matches("\\d+\\.\\d+"))){//正则表达式匹配是否为数字
                s2.add(i);
            }else {
                while(s1.size()!=0&&opera(i)<opera(s1.peek())){
                    s2.add(s1.pop());
                }
                s1.push(i);
            }
        }
        while(s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;
    }

3.后缀表达式计算:

1.new出一个stack s1

2.遍历后缀表达式的字符串

(1)如果遇到的是数字,就直接压入s1

(2)如果遇到的是运算符,那么我们直接从s1 中POP出两个数字进行计算,要注意的是,先弹出的栈顶元素在后面,次顶元素在前面,再将计算结果压入s1中

3.直到整个表达式遍历结束,s1栈中剩下的元素即为我们需要计算的结果

private double result_str(List<String> bexp) {
        Stack<String> stack = new Stack<String>();
        for(String i :bexp)
        {
            if(i.matches("\\d+")||(i.matches("\\d+\\.\\d+"))){
                stack.push(i);
            }else {
                num2=Double.parseDouble(stack.pop());
                num1=Double.parseDouble(stack.pop());
                res=0;
                if(i.equals("+"))
                    res=num1+num2;
                else if(i.equals("-"))
                    res=num1-num2;
                else if(i.equals("*"))
                    res=num1*num2;
                else if(i.equals("/"))
                    res=num1/num2;
                else {
                    throw  new RuntimeException();
                }
                stack.push(""+res);
            }
        }
        return res;
    }

整体代码

<?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-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@color/design_default_color_on_primary">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edittext_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom|right"
            android:hint="0"
            android:background="@color/black"
            android:textStyle="bold"
            android:textSize="50sp"
            android:textColor="@color/white" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button_C"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="C"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_quyu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="%"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_del"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="Del"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="40sp" />

        <Button
            android:id="@+id/button_chu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingRight="15sp"
            android:paddingBottom="15sp"
            android:text="÷"
            android:textColor="@color/white"
            android:textSize="40sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="7"
            android:textColor="@color/white"
            android:textSize="40sp"
        />
        <Button
            android:id="@+id/button_8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="8"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="9"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_cheng"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="×"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="4"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="5"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="6"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_jian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="-"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="2"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="3"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_jia"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="+"
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_00"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="0"
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_dian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F2222121"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="."
            android:textColor="@color/white"
            android:textSize="40sp"/>
        <Button
            android:id="@+id/button_dengyu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#BE0A41E1"
            android:paddingBottom="15sp"
            android:paddingRight="15sp"
            android:text="="
            android:textColor="@color/white"
            android:textSize="40sp"/>
    </LinearLayout>

</LinearLayout>
package com.example.siyu_calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_00,btn_sum,btn_sub,btn_dot,btn_del,btn_mult,btn_div,btn_clr,btn_equ;
    private TextView cal_result;
    private  boolean flag=false;
    private String exp,strnum;
    private  List<String> mexp,bexp;
    private double resnum;
    private double  num1,res;
    private double  num2;
    private String operation="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cal_result = findViewById(R.id.edittext_input);
        btn_00=findViewById(R.id.button_00);
        btn_0=findViewById(R.id.button_0);
        btn_1=findViewById(R.id.button_1);
        btn_2=findViewById(R.id.button_2);
        btn_3=findViewById(R.id.button_3);
        btn_4=findViewById(R.id.button_4);
        btn_5=findViewById(R.id.button_5);
        btn_6=findViewById(R.id.button_6);
        btn_7=findViewById(R.id.button_7);
        btn_8=findViewById(R.id.button_8);
        btn_9=findViewById(R.id.button_9);
        btn_dot=findViewById(R.id.button_dian);
        btn_sum=findViewById(R.id.button_jia);
        btn_sub=findViewById(R.id.button_jian);
        btn_mult=findViewById(R.id.button_cheng);
        btn_div=findViewById(R.id.button_chu);
        btn_clr=findViewById(R.id.button_C);
        btn_equ=findViewById(R.id.button_dengyu);
        btn_del=findViewById(R.id.button_del);

        btn_00.setOnClickListener(this);
        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_dot.setOnClickListener(this);
        btn_sum.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_mult.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_clr.setOnClickListener(this);
        btn_equ.setOnClickListener(this);
        btn_del.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button_00:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"00");
                break;
            case R.id.button_0:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"0");
                break;
            case R.id.button_1:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"1");
                break;
            case R.id.button_2:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"2");
                break;
            case R.id.button_3:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"3");
                break;
            case R.id.button_4:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"4");
                break;
            case R.id.button_5:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"5");
                break;
            case R.id.button_6:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"6");
                break;
            case R.id.button_7:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"7");
                break;
            case R.id.button_8:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"8");
                break;
            case R.id.button_9:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"9");
                break;
            case R.id.button_C:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText("0");
                break;
            case R.id.button_dian:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+".");
                break;
            case R.id.button_jia:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"+");
                break;
            case R.id.button_jian:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"-");
                break;
            case R.id.button_cheng :
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"*");
                break;
            case R.id.button_chu :
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                cal_result.setText(cal_result.getText().toString()+"/");
                break;
            case R.id.button_dengyu:
                exp=cal_result.getText().toString();
                if(exp.equals(" ")){
                    break;
                }
                cal_result.setText("");
                mexp=expList(exp);
                bexp=expbList(mexp);
                resnum=result_str(bexp);
                cal_result.setText(resnum+"");
                flag=true;
                break;
            case R.id.button_del:
                if(flag){
                    cal_result.setText("");
                    flag=false;
                }
                StringBuilder t=new StringBuilder(cal_result.getText().toString()+"");
                t.deleteCharAt(t.length()-1);
                cal_result.setText(t.toString());
                break;
        }
    }

    private double result_str(List<String> bexp) {
        Stack<String> stack = new Stack<String>();
        for(String i :bexp)
        {
            if(i.matches("\\d+")||(i.matches("\\d+\\.\\d+"))){
                stack.push(i);
            }else {
                num2=Double.parseDouble(stack.pop());
                num1=Double.parseDouble(stack.pop());
                res=0;
                if(i.equals("+"))
                    res=num1+num2;
                else if(i.equals("-"))
                    res=num1-num2;
                else if(i.equals("*"))
                    res=num1*num2;
                else if(i.equals("/"))
                    res=num1/num2;
                else {
                    throw  new RuntimeException();
                }
                stack.push(""+res);
            }
        }
        return res;
    }

    //将中缀表达式转换成后缀表达式
    private List<String> expbList(List<String> mexp) {
        Stack<String> s1=new Stack<String>();//运算符栈s1
        List<String> s2=new ArrayList<String>();//存储中间结果的栈
        for(String i :mexp){
            if(i.matches("\\d+")||(i.matches("\\d+\\.\\d+"))){//正则表达式匹配是否为数字
                s2.add(i);
            }else {
                while(s1.size()!=0&&opera(i)<opera(s1.peek())){
                    s2.add(s1.pop());
                }
                s1.push(i);
            }
        }
        while(s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;
    }

    private int opera(String oper) {
        switch(oper){
            case "+": return 1;
            case "-": return 1;
            case "*": return 2;
            case "/": return 2;
            default:
                return -1;
        }
    }

    //将表达式转换成中缀表达式,要注意的是多位数的表达式转成中缀式
    private List<String> expList(String exp) {
        List<String> list=new ArrayList<String>();
        int i=0;//指针
        char c = '0';
        String str;
        while(i<exp.length()){
            if((c=exp.charAt(i))<48||(c=exp.charAt(i))>57){//运算符
                list.add(c+"");
                i++;
            }
            else{
                str="";
                while(i<exp.length()&&(c=exp.charAt(i))>=48&&((c=exp.charAt(i))<=57)||c=='.'){
                    str+=c;
                    i++;
                }
                list.add(str);
            }
        }
        return list;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值