wiki:wade/android/example_1

輸入:1 + 2 * 3 - 4 / 2 + 18

source code

/*
 * 這個計算機會將輸入四則運算轉為 postfix,僅支援整數。
 * history:
 *   2011-06-21 初版
 */

package www.google.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Android_helloActivity extends Activity implements OnClickListener
{
    private EditText w_editText;                    // 存放 postfix 及目前輸入數值
    private EditText w_editText2;                   // 顯示結果                
    private String w_editTextString = "";           // w_editText 內的字串
    private String[] postfixStr = new String[100];  // postfix 運算式
    private int postfixPointer = 0;                 
    private String[] opStack = new String [100];    // 運算子的 stack
    private int opStackPointer = 0;
    private String op = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        w_editText = (EditText) findViewById(R.id.editText1);
        w_editText2 = (EditText) findViewById(R.id.editText2);
        
        // 將 button 事件加入
        ((Button) findViewById(R.id.button0)).setOnClickListener(this); // 0
        ((Button) findViewById(R.id.button1)).setOnClickListener(this); // 1
        ((Button) findViewById(R.id.button2)).setOnClickListener(this); // 2
        ((Button) findViewById(R.id.button3)).setOnClickListener(this); // 3
        ((Button) findViewById(R.id.button4)).setOnClickListener(this); // 4
        ((Button) findViewById(R.id.button5)).setOnClickListener(this); // 5
        ((Button) findViewById(R.id.button6)).setOnClickListener(this); // 6
        ((Button) findViewById(R.id.button7)).setOnClickListener(this); // 7
        ((Button) findViewById(R.id.button8)).setOnClickListener(this); // 8
        ((Button) findViewById(R.id.button9)).setOnClickListener(this); // 9
        ((Button) findViewById(R.id.button10)).setOnClickListener(this); // +
        ((Button) findViewById(R.id.button11)).setOnClickListener(this); // -
        ((Button) findViewById(R.id.button12)).setOnClickListener(this); // ×
        ((Button) findViewById(R.id.button13)).setOnClickListener(this); // ÷
        ((Button) findViewById(R.id.button14)).setOnClickListener(this); // =
        ((Button) findViewById(R.id.button15)).setOnClickListener(this); // AC
    }

    private boolean isOp(String str)
    {
        if (str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))
            return true;
        return false;
    }
    
    
    /** 計算結果 */
    private int compute(String[] seqStr, int size) 
    {
        int i;
        int numP = 0;
        int[] num = new int[10];
        
        for (i = 0; i < size; i++)
        {
            if (isOp(seqStr[i]))
            {
                numP--;
                if (seqStr[i].equals("+"))
                    num[numP - 1] = num[numP - 1] + num[numP];
                else if (seqStr[i].equals("-"))
                    num[numP - 1] = num[numP - 1] - num[numP];
                else if (seqStr[i].equals("*"))
                    num[numP - 1] = num[numP - 1] * num[numP];
                else if (seqStr[i].equals("/"))
                    num[numP - 1] = num[numP - 1] / num[numP];
                else
                    return -9999;  // error
            }
            else
                num[numP++] =Integer.parseInt(seqStr[i]);
        }
        return num[0];
    }

    /** click 事件  */
    @Override
    public void onClick(View view)
    {
        int i;
        switch (view.getId())
        {
            // 按鍵 0~9 事件
            case R.id.button0:
            case R.id.button1:
            case R.id.button2:
            case R.id.button3:
            case R.id.button4:
            case R.id.button5:
            case R.id.button6:
            case R.id.button7:
            case R.id.button8:
            case R.id.button9:
                // 將 android:text 的值取出
                String inDigit = ((Button) view).getText().toString();
                if (w_editTextString.equals("0")) {
                    w_editTextString = inDigit;
                } else {
                    w_editTextString += inDigit;
                }
                w_editText.setText(w_editTextString);
                break;

            case R.id.button10:     // 按鍵 +
                // 存入數值
                postfixStr[postfixPointer++] = w_editTextString;
                w_editTextString = "";
                // 將 op stack 全取出
                if (!op.equals(""))
                {
                    for (i = --opStackPointer; i >= 0; i--)
                    {
                        postfixStr[postfixPointer++] = opStack[i];
                    }
                    opStackPointer = 0;
                }
                // 將新的 op 放入 stack
                opStack[opStackPointer++] = "+";
                op = "+";
                break;
            case R.id.button11:     // 按鍵 -
                // 存入數值
                postfixStr[postfixPointer++] = w_editTextString;
                w_editTextString = "";
                // 將 op stack 全取出
                if (!op.equals(""))
                {
                    for (i = --opStackPointer; i >= 0; i--)
                    {
                        postfixStr[postfixPointer++] = opStack[i];
                    }
                    opStackPointer = 0;
                }
                // 將新的 op 放入 stack
                opStack[opStackPointer++] = "-";
                op = "-";
                break;
            case R.id.button12:     // 按鍵 *
                // 存入數值
                postfixStr[postfixPointer++] = w_editTextString;
                w_editTextString = "";
                // 將新的 op 放入 stack
                opStack[opStackPointer++] = "*";
                op = "*";
                break;
            case R.id.button13:     // 按鍵 /
                // 存入數值
                postfixStr[postfixPointer++] = w_editTextString;
                w_editTextString = "";
                // 將新的 op 放入 stack
                opStack[opStackPointer++] = "/";
                op = "/";
                break;
            case R.id.button14:     // 按鍵 =
                postfixStr[postfixPointer++] = w_editTextString;
                w_editTextString = "";
                
                for (i = --opStackPointer; i >= 0; i--)
                {
                    postfixStr[postfixPointer++] = opStack[i];
                }
                //w_editText.setText(" "+postfixPointer);
                for (i = 0; i < postfixPointer; i++)
                {
                    w_editTextString = w_editTextString + postfixStr[i] + " ";
                }
                w_editText.setText(w_editTextString);
                w_editText2.setText(Integer.toString(compute(postfixStr, postfixPointer)));
                break;
        
            // 按鍵 AC :回到初始值
            case R.id.button15:
                w_editTextString = "";  // w_editText 內的字串
                postfixPointer = 0;                 
                opStackPointer = 0;
                op = "";
                w_editText.setText("");
                w_editText2.setText("");
                break;
        }   // end of switch
    }   // edn of onClick

}

layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">
    <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content">
        <requestFocus></requestFocus>
    </EditText>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
        <Button android:text="7" android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="8" android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="9" android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="+" android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content">
        <Button android:text="4" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="5" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="6" android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="–" android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="wrap_content">
        <Button android:text="1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="3" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="*" android:id="@+id/button12" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout4" android:layout_width="match_parent" android:layout_height="wrap_content">
        <Button android:text="0" android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="AC" android:id="@+id/button15" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="=" android:id="@+id/button14" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="/" android:id="@+id/button13" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText>
  
</LinearLayout>

Last modified 13 years ago Last modified on Jun 21, 2011, 12:44:53 AM

Attachments (1)

Download all attachments as: .zip