首页 > 代码库 > 安卓利用栈和算术表达式优先级实现进阶版计算器

安卓利用栈和算术表达式优先级实现进阶版计算器

先po上具体的源代码

简单的xml文件

<?xml version="1.0" encoding="utf-8"?><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="com.example.admin.compute.MainActivity"    android:orientation="vertical">    <TextView        android:id="@+id/txt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="50sp"        android:gravity="center_vertical|end"        android:layout_marginTop="20dp"        android:singleLine="true"        android:ellipsize="start"        />    <TableLayout        android:id="@+id/tablelayout01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:shrinkColumns="1"        android:stretchColumns="2"        >    <TableRow        android:layout_marginTop="8dp">    <Button        android:id="@+id/add"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="+"        android:textSize="30dip"        android:layout_weight="1"/>        <Button            android:id="@+id/sub"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="-"            android:textSize="30dip"            android:layout_weight="1"/>        <Button            android:id="@+id/multiply"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="X"            android:textSize="30dip"            android:layout_weight="1"/>        <Button            android:id="@+id/divide"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="/"            android:textSize="30dip"            android:layout_weight="1"/>    </TableRow>        <TableRow            android:layout_marginTop="8dp">            <Button                android:id="@+id/zero"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="0"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/one"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="1"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/two"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="2"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/three"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="3"                android:textSize="30dip"                android:layout_weight="1"/>        </TableRow>        <TableRow            android:layout_marginTop="8dp">            <Button                android:id="@+id/four"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="4"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/five"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="5"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/six"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="6"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/seven"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="7"                android:textSize="30dip"                android:layout_weight="1"/>        </TableRow>        <TableRow            android:layout_marginTop="8dp">            <Button                android:id="@+id/eight"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="8"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/nine"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="9"                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/point"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="."                android:textSize="30dip"                android:layout_weight="1"/>            <Button                android:id="@+id/backspace"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="《—"                android:textSize="30dip"                android:layout_weight="1"/>        </TableRow>        <TableRow>            <Button                android:id="@+id/result"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:text="="                android:textSize="30dip"                android:layout_weight="1"/>            </TableRow>    </TableLayout></LinearLayout>

接着是具体的java实现

package com.example.admin.compute;import java.util.Stack;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {    private Button add,sub,multiply,divide,zero,one,two,three,four,five,six,seven,eight,nine,point,result,backspace;    private TextView txt;    private String operation="";    int q=0;    String a[];    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        setupViewComponent();    }    private void setupViewComponent() {        add= (Button) findViewById(R.id.add);        sub= (Button)findViewById(R.id.sub);        multiply= (Button)findViewById(R.id.multiply);        divide= (Button)findViewById(R.id.divide);        zero= (Button)findViewById(R.id.zero);        one= (Button)findViewById(R.id.one);        two= (Button)findViewById(R.id.two);        three= (Button)findViewById(R.id.three);        four= (Button)findViewById(R.id.four);        five= (Button)findViewById(R.id.five);        six= (Button)findViewById(R.id.six);        seven= (Button)findViewById(R.id.seven);        eight= (Button)findViewById(R.id.eight);        nine= (Button)findViewById(R.id.nine);        point= (Button)findViewById(R.id.point);        result= (Button)findViewById(R.id.result);        backspace=(Button)findViewById(R.id.backspace) ;        txt= (TextView) findViewById(R.id.txt);        add.setOnClickListener(addOnClick);        sub.setOnClickListener(subOnClick);        multiply.setOnClickListener(multiplyOnClick);        divide.setOnClickListener(divideOnClick);        one.setOnClickListener(oneOnClick);        two.setOnClickListener(twoOnClick);        three.setOnClickListener(threeOnClick);        four.setOnClickListener(fourOnClick);        five.setOnClickListener(fiveOnClick);        six.setOnClickListener(sixOnClick);        seven.setOnClickListener(sevenOnClick);        eight.setOnClickListener(eightOnClick);        nine.setOnClickListener(nineOnClick);        zero.setOnClickListener(zeroOnClick);        point.setOnClickListener(pointOnClick);        result.setOnClickListener(resultOnClick);        backspace.setOnClickListener(backspaceOnClick);    }    private Button.OnClickListener backspaceOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            if(operation.length()>1)            {operation=operation.substring(0,operation.length()-1);            txt.setText(operation);}            else{                operation="0";                txt.setText(operation);            }        }    };    private Button.OnClickListener addOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="+";            txt.setText(operation);        }    };    private Button.OnClickListener subOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="-";            txt.setText(operation);        }    };    private Button.OnClickListener multiplyOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="*";            txt.setText(operation);        }    };    private Button.OnClickListener divideOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="/";            txt.setText(operation);        }    };    private Button.OnClickListener zeroOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="0";            txt.setText(operation);        }    };    private Button.OnClickListener oneOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="1";            txt.setText(operation);        }    };    private Button.OnClickListener twoOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="2";            txt.setText(operation);        }    };    private Button.OnClickListener threeOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="3";            txt.setText(operation);        }    };    private Button.OnClickListener fourOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="4";            txt.setText(operation);        }    };    private Button.OnClickListener fiveOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="5";            txt.setText(operation);        }    };    private Button.OnClickListener sixOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="6";            txt.setText(operation);        }    };    private Button.OnClickListener sevenOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="7";            txt.setText(operation);        }    };    private Button.OnClickListener eightOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="8";            txt.setText(operation);        }    };    private Button.OnClickListener nineOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="9";            txt.setText(operation);        }    };    private Button.OnClickListener pointOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+=".";            txt.setText(operation);        }    };    private Button.OnClickListener resultOnClick=new Button.OnClickListener(){        @Override        public void onClick(View v) {            operation+="#";            if (operation.charAt(0)==‘-‘){                operation="0"+operation;            }            java.util.Stack<String> OPTR=new java.util.Stack<String>();            java.util.Stack<String> OPND=new java.util.Stack<String>();            OPTR.push("#");            String buf="";            String theta,a,b;            char buffer;            int i=1;            while(operation.charAt(i-1)!=‘#‘||OPTR.peek()!="#"){                if (operation.charAt(i-1)==‘+‘||operation.charAt(i-1)==‘-‘||operation.charAt(i-1)==‘*‘||operation.charAt(i-1)==‘/‘||operation.charAt(i-1)==‘#‘){                    if (buf!="")                    {OPND.push(buf);}                    buffer=(OPTR.peek()).charAt(0);                    if(Precede(buffer,operation.charAt(i-1))==‘>‘)                    {                        theta=OPTR.pop();                        b=OPND.pop();                        a=OPND.pop();                        OPND.push(Operate(a,theta,b));                    }                   else {                       OPTR.push(String.valueOf(operation.charAt(i - 1)));                        i=i+1;                    }                    buf="";                }                else{                    buf+=operation.charAt(i-1);                    i=i+1;                }            }                        operation=OPND.peek();            txt.setText(operation);        }        private String Operate(String a, String theta, String b) {            char cao=theta.charAt(0);            if(cao==‘+‘)                return String.valueOf(Float.parseFloat(a)+Float.parseFloat(b));            if(cao==‘-‘)                return String.valueOf(Float.parseFloat(a)-Float.parseFloat(b));            if(cao==‘*‘)                return String.valueOf(Float.parseFloat(a)*Float.parseFloat(b));            else                return String.valueOf(Float.parseFloat(a)/Float.parseFloat(b));        }        private char Precede(char peek, char c) {            if (peek==‘+‘||peek==‘-‘){                if(c==‘+‘||c==‘-‘||c==‘#‘)                    return ‘>‘;            }            if (peek==‘*‘||peek==‘/‘){                return ‘>‘;            }            if (c==‘#‘){                return ‘>‘;            }            else                return ‘<‘;        }    };}

具体实现方式和原理参考《数据结构》中对于栈的简单应用,博主也是为了考研,再加上安卓老师要求制作计算器,便顺带这着小小复习了一下栈;

不得不说高级语言就是蛮好用的,栈可以直接使用,方法也都是现成的,方便。

具体实现过程就是按下每个运算符或者数字按钮,就将字符串operation后边接上新的字符,对于整体的处理则放在等号的监听器中;其他的大体架构切记要和书上一致啊,这点博主算是吃了大亏,以为耍了个小聪明便用了别的处理方式,结果最后又推到了全重做了,好气啊。。。。

这里还有编程中遇到的几个问题没有研究透,比如说字符char和String之间的转换,总感觉会产生乱七八糟的丢失。还有这里边的switch,TM的总是整不出正确的结果,真是奇怪我日。。。。

就这样。。

安卓利用栈和算术表达式优先级实现进阶版计算器