开发计算器源代码

package com.example.android_layout;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button one, two, three, four, five, six, seven, eight, nine, point,
			zero, equal, plus, minus, multiply, except, del, clear;
	private EditText editText;
	private Double resultDouble = 0.0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		editText = (EditText) findViewById(R.id.msg);
		editText.setCursorVisible(false);
		one = (Button) findViewById(R.id.btnone);
		one.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity") || str.equals("Infinity")) {
					str = "1";
				} else {
					str = str + "1";
				}
				editText.setText(str);
				
			}
		});
		two = (Button) findViewById(R.id.btntwo);
		two.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "2";
				} else {
					str = str + "2";
				}
				editText.setText(str);
				
			}
		});
		three = (Button) findViewById(R.id.btnthree);
		three.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "3";
				} else {
					str = str + "3";
				}
				editText.setText(str);
				
			}
		});
		four = (Button) findViewById(R.id.btnfour);
		four.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "4";
				} else {
					str = str + "4";
				}
				editText.setText(str);
				
			}
		});
		five = (Button) findViewById(R.id.btnfive);
		five.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "5";
				} else {
					str = str + "5";
				}
				editText.setText(str);
				
			}
		});
		six = (Button) findViewById(R.id.btnsix);
		six.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "6";
				} else {
					str = str + "6";
				}
				editText.setText(str);
				
			}
		});
		seven = (Button) findViewById(R.id.btnseven);
		seven.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "7";
				} else {
					str = str + "7";
				}
				editText.setText(str);
				
			}
		});
		eight = (Button) findViewById(R.id.btneight);
		eight.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "8";
				} else {
					str = str + "8";
				}
				editText.setText(str);
				
			}
		});
		nine = (Button) findViewById(R.id.btnnine);
		nine.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "9";
				} else {
					str = str + "9";
				}
				editText.setText(str);
				
			}
		});
		zero = (Button) findViewById(R.id.btnzero);
		zero.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.equals("0") || str.equals(eq) || str.equals("Infinity")) {
					str = "0";
				} else {
					str = str + "0";
				}
				editText.setText(str);
				
			}
		});
		point = (Button) findViewById(R.id.btnpoint);
		point.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				String eq = resultDouble.toString();
				if (eq.substring(eq.indexOf(".") + 1).equals("0")) {
					eq = eq.substring(0, eq.indexOf("."));
				}
				if (str.length() == 0 || str.equals(eq)) {
					str = "0.";
				} else if (str.indexOf(".") < 0) {
					str = str + ".";
				}
				editText.setText(str);
				
			}
		});

		del = (Button) findViewById(R.id.btndel);
		del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				if (str.length() > 0) {
					String eq = resultDouble.toString();
					if (str.equals(eq)) {
						str = "0";
					} else {
						str = str.substring(0, str.length() - 1);
					}

				}
				if (str.length() == 0)
					str = "0";
				editText.setText(str);
				
			}
		});
		clear = (Button) findViewById(R.id.btnclear);
		clear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				editText.setText("0");
				editText.setSelection(0);
			}
		});
		plus = (Button) findViewById(R.id.btnplus);
		plus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
						|| str.endsWith("÷")) {
					str = str.substring(0, str.length() - 1) + "+";
				} else {
					str = str + "+";
				}
				editText.setText(str);
				
			}
		});
		multiply = (Button) findViewById(R.id.btnmultiply);
		multiply.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
						|| str.endsWith("÷")) {
					str = str.substring(0, str.length() - 1) + "×";
				} else {
					str = str + "×";
				}
				editText.setText(str);
				
			}
		});
		minus = (Button) findViewById(R.id.btnminus);
		minus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
						|| str.endsWith("÷")) {
					str = str.substring(0, str.length() - 1) + "-";
				} else {
					str = str + "-";
				}
				editText.setText(str);
				
			}
		});
		except = (Button) findViewById(R.id.btnexcept);
		except.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				String str = editText.getText().toString();
				if (str.endsWith("+") || str.endsWith("-") || str.endsWith("×")
						|| str.endsWith("÷")) {
					str = str.substring(0, str.length() - 1) + "÷";
				} else {
					str = str + "÷";
				}
				editText.setText(str);
				
			}
		});
		equal = (Button) findViewById(R.id.btnequal);
		equal.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String str = editText.getText().toString();
				resultDouble = Calculate(str);
				String iszero = resultDouble.toString().substring(
						resultDouble.toString().indexOf(".") + 1);
				if (iszero.equals("0")) {
					str = resultDouble.toString().substring(0,
							resultDouble.toString().indexOf("."));
				} else {
					str = resultDouble.toString();
				}
				editText.setText(str);
				
			}
		});
	}

	protected Double Calculate(String str) {
		List<String> symbol = getsymbol(str);
		List<Double> num = getnum(str);
		if(num.size()==symbol.size())
			num.add(0.0);
		if (symbol != null) {
			for (int i = 0; i < symbol.size(); i++) {
				if (symbol.get(i).equals("×") || symbol.get(i).equals("÷")) {
					String sign = symbol.remove(i);
					Double n1 = num.remove(i);
					Double n2 = num.remove(i);
					Double result = 0.0;
					if (sign.equals("×")) {
						result = n1 * n2;
					} else {
						result = n1 / n2;
					}
					num.add(i, result);
					i = i - 1;
				}
			}
		}
		while (!symbol.isEmpty()) {
			String sign = symbol.remove(0);
			Double n1 = num.remove(0);
			Double n2 = num.remove(0);
			Double result = 0.0;
			if (sign.equals("+")) {
				result = n1 + n2;
			} else {
				result = n1 - n2;
			}
			num.add(0, result);
		}
		return num.remove(0);
	}

	private List<Double> getnum(String str) {
		StringTokenizer tokenizer = new StringTokenizer(str, "+-×÷");
		List<Double> ops = new ArrayList<Double>();
		while (tokenizer.hasMoreElements()) {
			String string = tokenizer.nextElement().toString();
			Double double1 = Double.parseDouble(string);
			ops.add(double1);
		}
		return ops;
	}

	private List<String> getsymbol(String str) {
		StringTokenizer tokenizer = new StringTokenizer(str, "0123456789.");
		List<String> ops = new ArrayList<String>();
		while (tokenizer.hasMoreElements()) {
			String string = tokenizer.nextToken();
			ops.add(string);
		}
		return ops;
	}

}

第一次做手机软件,也许有很多问题,欢迎大家来指出

下载地址http://download.csdn.net/detail/ymq_2012/7028217

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值