[[PageOutline]] 輸入:1 + 2 * 3 - 4 / 2 + 18 [[Image(1.png)]] = source code = {{{ #!java /* * 這個計算機會將輸入四則運算轉為 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 }}}