这个程序是在别人的程序基础上修改而成。有一个bug调了一天才调出来。汗!
下载地址:http://download.csdn.net/detail/hong0220/4675631
package com.calculator; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; import android.view.View.OnClickListener; public class Calculator extends Activity { //创建控件的引用 private EditText result; private Button delete; private Button clear; private Button one; private Button two; private Button three; private Button four; private Button five; private Button six; private Button seven; private Button eight; private Button nine; private Button zero; private Button division; private Button multiply; private Button subtract; private Button addition; private Button amount; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得控件的实例 result = (EditText)findViewById(R.id.result); delete = (Button)findViewById(R.id.delete); clear = (Button)findViewById(R.id.clear); 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); zero = (Button)findViewById(R.id.zero); division = (Button)findViewById(R.id.division); multiply = (Button)findViewById(R.id.multiply); subtract = (Button)findViewById(R.id.subtract); addition = (Button)findViewById(R.id.addition); amount = (Button)findViewById(R.id.amount); //定义按键的监听器 clear.setOnClickListener(new clearClickListener()); delete.setOnClickListener(new deleteClickListener()); one.setOnClickListener(new oneClickListener()); two.setOnClickListener(new twoClickListener()); three.setOnClickListener(new threeClickListener()); four.setOnClickListener(new fourClickListener()); five.setOnClickListener(new fiveClickListener()); six.setOnClickListener(new sixClickListener()); seven.setOnClickListener(new sevenClickListener()); eight.setOnClickListener(new eightClickListener()); nine.setOnClickListener(new nineClickListener()); zero.setOnClickListener(new zeroClickListener()); division.setOnClickListener(new divisionClickListener()); multiply.setOnClickListener(new multiplyClickListener()); subtract.setOnClickListener(new subtractClickListener()); addition.setOnClickListener(new additionClickListener()); amount.setOnClickListener(new amountClickListener()); } class clearClickListener implements OnClickListener{ public void onClick(View v) { result.setText(""); } } class deleteClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); if(str.length()>=1){ result.setText(str.substring(0, str.length()-1)); } } } class oneClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+1); } } class twoClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+2); } } class threeClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+3); } } class fourClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+4); } } class fiveClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+5); } } class sixClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+6); } } class sevenClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+7); } } class eightClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+8); } } class nineClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+9); } } class zeroClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); result.setText(str+0); } } class divisionClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); char ch = str.charAt(str.length()-1); if(ch != '+' && ch != '-' && ch != '/' && ch != '*'){ result.setText(str+"/"); }else{ result.setText(str.substring(0, str.length()-1)+"/"); } } } class multiplyClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); char ch = str.charAt(str.length()-1); if(ch != '+' && ch != '-' && ch != '/' && ch != '*'){ result.setText(str+"*"); }else{ result.setText(str.substring(0, str.length()-1)+"*"); } } } class subtractClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); char ch = str.charAt(str.length()-1); if(ch != '+' && ch != '-' && ch != '/' && ch != '*'){ result.setText(str+"-"); }else{ result.setText(str.substring(0, str.length()-1)+"-"); } } } class additionClickListener implements OnClickListener{ public void onClick(View v) { String str = result.getText().toString(); char ch = str.charAt(str.length()-1); if(ch != '+' && ch != '-' && ch != '/' && ch != '*'){ result.setText(str+"+"); }else{ result.setText(str.substring(0, str.length()-1)+"+"); } } } class amountClickListener implements OnClickListener{ public void onClick(View v) { int now = 100; int index = 1; int[] operator = new int[10]; operator[0] = 0; double a = 0; double b = 0; ArrayList<Double> numArray = new ArrayList<Double>(); String str = result.getText().toString(); StringBuffer bsNum = new StringBuffer(); StringBuffer bsOper = new StringBuffer(); String ss[]=new String[100]; int t=0; for(int i=0; i<str.length(); i++) { if(str.charAt(i) >= 48 && str.charAt(i) <= 57) bsNum.append(str.charAt(i)); else if(str.charAt(i)== '+' || str.charAt(i)== '-' || str.charAt(i)== '*' || str.charAt(i)== '/') { ss[t]=bsNum.toString(); t++; int sum=bsNum.length(); bsNum.delete(0, sum); if(bsOper.toString().equals("")) { bsOper.append(str.charAt(i)); switch(str.charAt(i)) { case '+': now = 1; break; case '-': now = 1; break; case '*': now = 2; break; case '/': now = 2; break; default: break; } operator[index] = now; index++; }else { switch(str.charAt(i)) { case '+': now = 1; break; case '-': now = 1; break; case '*': now = 2; break; case '/': now = 2; break; default: break; } while(now <= operator[index - 1]){ bsNum.append(bsOper.charAt(bsOper.length()-1)); ss[t]=bsNum.toString(); t++; sum=bsNum.length(); bsNum.delete(0, sum); bsOper.deleteCharAt(bsOper.length()-1); index--; } bsOper.append(str.charAt(i)); operator[index] = now; index++; } } System.out.println("bsNum : "+bsNum); System.out.println("bsOper : "+bsOper); } ss[t]=bsNum.toString(); t++; int sum=bsNum.length(); bsNum.delete(0, sum); while(!bsOper.toString().equals("")){ bsNum.append(bsOper.charAt(bsOper.length()-1)); ss[t]=bsNum.toString(); t++; sum=bsNum.length(); bsNum.delete(0, sum); bsOper.deleteCharAt(bsOper.length()-1); } System.out.println("bsNum : "+bsNum); System.out.println("bsOper : "+bsOper); System.out.println("hello world!"); for(int i=0;i<t;i++) System.out.println(ss[i]); for(int i=0; i<t; i++){ if(ss[i].equals("+")) { a = numArray.get(numArray.size()-1); b = numArray.get(numArray.size()-2); numArray.remove(numArray.size()-1); numArray.remove(numArray.size()-1); System.out.println(a+" "+b); numArray.add(Double.valueOf(b+a)); }else if(ss[i].equals("-")) { a = numArray.get(numArray.size()-1); b = numArray.get(numArray.size()-2); numArray.remove(numArray.size()-1); numArray.remove(numArray.size()-1); System.out.println(a+" "+b); numArray.add(Double.valueOf(b-a)); }else if(ss[i].equals("*")) { a = numArray.get(numArray.size()-1); b = numArray.get(numArray.size()-2); numArray.remove(numArray.size()-1); numArray.remove(numArray.size()-1); System.out.println(a+" "+b); numArray.add(Double.valueOf(b*a)); }else if(ss[i].equals("/")) { a = numArray.get(numArray.size()-1); b = numArray.get(numArray.size()-2); numArray.remove(numArray.size()-1); numArray.remove(numArray.size()-1); System.out.println(a+" "+b); numArray.add(Double.valueOf(b/a)); } else numArray.add(Double.parseDouble(ss[i])); } System.out.println("result : "+numArray.get(0).doubleValue()); String s = String.valueOf(numArray.get(0).doubleValue()); result.setText(String.valueOf(s)); } } }