首页 > 代码库 > android之计算器

android之计算器

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/show"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:editable="false"
            android:gravity="right"
            android:hint="@string/class3_1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:layout_weight="6"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="vertical" >
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
                <Button
                    android:id="@+id/bt_c"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="C" />
                <Button
                    android:id="@+id/bt_del"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="DEL" />
                <Button
                    android:id="@+id/bt_d"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="÷" />
                <Button
                    android:id="@+id/bt_m"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="×" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
                <Button
                    android:id="@+id/bt_7"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="7" />
                <Button
                    android:id="@+id/bt_8"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="8" />
                <Button
                    android:id="@+id/bt_9"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="9" />
                <Button
                    android:id="@+id/bt_di"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="-" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >
                <Button
                    android:id="@+id/bt_4"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="4" />
                <Button
                    android:id="@+id/bt_5"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="5" />
                <Button
                    android:id="@+id/bt_6"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="6" />
                <Button
                    android:id="@+id/bt_a"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="+" />
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal" >
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:orientation="vertical" >
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal" >
                    <Button
                        android:id="@+id/bt_1"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="1" />
                    <Button
                        android:id="@+id/bt_2"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="2" />
                    <Button
                        android:id="@+id/bt_3"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="3" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal" >
                    <Button
                        android:id="@+id/bt_0"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="2"
                        android:text="0" />
                    <Button
                        android:id="@+id/bt_p"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="." />
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="vertical" >
                <Button
                    android:id="@+id/bt_e"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="=" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
j

java 代码:

 

package com.example.class3;
import java.util.Stack;
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;
import com.example.lesson.R;
public class Activity3 extends Activity implements OnClickListener {
 private EditText show;
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.class3_1);
  show = (EditText) findViewById(R.id.show);
  /*
   * 数字键监听
   */
  findViewById(R.id.bt_0).setOnClickListener(this);
  findViewById(R.id.bt_1).setOnClickListener(this);
  findViewById(R.id.bt_2).setOnClickListener(this);
  findViewById(R.id.bt_3).setOnClickListener(this);
  findViewById(R.id.bt_4).setOnClickListener(this);
  findViewById(R.id.bt_5).setOnClickListener(this);
  findViewById(R.id.bt_6).setOnClickListener(this);
  findViewById(R.id.bt_7).setOnClickListener(this);
  findViewById(R.id.bt_8).setOnClickListener(this);
  findViewById(R.id.bt_9).setOnClickListener(this);
  /*
   * 功能键监听
   */
  findViewById(R.id.bt_c).setOnClickListener(this);
  findViewById(R.id.bt_p).setOnClickListener(this);
  findViewById(R.id.bt_del).setOnClickListener(this);
  findViewById(R.id.bt_e).setOnClickListener(this);
  /*
   * 运算符监听
   */
  findViewById(R.id.bt_di).setOnClickListener(this);
  findViewById(R.id.bt_d).setOnClickListener(this);
  findViewById(R.id.bt_a).setOnClickListener(this);
  findViewById(R.id.bt_m).setOnClickListener(this);
 }
 public void onClick(View view) {
  switch (view.getId()) {
  case R.id.bt_c:
   show.setText("");
   break;
  case R.id.bt_del:
   String text = show.getText().toString();
   text = (String) text.subSequence(0, text.length() - 1);
   show.setText(text);
   break;
  case R.id.bt_e:
   Calculate();
   break;
  default:
   Button button = (Button) findViewById(view.getId());
   show.setText(show.getText() + "" + button.getText());
   break;
  }
 }
 private void Calculate() {
  Stack<Double> num = new Stack<Double>();
  Stack<Character> operator = new Stack<Character>();
  char[] op = { ‘#‘, ‘+‘, ‘-‘, ‘ב, ‘÷‘ };
  int[] rules = new int[256];
  String express = show.getText().toString();
  express += ‘#‘;
  rules[op[0]] = 0;
  rules[op[1]] = rules[op[2]] = 1;
  rules[op[3]] = rules[op[4]] = 2;
  operator.push(‘#‘);
  int i = 0, len = express.length();
  double value1 = 0, value2 = 0;
  double value = http://www.mamicode.com/0;
  boolean isNum = false;
  boolean firstNum = false;
  boolean isOver = false;
  boolean isZero = false;
  boolean isXiao = false;
  double index = 1;
  String ans = "";
  while (i < len) {
   char ch = express.charAt(i);
   if ((ch >= ‘0‘ && ch <= ‘9‘) || ch == ‘.‘) {
    if (ch == ‘.‘)
     isXiao = true;
    else {
     if (isXiao == true) {
      index *= 0.1;
      value2 = value2 + index * (ch - ‘0‘);
     } else
      value1 = value1 * 10 + ch - ‘0‘;
    }
    System.out.println("c = " + ch);
    System.out.print("value1 = " + value1);
    System.out.println("           value2 = " + value2);
    isNum = true;
   } else if (isNum == true) {
    value = http://www.mamicode.com/value1 + value2;
    if (firstNum == true)
     value = http://www.mamicode.com/-value;
    num.push(value);
    value1 = value2 = 0;
    index = 1;
    isXiao = firstNum = isNum = false;
    while (rules[ch] <= rules[operator.peek()]) {
     char c = operator.pop();
     if (c == ‘#‘) {
      isOver = true;
      break;
     }
     double num2 = num.pop();
     double num1 = num.pop();
     if (c == ‘÷‘ && num2 == 0) {
      isZero = true;
      break;
     }
     double v = calcu(num1, c, num2);
     num.push(v);
    }
    if (isOver == true || isZero == true)
     break;
    operator.push(ch);
   } else
    firstNum = true;
   i++;
  }
  if (isZero == true)
   ans = "表达式不成立(出现了除数为0的情况)";
  else
   ans = num.pop() + "";
  show.setText(express.substring(0, express.length() - 1) + " = "
    + ans);
 }
 private double calcu(double num1, char ch, double num2) {
  double ans = 0.0;
  switch (ch) {
  case ‘+‘:
   ans = num1 + num2;
   break;
  case ‘-‘:
   ans = num1 - num2;
   break;
  case ‘ב:
   ans = num1 * num2;
   break;
  case ‘÷‘:
   ans = num1 / num2;
   break;
  default:
   break;
  }
  return ans;
 }
}

android之计算器