找传奇、传世资源到传世资源站!

android_简易计算器_实例源码下载_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android_简易计算器_实例源码下载_ Android平台开发-第1张

package com.example.sjq;

import java.util.Iterator;
import java.util.LinkedList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private Button btn_zero;
	private Button btn_one;
	private Button btn_two;
	private Button btn_three;
	private Button btn_four;
	private Button btn_five;
	private Button btn_six;
	private Button btn_seven;
	private Button btn_eight;
	private Button btn_nine;
	private Button btn_plus;
	private Button btn_sub;
	private Button btn_mul;
	private Button btn_div;
	private Button btn_point;
	private Button btn_equal;
	private Button btn_del;
	private Button btn_c;
	private EditText et;
	LinkedList<String> center = new LinkedList<String>();// 对et进行解析,即存放中缀表达式的链表
	LinkedList<String> postfix = new LinkedList<String>();// 存放后缀表达式的链表
	LinkedList<Double> data = new LinkedList<Double>();// 存放后缀表达式的数字链表
	LinkedList<String> op = new LinkedList<String>();// 1:作为临时存放运算符的链表;2:存放后缀表达式的运算符

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initview();
	}

	private void initview() {
		btn_one = (Button) findViewById(R.id.btn_one);
		btn_two = (Button) findViewById(R.id.btn_two);
		btn_three = (Button) findViewById(R.id.btn_three);
		btn_four = (Button) findViewById(R.id.btn_four);
		btn_five = (Button) findViewById(R.id.btn_five);
		btn_six = (Button) findViewById(R.id.btn_six);
		btn_seven = (Button) findViewById(R.id.btn_seven);
		btn_eight = (Button) findViewById(R.id.btn_eight);
		btn_nine = (Button) findViewById(R.id.btn_nine);
		btn_plus = (Button) findViewById(R.id.btn_plus);
		btn_sub = (Button) findViewById(R.id.btn_sub);
		btn_mul = (Button) findViewById(R.id.btn_mul);
		btn_div = (Button) findViewById(R.id.btn_div);
		btn_point = (Button) findViewById(R.id.btn_point);
		btn_equal = (Button) findViewById(R.id.btn_equal);
		btn_one = (Button) findViewById(R.id.btn_one);
		btn_del = (Button) findViewById(R.id.btn_del);
		btn_c = (Button) findViewById(R.id.btn_c);
		btn_zero = (Button) findViewById(R.id.btn_zero);
		btn_one.setOnClickListener(this);
		btn_two.setOnClickListener(this);
		btn_three.setOnClickListener(this);
		btn_four.setOnClickListener(this);
		btn_five.setOnClickListener(this);
		btn_six.setOnClickListener(this);
		btn_seven.setOnClickListener(this);
		btn_eight.setOnClickListener(this);
		btn_nine.setOnClickListener(this);
		btn_plus.setOnClickListener(this);
		btn_sub.setOnClickListener(this);
		btn_mul.setOnClickListener(this);
		btn_div.setOnClickListener(this);
		btn_equal.setOnClickListener(this);
		btn_point.setOnClickListener(this);
		btn_del.setOnClickListener(this);
		btn_c.setOnClickListener(this);
		btn_zero.setOnClickListener(this);
		et = (EditText) findViewById(R.id.edit);
	}

	@Override
	public void onClick(View v) {
		String str = et.getText().toString();
		switch (v.getId()) {
		case R.id.btn_c:
			et.setText("");
			break;
		case R.id.btn_one:
		case R.id.btn_two:
		case R.id.btn_three:
		case R.id.btn_four:
		case R.id.btn_five:
		case R.id.btn_six:
		case R.id.btn_seven:
		case R.id.btn_eight:
		case R.id.btn_nine:
		case R.id.btn_zero:
		case R.id.btn_point:
		case R.id.btn_div:
		case R.id.btn_mul:
		case R.id.btn_plus:
		case R.id.btn_sub:
			str = ((TextView) v).getText();
			et.setText(str);
			et.setSelection(et.getText().length());
			break;
		case R.id.btn_equal:
			getresult(str);
			break;
		case R.id.btn_del:
			if (str.length() >= 1)
				et.setText(str.substring(0, str.length() - 1));
			break;
		default:
			break;
		}
	}

	private void getresult(String str) {
		MainActivity.this.Analysis(str);// 调用Analysis函数对et进行解析
		Iterator<String> it = center.iterator();
		while (it.hasNext()) {
			String tmpstr = it.next();
			if (isNum(tmpstr)) {
				postfix.addLast(tmpstr);
			} else {
				int peeklevel = op.isEmpty() ? 0 : getLevel(op.getLast());
				if (getLevel(tmpstr) > peeklevel) {
					op.addLast(tmpstr);
				} else {
					String str2 = op.removeLast();
					while (getLevel(str2) >= peeklevel) {
						postfix.addLast(str2);
						if (op.isEmpty()) {
							break;
						}
						str2 = op.removeLast();
					}
					op.addLast(tmpstr);
				}
			}
		}
		center.clear();
		if (postfix.size() <= op.size()) {
			et.setText("");
			postfix.clear();
			op.clear();
		} else {
			while (!op.isEmpty()) {
				postfix.addLast(op.removeLast());
			}
			System.out.println(postfix);
			while (!(postfix.isEmpty())) {
				String tmpstr = postfix.removeFirst();
				if (isOp(tmpstr)) {
					char ch = tmpstr.charAt(0);
					Double first = data.removeLast();
					Double last = data.removeLast();
					switch (ch) {
					case '*':
						data.addLast(last * first);
						break;
					case '/':
						data.addLast(last / first);
						break;
					case ' ':
						data.addLast(last  first);
						break;
					case '-':
						data.addLast(last - first);
						break;
					}
				} else {
					data.addLast(Double.parseDouble(tmpstr));
				}
			}
			Double result = data.removeFirst();
			et.setText(Double.toString(result));
			et.setSelection(et.getText().length());
		}
	}

	public boolean isNum(String str) {
		int num = 0;
		for (int i = 0; i < str.length(); i ) {
			String strr = str.substring(i, i  1);
			if (strr.equals("0") || strr.equals("1") || strr.equals("2")
					|| strr.equals("3") || strr.equals("4") || strr.equals("5")
					|| strr.equals("6") || strr.equals("7") || strr.equals("8")
					|| strr.equals("9") || strr.equals("."))
				num = num  1;
		}
		if (num == str.length())
			return true;
		else
			return false;
	}

	public boolean isOp(String strr) {
		if (strr.equals(" ") || strr.equals("-") || strr.equals("*")
				|| strr.equals("/"))
			return true;
		else
			return false;
	}

	public int getLevel(String str) {
		if (str.equals("*") || str.equals("/")) {
			return 2;
		} else if (str.equals(" ") || str.equals("-")) {
			return 1;
		} else {
			return 0;
		}
	}

	public void Analysis(String str) {
		String sub = "";
		for (int i = 0; i < str.length(); i ) {
			String strr = str.substring(i, i  1);
			if (isNum(strr)) {
				sub = strr;
			} else {
				if (sub != "") {
					center.addLast(sub);// 首先sub进center
					sub = "";// 将sub清空
				}
				center.addLast(strr);// " -*/" "(" ")" 则直接进center表
			}
		}
		if (isNum(str.substring(str.length() - 1))) {
			center.addLast(sub);// 首先sub进center
			sub = "";// 将sub清空
		}
	}

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复