zoukankan      html  css  js  c++  java
  • Android------Mycalculator堆栈完整版(非实现多位操作)

    <span style="font-size:14px;">package com.edu.mju.mycalculater;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.text.DecimalFormat;
    import java.util.Stack;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private TextView tv;
        /** 格式化数据 */
        private static final DecimalFormat mFormat = new DecimalFormat(
                "########.#####");
        /** 堆栈 */
        private Stack<String> mMathStack = new Stack<String>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //绑定控件
            Button btn0 = (Button) findViewById(R.id.num0);
            Button btn1 = (Button) findViewById(R.id.num1);
            Button btn2 = (Button) findViewById(R.id.num2);
            Button btn3 = (Button) findViewById(R.id.num3);
            Button btn4 = (Button) findViewById(R.id.num4);
            Button btn5 = (Button) findViewById(R.id.num5);
            Button btn6 = (Button) findViewById(R.id.num6);
            Button btn7 = (Button) findViewById(R.id.num7);
            Button btn8 = (Button) findViewById(R.id.num8);
            Button btn9 = (Button) findViewById(R.id.num9);
            Button addbtn = (Button) findViewById(R.id.add);
            Button subbtn = (Button) findViewById(R.id.sub);
            Button divbtn = (Button) findViewById(R.id.div);
            Button mulbtn = (Button) findViewById(R.id.mul);
            Button pointbtn = (Button) findViewById(R.id.point);
            Button equalbtn = (Button) findViewById(R.id.equal);
            Button clearbtn = (Button) findViewById(R.id.clear);
            Button clear1btn = (Button) findViewById(R.id.clear1);
            tv = (TextView) findViewById(R.id.TextView);
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
            btn6.setOnClickListener(this);
            btn7.setOnClickListener(this);
            btn8.setOnClickListener(this);
            btn9.setOnClickListener(this);
            addbtn.setOnClickListener(this);
            subbtn.setOnClickListener(this);
            divbtn.setOnClickListener(this);
            mulbtn.setOnClickListener(this);
            pointbtn.setOnClickListener(this);
            equalbtn.setOnClickListener(this);
            clearbtn.setOnClickListener(this);
            clear1btn.setOnClickListener(this);
    
        }
    
        /** 操作数 入栈 */
        private void push(char obj) {
            final int size = mMathStack.size();
            // 清除
            if ('c' == obj) {
                mMathStack.clear();
                tv.setText("0");
                return;
            }
    
            // 操作符号
            if ('+' == obj || '-' == obj || '×' == obj || '÷' == obj || '=' == obj) {
                switch (size) {
                    case 0:
                        break;
                    case 2:
                        if ('=' != obj)
                            mMathStack.set(1, obj + "");// 同时输入两个操作符,后面的操作符替换前面的
                        break;
                    case 1:
                        if ('=' != obj)
                            mMathStack.push(obj + "");
                        break;
                    case 3:
                        String preResult = mFormat.format(calc());
                        mMathStack.push(preResult);
                        if ('=' != obj)
                            mMathStack.push(obj + "");
                        tv.setText(preResult);
                        break;
                }
                return;
            }
    
            String str = "";
            int location = 0;
            switch (size) {
                case 0:
                    mMathStack.push("");
                case 1:
                    str = mMathStack.peek();
                    break;
                case 2:
                    mMathStack.push("");
                case 3:
                    location = 2;
                    str = mMathStack.peek();
                    break;
            }
    
            int len = str.length();
            if ('d' == obj) {
                // 删除
                if (len > 1)
                    str = str.substring(0, len - 1);
                else if (len == 1)
                    str = "0";
    //        } else if ('f' == obj) {
    //            if ("0".equals(str) || len == 0) {
    //                return;
    //            } else if (str.charAt(0) == '-') {
    //                str = str.replace('-', ' ').trim();
    //            } else {
    //                str = '-' + str;
    //            }
            } else {
                if ('.' == obj) {
                    if (str.indexOf(".") > 0)
                        return;
                } else if ('0' == obj) {
                    if (str.length() == 0 || str.equals("0"))
                        return;
                }
                str += obj;
            }
            if ('.' != obj)
                str = mFormat.format(parseDouble(str));
            mMathStack.set(location, str);
            tv.setText(str);
        }
    
        private double calc() {
            double result = 0.0D;
            if (mMathStack.size() == 3) {
                double right = parseDouble(mMathStack.pop());
                String oper = mMathStack.pop();
                double left = parseDouble(mMathStack.pop());
                if ("+".equals(oper)) {
                    result = left + right;
                } else if ("-".equals(oper)) {
                    result = left - right;
                } else if ("×".equals(oper)) {
                    result = left * right;
                } else if ("÷".equals(oper)) {
                    if (right != 0.0D)
                        result = left / right;
                }
            }
            return result;
        }
    
        /** 解析文本数据 */
        private double parseDouble(String str) {
            try {
                return Double.parseDouble(str);
            } catch (NumberFormatException e) {
                return 0.0D;
            }
        }
    
        /** 点击事件 */
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.div:// 除
                    push('÷');
                    break;
                case R.id.mul:// 乘
                    push('×');
                    break;
                case R.id.sub:// 减
                    push('-');
                    break;
                case R.id.add:// 加
                    push('+');
                    break;
                case R.id.clear:// C
                    push('c');
                    break;
                case R.id.num0:
                    push('0');
                    break;
                case R.id.num1:
                    push('1');
                    break;
                case R.id.num2:
                    push('2');
                    break;
                case R.id.num3:
                    push('3');
                    break;
                case R.id.num4:
                    push('4');
                    break;
                case R.id.num5:
                    push('5');
                    break;
                case R.id.num6:
                    push('6');
                    break;
                case R.id.num7:
                    push('7');
                    break;
                case R.id.num8:
                    push('8');
                    break;
                case R.id.num9:
                    push('9');
                    break;
                case R.id.point:
                    push('.');
                    break;
                case R.id.equal:// =
                    push('=');
                    break;
    //            case R.id.btnPM:// 符号,正负数
    //                push('f');
    //                break;
                case R.id.clear1:// <- delete
                    push('d');
                    break;
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }</span>
    

    以上是mainActivity文件代码

    以下是layout布局文件

    <span style="font-size:14px;color:#000000;"><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="2dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp"
        tools:context=".MainActivity"
        android:orientation="vertical"
        >
    
        <TextView
            android:singleLine="true"
            android:ellipsize="start"
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:text="0"
            android:textSize="60dp"
            android:gravity="right|center_vertical"
    
            />
        <GridLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:columnCount="4">
    
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/clear"
                android:text="C"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/div"
                android:text="÷"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/mul"
                android:text="×"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/sub"
                android:text="-"
                android:textSize="30dp"
                />
            <Button
                android:onClick=""
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num7"
                android:text="7"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num8"
                android:text="8"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num9"
                android:text="9"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_rowSpan="2"
                android:id="@+id/add"
                android:text="+"
                android:textSize="30dp"
                />
    
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num4"
                android:text="4"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num5"
                android:text="5"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num6"
                android:text="6"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num1"
                android:text="1"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num2"
                android:text="2"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num3"
                android:text="3"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/clear1"
                android:text="←"
                android:textStyle="bold"
                android:textSize="30dp"
                />
    
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/num0"
                android:text="0"
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/point"
                android:text="."
                android:textSize="30dp"
                />
            <Button
                android:layout_gravity="fill"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1.2dp"
                android:layout_marginBottom="1.2dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:layout_columnSpan="2"
                android:id="@+id/equal"
                android:text="="
                android:textSize="30dp"
                />
        </GridLayout>
    
    </LinearLayout></span>
    


  • 相关阅读:
    移动端支付6位密码框
    移动端canvas刮刮乐
    原生ajax请求json数据
    canvas绘制video
    移动端阻止默认长按选中文本和弹出菜单、点击阴影
    前端移动端相关的代码(pc端与移动端自动切换)
    统一管理网站中的某些需要定期更新的时间届数 倒计时 ( 换届 之类的网站)( 兼容ie )
    Hbuilder 常用快捷键汇总
    文件找不到,路径错误问题
    图片清晰度问题
  • 原文地址:https://www.cnblogs.com/laohuihui/p/5308757.html
Copyright © 2011-2022 走看看