欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

简单计算器 安卓 Android 作业

程序员文章站 2022-04-09 17:40:28
Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com/wonke/Calculator 使用GirdLayout比较适合计算器这种规则的布局,每行有几个 ......

android老师布置的课程作业——简单计算器

功能要求实现四则运算,参考界面见下图:

简单计算器 安卓 Android 作业

 

首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~

github地址:https://github.com/wonke/calculator

使用girdlayout比较适合计算器这种规则的布局,每行有几个按钮几乎都是固定的,遇到一个按钮需要占两个常规按钮空间时,使用android:layout_columnspan="2"属性即可解决。

 

layout布局代码:

<?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=".mainactivity">

    <textview

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="30dp"
        android:gravity="center"
        android:text="@string/title"
        android:textappearance="@style/textappearance.appcompat.large" />

    <gridlayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="500sp"
        android:background="#ece7e7"
        android:alignmentmode="alignbounds"
        android:columncount="4"
        android:orientation="horizontal"
        android:rowcount="10"
        android:usedefaultmargins="false">

        <textview
            android:id="@+id/contenttext"
            android:background="@drawable/textview_border"
            android:gravity="end"
            android:textsize="50sp"

            android:layout_columnspan="4"
            android:layout_columnweight="1"
            android:layout_rowweight="5"
            />

        <button
            android:id="@+id/allclear"
            android:textsize="30sp"
            android:background="@drawable/button_border"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:text="ce" />

        <button
            android:id="@+id/divide"
            android:textsize="30sp"
            android:background="@drawable/button_border"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:text="÷" />

        <button
            android:id="@+id/multiply"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="×"
            android:textsize="30sp" />

        <button
            android:id="@+id/clear"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="c"
            android:textsize="30sp" />

        <button
            android:id="@+id/b7"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="7"
            android:textsize="30sp" />

        <button
            android:id="@+id/b8"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="8"
            android:textsize="30sp" />

        <button
            android:id="@+id/b9"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="9"
            android:textsize="30sp" />

        <button
            android:id="@+id/add"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="+"
            android:textsize="30sp" />

        <button
            android:id="@+id/b4"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="4"
            android:textsize="30sp" />

        <button
            android:id="@+id/b5"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="5"
            android:textsize="30sp" />

        <button
            android:id="@+id/b6"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="6"
            android:textsize="30sp" />

        <button
            android:id="@+id/subtract"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="-"
            android:textsize="30sp" />

        <button
            android:id="@+id/b1"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="1"
            android:textsize="30sp" />

        <button
            android:id="@+id/b2"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="2"
            android:textsize="30sp" />

        <button
            android:id="@+id/b3"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="3"
            android:textsize="30sp" />

        <button
            android:id="@+id/root"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="√ ̄"
            android:textsize="30sp" />

        <button
            android:id="@+id/b0"
            android:layout_rowweight="1"
            android:layout_columnspan="2"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="0"
            android:textsize="30sp" />

        <button
            android:id="@+id/point"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="."
            android:textsize="30sp" />

        <button
            android:id="@+id/equal"
            android:layout_rowweight="1"
            android:layout_columnweight="1"
            android:background="@drawable/button_border"
            android:text="="
            android:textsize="30sp" />
    </gridlayout>

</linearlayout>

mainactivity逻辑处理代码:

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.textview;

public class mainactivity extends appcompatactivity implements view.onclicklistener {

    private button[] buttons = new button[18];
    private int[] ids = new int[]{r.id.allclear,r.id.divide,r.id.multiply,r.id.clear,r.id.b0,r.id.b1,
            r.id.b2,r.id.b3,r.id.b4,r.id.b5,r.id.b6,r.id.b7,r.id.b8,r.id.b9,r.id.add,r.id.subtract,
            r.id.equal,r.id.point};

    private textview textview;
    private string expression = "";
    private boolean end = false;
    private int countoperate=2;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

       for (int i=0; i<ids.length;i++){
           buttons[i] = findviewbyid(ids[i]);
           buttons[i].setonclicklistener(this);
       }
        textview = (textview)findviewbyid(r.id.contenttext);
    }
    public void onclick(view view) {
        int id = view.getid();
        button button = (button)view.findviewbyid(id);
        string current = button.gettext().tostring();
        if(end){ //如果上一次算式已经结束,则先清零
            expression = "";
            end = false;
        }
        if(current.equals("ce")){ //如果为ce则清零
            expression = "";
            countoperate=0;
        }else if(current.equals("c")){ //如果点击退格
            if(expression.length()>1){ //算式长度大于1
                expression = expression.substring(0,expression.length()-1);//退一格
                int i = expression.length()-1;
                char tmp = expression.charat(i); //获得最后一个字符
                char tmpfront = tmp;
                for(;i>=0;i--){ //向前搜索最近的 +-*/和.,并退出
                    tmpfront = expression.charat(i);
                    if(tmpfront=='.'||tmpfront=='+'||tmpfront=='-'||tmpfront=='×'||tmpfront=='÷'){
                        break;
                    }
                }
                if(tmp>='0'&&tmp<='9'){ //最后一个字符为数字,则识别数赋值为0
                    countoperate=0;
                }else if(tmp==tmpfront&&tmpfront!='.') countoperate=2; //如果为+-*/,赋值为2
                else if(tmpfront=='.') countoperate=1; //如果前面有小数点赋值为1
            }else if(expression.length()==1){
                expression = "";
            }
        }else if(current.equals(".")){
            if(expression.equals("")||countoperate==2){
                expression+="0"+current;
                countoperate = 1;  //小数点按过之后赋值为1
            }
            if(countoperate==0){
                expression+=".";
                countoperate = 1;
            }
        }else if(current.equals("+")||current.equals("-")||current.equals("×")||current.equals("÷")){
            if(countoperate==0){
                expression+=current;
                countoperate = 2;  //  +-*/按过之后赋值为2
            }
        }else if(current.equals("=")){ //按下=时,计算结果并显示
            double result = (double) math.round(count()*100)/100;
            expression+="="+result;
            end = true; //此次计算结束
        }
        else{//此处是当退格出现2+0时,用current的值替代0
            if(expression.length()>=1){
                char tmp1 = expression.charat(expression.length()-1);
                if(tmp1=='0'&&expression.length()==1){
                    expression = expression.substring(0,expression.length()-1);
                }
                else if(tmp1=='0'&&expression.length()>1){
                    char tmp2 = expression.charat(expression.length()-2);
                    if(tmp2=='+'||tmp2=='-'||tmp2=='×'||tmp2=='÷'){
                        expression = expression.substring(0,expression.length()-1);
                    }
                }
            }
            expression+=current;
            if(countoperate==2||countoperate==1) countoperate=0;
        }
        //    toast.maketext(this, "countoperate:"+countoperate, toast.length_short).show();
        textview.settext(expression); //显示出来
    }
    //计算逻辑,求expression表达式的值
    private double count(){
        double result=0;
        double tnum=1,lownum=0.1,num=0;
        char tmp=0;
        int operate = 1; //识别+-*/,为+时为正数,为-时为负数,为×时为-2/2,为/时为3/-3;
        boolean point = false;
        for(int i=0;i<expression.length();i++){ //遍历表达式
            tmp = expression.charat(i);
            if(tmp=='.'){ //因为可能出现小数,此处进行判断是否有小数出现
                point = true;
                lownum = 0.1;
            }else if(tmp=='+'||tmp=='-'){
                if(operate!=3&&operate!=-3){ //此处判断通用,适用于+-*
                    tnum *= num;
                }else{ //计算/
                    tnum /= num;
                }
                //    toast.maketext(this, "tnum = "+tnum, toast.length_short).show();
                if(operate<0){ //累加入最终的结果
                    result -= tnum;
                }else{
                    result += tnum;
                }
                operate = tmp=='+'?1:-1;
                num = 0;
                tnum = 1;
                point = false;
            }else if(tmp=='×'){
                if(operate!=3&&operate!=-3){
                    tnum *= num;
                }else{
                    tnum /= num;
                }
                operate = operate<0?-2:2;
                point = false;
                num = 0;
            }else if(tmp=='÷'){
                if(operate!=3&&operate!=-3){
                    tnum *= num;
                }else{
                    tnum /= num;
                }
                operate = operate<0?-3:3;
                point = false;
                num = 0;
            }else{
                //读取expression中的每个数字,doube型
                if(!point){
                    num = num*10+tmp-'0';
                }else{
                    num += (tmp-'0')*lownum;
                    lownum*=0.1;
                }
            }
        }
        //循环遍历结束,计算最后一个运算符后面的数
        if(operate!=3&&operate!=-3){
            tnum *= num;
        }else{
            tnum /= num;
        }
        //    toast.maketext(this, "tnum = "+tnum, toast.length_short).show();
        if(operate<0){
            result -= tnum;
        }else{
            result += tnum;
        }
        //返回最后的结果
        return result;
    }

}