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 = 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 = value1 + value2;
    if (firstNum == true)
     value = -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;
 }
}

时间: 2024-08-03 11:07:32

android之计算器的相关文章

android实现计算器功能

设计一个简单的计算器. 第一个Activity的界面. 第二个Activity显示算式和计算结果. 第一个Activity代码: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; impor

Android Studio计算器

---恢复内容开始--- 从计算器的例子中学到,1 导入相应包声明alt+enter2 ctrl+o对父类方法进行重写3 删除一行ctrl+x4 padding表示内边距布局文件写在layout布局中,layout中的布局背景颜色在drawable中,drawable中引用的颜色在values中的color.xml中 取出显示的内容String str=et_input.getText().toString() MainActivity 1 package calcluatedemo.examp

【留念贴】Android开发——计算器

[过程] 在电商学霸&&代码女神XuFei的影响下,接触到了关于Android客户端的一些开发,第一次在Android平台搞出了一个App,真的是激动不已,所以必须开个留念贴记录一下. 本来准备一晚上搞定计算器的,但是当我看到那些天花乱坠的语法,简直蛋碎!最终吃起了烧烤....下起了飞行棋....五子棋....还被虐!!....T T 第二天又看了一下ChenYong的代码,感觉可以写了,理论上能做出来,但是没有装编译器.....因此花一晚上的时间去装.....终于跑起来了....但是不知

android的计算器

今天我闲着无聊,便想仿照Iphone的计算器自己写一个出来玩玩,于是就开动脑经,来场头脑风暴了!我拿什么了写呢?wp?这是个不错的选择,但是我最近在研究安卓的开发(求各位MSP不要烧我,我买不起MAC,你们要搞应该找昌豪,他有MAC,先烧死他再来烧我~~~)搞安卓开发其实还是蛮简单的!不就是监听,然后实现功能吗?跟WP的开发过程有点像  <Button  click = "clickaction"\>  在后台process中会自动生成触发函数 这是wp的开发过程,而安卓呢

Android简易计算器

这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了.现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多.采用的是相对布局,但是此布局只适合480*800的分辨率,更高的分辨率界面会显示不正常,但是功能没有任何问题,你可以尝试用其他布局来实现界面,总之核心功能实现是不变的. activity_main.xml文件如下: <RelativeLayout xmlns:android="http://schemas.a

Android 简单计算器源码....

PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧...     顺便去对比了一下别人写的代码...有的使用到了集合框架去实现,我却是用的数组,相比之下实在是有点弱爆了...sad....自己还需要努力啊... <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_widt

Android课程---计算器的实现

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hanqi.test5"> <application android:allowBackup="true" android:icon=&q

Android 简单计算器

本例子基于Android googl sdk一书的计算器实现例子: 1 创建三个TextView对象,两个 EditText对象,四个Button对象 main.xml为 <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" andr

Android简单计算器

这是很久之前做的,方法很简单,都是一些基础的,但还有很多地方需要改善的. activity_main.xml布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation=