简易计算器

界面如图:

1.布局设置

2.监听按钮

3.运算符优先级

1.布局

用户界面通过View和ViewGroup对象构建。View对象是android平台上表示用户界面的基本单元,一个View占据屏幕上的一块方形区域,存储该区域的布局参数和内容,所有可视化控件的基类,主要提供绘制控件和事件处理的方法。

View<----ViewGroup<----LinearLayout/FrameLayout/AbsoluteLayout/RelativeLayout

LinearLayout<----TableLayout

【箭头表示泛化关系】

布局属性说明

(1)ID属性

声明ID属性的方法:

android :id="@+id/idName"

idName就是自定义的id名。idName的用处在于,在MainActivity.java文件中需要用到某控件,直接寻找该idName即可使用该控件。(叫一声就答应了)

如Button控件:Button btn=(Button) findViewById(R.id.idName)。

(2)尺寸

layout_width和layout_height。根据自己的需求定义。

如:android:layout_width="match_parent"(屏幕多宽我多宽)

android:layout_width="wrap_content"(我多胖我就占多大的地)

其他属性如颜色位置等都可根据自己实际需求设置。

2.监听

事件:如键盘操作、鼠标操作。

事件源:控件,如按钮、文本框等。

事件处理者:接收事件并对其进行处理的对象,事件处理者一般是一个实现某些特定接口类的对象。

当事件源与事件监听器联系在一起,就需要为事件源注册监听事件,即为事件源对象(如Button)添加某个事件的监听。

当事件发生时,系统将会将事件封装成相应类型的事件对象,并发送给注册到事件源的事件监听器。当监听器对象接收到事件对象之后,回调用监听器中相应的事件处理方法来处理事件。

(1)直接实现接口的处理方式

@Override
public void onCreate(Bundle saveInstance) {
    ....
    Button myButton=(Button) findViewById(R.id.idName);
    myButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.idName:
            (处理事件)
        break;
        }
}

(2)内部类处理方式

@Override
public void onCreate(Bundle saveInstanceState) {
    Button myButton=(Button) findViewById(R.id.idName);
    myButton.setOnClickListener(new MyClickListener());
}

//定义内部类

class MyClickListener implements OnClickListener {
@Override
    public void onClick(View v) {
    //如上例
    }
}

(3)匿名内部类处理方式

@Override
public void onCreate(Bundle saveInstanceState) {
    Button myButton=(Button) findViewById(R.id.idName);
    myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//如上例    
}
    );
}

接口实现方法和内部类都是通过继承接口OnClickListener并重写其中的onClick()方法。

匿名实现类实现方法则是通过重写onClick()方法。

3.计算表达式

如:2.3+1.22*4-6/1.2=

因为是字符串,所以先转为运算符和浮点数。

把运算符和数分别放入两个栈。

不断取出数据进行运算,直到栈空。

处理优先级,乘除的优先级大于加减。此例中,栈顶到栈底:2.3 ~ 1.22 ~ 4 ~ 6 ~ 1.2,栈底到栈底:+ ~ * ~ - ~ /。

如果按照顺序那么不符合运算法则。那么每次取出三个数d1,d2,d3,两个运算符c1,c2,如果第一个运算符的优先级小于第二个运算符的优先级,那么先计算d2与d3的运算结果d,然后把d压入栈,把第一个数d1压入栈,把第一个运算符压入栈。循环,直至运算结束。

贴上代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hz.calculator.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shadowColor="@color/colorAccent"
        android:textColor="@color/colorPrimaryDark"
        android:text="thanks for using it"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/buttonMul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*" />
        <Button
            android:id="@+id/buttonDiv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/" />
        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />
        <Button
            android:id="@+id/buttonSub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/buttonZero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />
        <Button
            android:id="@+id/buttonOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />
        <Button
            android:id="@+id/buttonTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />
        <Button
            android:id="@+id/buttonThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/buttonFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />
        <Button
            android:id="@+id/buttonFive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />
        <Button
            android:id="@+id/buttonSix"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6" />
        <Button
            android:id="@+id/buttonSeven"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/buttonEight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8" />
        <Button
            android:id="@+id/buttonNine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9" />
        <Button
            android:id="@+id/buttonDot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="." />
        <Button
            android:id="@+id/buttonClear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C" />

    </LinearLayout>
    <Button
        android:id="@+id/buttonRes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        />
    <TextView
        android:id="@+id/textShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        />



</LinearLayout>
package com.example.hz.calculator;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import static java.sql.DriverManager.println;

public class MainActivity extends Activity implements View.OnClickListener{
   public StringBuilder s=new StringBuilder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonMul=(Button) findViewById(R.id.buttonMul);
        Button buttonDiv=(Button) findViewById(R.id.buttonDiv);
        Button buttonAdd=(Button) findViewById(R.id.buttonAdd);
        Button buttonSub=(Button) findViewById(R.id.buttonSub);
        Button buttonZero=(Button) findViewById(R.id.buttonZero);
        Button buttonOne=(Button) findViewById(R.id.buttonOne);
        Button buttonTwo=(Button) findViewById(R.id.buttonTwo);
        Button buttonThree=(Button) findViewById(R.id.buttonThree);
        Button buttonFour=(Button) findViewById(R.id.buttonFour);
        Button buttonFive=(Button) findViewById(R.id.buttonFive);
        Button buttonSix=(Button) findViewById(R.id.buttonSix);
        Button buttonSeven=(Button) findViewById(R.id.buttonSeven);
        Button buttonEight=(Button) findViewById(R.id.buttonEight);
        Button buttonNine=(Button) findViewById(R.id.buttonNine);
        Button buttonClear=(Button) findViewById(R.id.buttonClear);
        Button buttonDot=(Button) findViewById(R.id.buttonDot);
        Button buttonRes=(Button) findViewById(R.id.buttonRes);
        buttonAdd.setOnClickListener(this);
        buttonSub.setOnClickListener(this);
        buttonMul.setOnClickListener(this);
        buttonDiv.setOnClickListener(this);
        buttonRes.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
        buttonDot.setOnClickListener(this);
        buttonZero.setOnClickListener(this);
        buttonOne.setOnClickListener(this);
        buttonTwo.setOnClickListener(this);
        buttonThree.setOnClickListener(this);
        buttonFour.setOnClickListener(this);
        buttonFive.setOnClickListener(this);
        buttonSix.setOnClickListener(this);
        buttonSeven.setOnClickListener(this);
        buttonEight.setOnClickListener(this);
        buttonNine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        try {
            switch (v.getId()) {
                case R.id.buttonAdd:
                    System.out.println("add");
                    s.append("+");
                    break;
                case R.id.buttonSub:
                    System.out.println("sub");
                    s.append("-");
                    break;
                case R.id.buttonMul:
                    System.out.println("mul");
                    s.append("*");
                    break;
                case R.id.buttonDiv:
                    System.out.println("div");
                    s.append("/");
                    break;
                case R.id.buttonRes:
                    System.out.println("result");
                    s.append("=");
                    String ss = s.toString();
                    Calculate cal = new Calculate(ss);
                    double res=cal.result();
                    s.append(String.valueOf(res));
                    break;
                case R.id.buttonClear:
                    System.out.println("clear");
                    s.delete(0, s.capacity());
                    break;
                case R.id.buttonDot:
                    System.out.println("dot");
                    s.append(".");
                    break;
                case R.id.buttonZero:
                    System.out.println("0");
                    s.append("0");
                    break;
                case R.id.buttonOne:
                    System.out.println("1");
                    s.append("1");
                    break;
                case R.id.buttonTwo:
                    System.out.println("2");
                    s.append("2");
                    break;
                case R.id.buttonThree:
                    System.out.println("3");
                    s.append("3");
                    break;
                case R.id.buttonFour:
                    System.out.println("4");
                    s.append("4");
                    break;
                case R.id.buttonFive:
                    System.out.println("5");
                    s.append("5");
                    break;
                case R.id.buttonSix:
                    System.out.println("6");
                    s.append("6");
                    break;
                case R.id.buttonSeven:
                    System.out.println("7");
                    s.append("7");
                    break;
                case R.id.buttonEight:
                    System.out.println("8");
                    s.append("8");
                    break;
                case R.id.buttonNine:
                    System.out.println("9");
                    s.append("9");
                    break;
                default:
                    break;

            }
            TextView textShow = (TextView) findViewById(R.id.textShow);
            textShow.setText(s.toString());
            }catch (Exception e) {
                System.out.println("error in main_activity");
        }
    }

}
package com.example.hz.calculator;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import static java.sql.DriverManager.println;

public class MainActivity extends Activity implements View.OnClickListener{
   public StringBuilder s=new StringBuilder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonMul=(Button) findViewById(R.id.buttonMul);
        Button buttonDiv=(Button) findViewById(R.id.buttonDiv);
        Button buttonAdd=(Button) findViewById(R.id.buttonAdd);
        Button buttonSub=(Button) findViewById(R.id.buttonSub);
        Button buttonZero=(Button) findViewById(R.id.buttonZero);
        Button buttonOne=(Button) findViewById(R.id.buttonOne);
        Button buttonTwo=(Button) findViewById(R.id.buttonTwo);
        Button buttonThree=(Button) findViewById(R.id.buttonThree);
        Button buttonFour=(Button) findViewById(R.id.buttonFour);
        Button buttonFive=(Button) findViewById(R.id.buttonFive);
        Button buttonSix=(Button) findViewById(R.id.buttonSix);
        Button buttonSeven=(Button) findViewById(R.id.buttonSeven);
        Button buttonEight=(Button) findViewById(R.id.buttonEight);
        Button buttonNine=(Button) findViewById(R.id.buttonNine);
        Button buttonClear=(Button) findViewById(R.id.buttonClear);
        Button buttonDot=(Button) findViewById(R.id.buttonDot);
        Button buttonRes=(Button) findViewById(R.id.buttonRes);
        buttonAdd.setOnClickListener(this);
        buttonSub.setOnClickListener(this);
        buttonMul.setOnClickListener(this);
        buttonDiv.setOnClickListener(this);
        buttonRes.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
        buttonDot.setOnClickListener(this);
        buttonZero.setOnClickListener(this);
        buttonOne.setOnClickListener(this);
        buttonTwo.setOnClickListener(this);
        buttonThree.setOnClickListener(this);
        buttonFour.setOnClickListener(this);
        buttonFive.setOnClickListener(this);
        buttonSix.setOnClickListener(this);
        buttonSeven.setOnClickListener(this);
        buttonEight.setOnClickListener(this);
        buttonNine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        try {
            switch (v.getId()) {
                case R.id.buttonAdd:
                    System.out.println("add");
                    s.append("+");
                    break;
                case R.id.buttonSub:
                    System.out.println("sub");
                    s.append("-");
                    break;
                case R.id.buttonMul:
                    System.out.println("mul");
                    s.append("*");
                    break;
                case R.id.buttonDiv:
                    System.out.println("div");
                    s.append("/");
                    break;
                case R.id.buttonRes:
                    System.out.println("result");
                    s.append("=");
                    String ss = s.toString();
                    Calculate cal = new Calculate(ss);
                    double res=cal.result();
                    s.append(String.valueOf(res));
                    break;
                case R.id.buttonClear:
                    System.out.println("clear");
                    s.delete(0, s.capacity());
                    break;
                case R.id.buttonDot:
                    System.out.println("dot");
                    s.append(".");
                    break;
                case R.id.buttonZero:
                    System.out.println("0");
                    s.append("0");
                    break;
                case R.id.buttonOne:
                    System.out.println("1");
                    s.append("1");
                    break;
                case R.id.buttonTwo:
                    System.out.println("2");
                    s.append("2");
                    break;
                case R.id.buttonThree:
                    System.out.println("3");
                    s.append("3");
                    break;
                case R.id.buttonFour:
                    System.out.println("4");
                    s.append("4");
                    break;
                case R.id.buttonFive:
                    System.out.println("5");
                    s.append("5");
                    break;
                case R.id.buttonSix:
                    System.out.println("6");
                    s.append("6");
                    break;
                case R.id.buttonSeven:
                    System.out.println("7");
                    s.append("7");
                    break;
                case R.id.buttonEight:
                    System.out.println("8");
                    s.append("8");
                    break;
                case R.id.buttonNine:
                    System.out.println("9");
                    s.append("9");
                    break;
                default:
                    break;

            }
            TextView textShow = (TextView) findViewById(R.id.textShow);
            textShow.setText(s.toString());
            }catch (Exception e) {
                System.out.println("error in main_activity");
        }
    }

}

以及需要说明的是,浮点数的精度。

可参考:

http://www.cnblogs.com/riyueshiwang/p/4822677.html

原文地址:https://www.cnblogs.com/zhenzhenhuang/p/7507501.html