欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

androidstudio项目:UI设计高精度实现简单计算器,

来源: 开发者 投稿于  被查看 7133 次 评论:24

androidstudio项目:UI设计高精度实现简单计算器,


UI设计:

实验目的:

自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局。
实验要求:

  • 1.完成一个计算器的设计,可以以手机自带的计算器为参考。设计过程中,注意考虑界面的美观性,不同机型的适应性,以及功能的完备性。
  • 2.注意结合Activity的生命周期,考虑不同情况下计算器的界面状态。
  • 3.如有余力,可以考虑实现一个高精度科学计算型的计算器。

实现效果:

整数和浮点数的加减乘除、取余、开根号,异号、清零 计算结果全部实现。

精度保留到小数点后100位,比如下面是√3的计算结果。(可以自行修改精度)

在这里插入图片描述

完全满足日常使用,可以取代手机自带的计算器软件。。。

在这里插入图片描述

程序主要包含Mainactivity.javaCalculator.java activity_main.xml三个文件,分别用于控制、逻辑和视图。符合MVC框架。

文件结构:

在这里插入图片描述

代码:

MainActivity.java

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView1,textView2,textView_op,textView_res;
    Calculator calculator;

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

        textView1=(TextView) findViewById(R.id.textview_num1);
        textView2=(TextView)findViewById(R.id.textview_num2);
        textView_op=(TextView)findViewById(R.id.textview_op);
        textView_res=(TextView)findViewById(R.id.textview_res);

        Button t_C=(Button) findViewById(R.id.C);
        Button t_genhao=(Button) findViewById(R.id.genhao);
        Button t_quyu=(Button) findViewById(R.id.quyu);
        Button t_add=(Button) findViewById(R.id.add);

        Button t_num7=(Button) findViewById(R.id.num7);
        Button t_num8=(Button) findViewById(R.id.num8);
        Button t_num9=(Button) findViewById(R.id.num9);
        Button t_sub=(Button) findViewById(R.id.sub);

        Button t_num4=(Button) findViewById(R.id.num4);
        Button t_num5=(Button) findViewById(R.id.num5);
        Button t_num6=(Button) findViewById(R.id.num6);
        Button t_xinghao=(Button) findViewById(R.id.xinghao);

        Button t_num1=(Button) findViewById(R.id.num1);
        Button t_num2=(Button) findViewById(R.id.num2);
        Button t_num3=(Button) findViewById(R.id.num3);
        Button t_chuhao=(Button) findViewById(R.id.chuhao);

        Button t_jiahuojian=(Button) findViewById(R.id.jiahuojian);
        Button t_num0=(Button) findViewById(R.id.num0);
        Button t_dian=(Button) findViewById(R.id.dian);
        Button t_denhao=(Button) findViewById(R.id.denhao);

        calculator=new Calculator();

        View.OnClickListener buttonlistener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String show=((Button)view).getText().toString();
                calculator.process(show);
                textView1.setText(calculator.getSnum1());
                textView_op.setText(calculator.getSop());
                textView2.setText(calculator.getSnum2());
                textView_res.setText(calculator.getSres());
            }
        };
        t_num0.setOnClickListener(buttonlistener);
        t_num1.setOnClickListener(buttonlistener);
        t_num2.setOnClickListener(buttonlistener);
        t_num3.setOnClickListener(buttonlistener);
        t_num4.setOnClickListener(buttonlistener);
        t_num5.setOnClickListener(buttonlistener);
        t_num6.setOnClickListener(buttonlistener);
        t_num7.setOnClickListener(buttonlistener);
        t_num8.setOnClickListener(buttonlistener);
        t_num9.setOnClickListener(buttonlistener);

        t_C.setOnClickListener(buttonlistener);
        t_genhao.setOnClickListener(buttonlistener);
        t_quyu.setOnClickListener(buttonlistener);
        t_jiahuojian.setOnClickListener(buttonlistener);

        t_add.setOnClickListener(buttonlistener);
        t_sub.setOnClickListener(buttonlistener);
        t_xinghao.setOnClickListener(buttonlistener);
        t_chuhao.setOnClickListener(buttonlistener);

        t_dian.setOnClickListener(buttonlistener);
        t_denhao.setOnClickListener(buttonlistener);



    }


Calculator.java

package com.example.calculator;

import static java.math.BigDecimal.ROUND_HALF_UP;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Calculator {
    BigDecimal b1,b2,bres;
    boolean floatflag1,floatflag2;
    int scale;
    String snum1,snum2,sop,sres;
    enum state{state_i1,state_i2,state_init,state_res};
    state s;
    //enum operator{op_add,op_sub,op_mul,op_div,op_none}
    //operator op;

    public Calculator(){
        scale=100;
        floatflag1=false;
        floatflag2=false;
        clear();
    }
    public void process(String show)
    {
        if(show.charAt(0)>='0'&& show.charAt(0)<='9')//输入数字
        {
            switch (s)
            {
                case state_init:
                    if(show.charAt(0)=='0')break;
                    snum1=show;
                    s=state.state_i1;
                    break;
                case state_i1:
                    snum1+=show;
                    break;
                case state_i2:
                    snum2+=show;
                    break;
                case state_res:
                    clear();
                    process(show);
                    break;
            }
        }
        else//输入操作符
        {
            switch (show)
            {
                case "C":
                    clear();
                    break;
                case "+":
                    switch (s)
                    {
                        case state_init: sop=show;s=state.state_i2;
                        break;
                        case state_i1: sop=show;s=state.state_i2;
                        break;
                        case state_i2:
                            if(snum2=="")
                            {
                                sop=show;
                                break;
                            }
                            snum1=getres(2);snum2="";sres="";sop=show;
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            sop=show;snum1=sres;snum2="";sres="";s=state.state_i2;
                            break;
                    }
                    break;
                case "=":
                    switch (s)
                    {
                        case state_i1:
                            if(snum1.charAt(snum1.length()-1)=='.')snum1+="0";
                            sres=snum1;
                            snum1="";
                            snum2="";
                            sop="";
                            s=state.state_res;
                            break;
                        case state_i2:

                            if(snum2=="")break;
                            if(snum2.charAt(snum2.length()-1)=='.')break;
                            sres=getres(2);
                            snum1="";
                            snum2="";
                            sop="";
                            s=state.state_res;
                            break;
                        default:break;
                    }
                    break;
                case "-":
                    switch (s)
                    {
                        case state_init: sop=show;s=state.state_i2;
                            break;
                        case state_i1: sop=show;s=state.state_i2;
                            break;
                        case state_i2:
                            if(snum2=="")
                            {
                                sop=show;
                                break;
                            }
                            snum1=getres(2);snum2="";sres="";sop=show;
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            sop=show;snum1=sres;snum2="";sres="";s=state.state_i2;
                            break;
                    }
                    break;
                case "*":
                    switch (s)
                    {
                        case state_init: sop=show;s=state.state_i2;
                            break;
                        case state_i1: sop=show;s=state.state_i2;
                            break;
                        case state_i2:
                            if(snum2=="")
                            {
                                sop=show;
                                break;
                            }
                            snum1=getres(2);snum2="";sres="";sop=show;
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            sop=show;snum1=sres;snum2="";sres="";s=state.state_i2;
                            break;
                    }
                    break;
                case "/":
                    switch (s)
                    {
                        case state_init: sop=show;s=state.state_i2;
                            break;
                        case state_i1: sop=show;s=state.state_i2;
                            break;
                        case state_i2:
                            if(snum2=="")
                            {
                                sop=show;
                                break;
                            }
                            snum1=getres(2);snum2="";sres="";sop=show;
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            sop=show;snum1=sres;snum2="";sres="";s=state.state_i2;
                            break;
                    }
                    break;
                case "√":
                    switch (s)
                    {
                        case state_init: snum1="";snum2="";sop="";sres="0";s=state.state_res;
                            break;
                        case state_i1: sop=show;sres=getres(1);snum1="";snum2="";sop="";s=state.state_res;
                            break;
                        case state_i2:
                            if(snum2=="")break;
                            String t=snum1;snum1=snum2;
                            String t_op=sop;sop=show;
                            snum2=getres(1);
                            snum1=t;
                            sop=t_op;
                            sres="";
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            sop=show;
                            snum1=sres;
                            sres=getres(1);
                            snum1="";
                            sop="";
                            break;
                    }
                    break;
                case "%":
                    switch (s)
                    {
                        case state_init:
                            break;
                        case state_i1:
                            if(snum1.contains("."))break;
                            sop=show;s=state.state_i2;
                            break;
                        case state_i2:
                            if(snum2=="")
                            {
                                if(!snum1.contains("."))
                                sop=show;
                                break;
                            }
                            snum1=getres(2);snum2="";sres="";sop=show;
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            if(sres.contains("."))break;
                            sop=show;snum1=sres;snum2="";sres="";s=state.state_i2;
                            break;
                    }
                    break;
                case "+/-":
                    switch (s)
                    {
                        case state_init:
                            break;
                        case state_i1:
                            if(snum1.charAt(0)!='-')
                            snum1="-"+snum1;
                            else snum1=snum1.substring(1);
                            break;
                        case state_i2:
                            if(snum2=="")break;
                            if(snum2.charAt(0)!='-')
                                snum2="-"+snum2;
                            else snum2=snum2.substring(1);
                            break;
                        case state_res:
                            if(sres.contains("error"))
                            {
                                clear();
                                break;
                            }
                            if(sres.charAt(0)!='-'&& !sres.equals(0))
                                sres="-"+sres;
                            else sres=sres.substring(1);
                            snum1=sres;
                            snum2="";
                            sres="";
                            sop="";
                            if(snum1.equals("0"))
                                s=state.state_init;
                            else
                            s=state.state_i1;
                            break;
                    }
                    break;
                case ".":
                    switch (s)
                    {
                        case state_init:
                            snum1+=show;
                            s=state.state_i1;
                            break;
                        case state_i1:
                            if(snum1.contains("."))break;
                            else snum1+=show;
                            break;
                        case state_i2:
                            if(snum2.equals("")){
                                if(sop.equals("%"))break;
                                snum2="0.";
                                break;
                            }
                            if(sop.equals("%")||snum2.contains("."))break;
                            else snum2+=show;
                            break;
                        case state_res:
                            break;
                    }
                    break;
            }
        }
    }

    public String getSnum1()
    {
        return  snum1;
    }
    public String getSnum2()
    {
        return snum2;
    }
    public String getSop()
    {
        return sop;
    }
    public String getSres()
    {
        return sres;
    }

    public void clear()
    {
        snum1="0";
        snum2="";
        sop="";
        sres="";
        s=state.state_init;
        b1=BigDecimal.valueOf(0);
        b2=BigDecimal.valueOf(0);
        bres=BigDecimal.valueOf(0);
    }
    public String getres(int flag)
    {
        String t="null";
        if(flag==1)
        {
            b1=new BigDecimal(snum1);
            if(snum1.equals("0"))return t="0";
            if(sop.equals("√"))
            {
                bres=sqrt(b1,scale);
                t=bres.toString();
                return  t;
            }
            else if(sop.equals("+/-"))
            {
                //略了,直接字符串处理
            }
        }
        else if(flag==2)
        {
            b1=new BigDecimal(snum1);
            b2=new BigDecimal(snum2);
            switch (sop)
            {
                case "+":
                    bres=b1.add(b2);
                    t=bres.toString();
                    break;
                case "-":
                    bres=b1.subtract(b2);
                    t=bres.toString();
                    break;
                case "*":
                    bres=b1.multiply(b2);
                    t=bres.toString();
                    break;
                case "/":
                    if(b2.equals(BigDecimal.valueOf(0)))return t="error: / by zero";
                    bres=b1.divide(b2,scale,RoundingMode.HALF_UP);
                    t=bres.toString();
                    break;
                case "%":
                    if(b2.equals(BigDecimal.valueOf(0)))return t="error: / by zero";
                    bres=b1.remainder(b2);
                    t=bres.toString();
                    break;
            }
        }
        return t;
    }


    public static BigDecimal sqrt(BigDecimal value, int scale){         //高精度浮点数开根号
        BigDecimal num2 = BigDecimal.valueOf(2);
        int precision = 100;
        MathContext mc = new MathContext(precision, RoundingMode.HALF_UP);
        BigDecimal deviation = value;
        int cnt = 0;
        while (cnt < precision) {
            deviation = (deviation.add(value.divide(deviation, mc))).divide(num2, mc);
            cnt++;
        }
        deviation = deviation.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return deviation;
    }


}

布局文件:activity_main.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:orientation="vertical">

    <TextView
        android:id="@+id/textview_num1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:text="0"
        android:textAlignment="textEnd"
        android:textColor="#009688"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textview_op"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:text=""
        android:textAlignment="textEnd"
        android:textColor="#009688"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textview_num2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:text=""
        android:textAlignment="textEnd"
        android:textColor="#009688"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textview_res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:text=""
        android:textAlignment="textEnd"
        android:textColor="#009688"
        android:textSize="24sp"
        android:textStyle="bold" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/C"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="C" />

        <Button
            android:id="@+id/genhao"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="√" />

        <Button
            android:id="@+id/quyu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="%" />

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+" />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/num7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7" />

        <Button
            android:id="@+id/num8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="8" />

        <Button
            android:id="@+id/num9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="9" />

        <Button
            android:id="@+id/sub"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="-" />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/num4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4" />

        <Button
            android:id="@+id/num5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:id="@+id/num6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6" />

        <Button
            android:id="@+id/xinghao"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="*" />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/num2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:id="@+id/num3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3" />

        <Button
            android:id="@+id/chuhao"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="/" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/jiahuojian"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+/-" />

        <Button
            android:id="@+id/num0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="0" />

        <Button
            android:id="@+id/dian"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="." />

        <Button
            android:id="@+id/denhao"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="=" />

    </LinearLayout>

</LinearLayout>

到此这篇关于android studio 项目 :UI设计高精度实现简单计算器的文章就介绍到这了,更多相关UI设计高精度实现简单计算器内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

您可能感兴趣的文章:
  • android studio实验: UI设计 ListView及事件响应
  • android studio组件通信:Intend启动Activity接收返回结果

用户评论