Android带小数运算的计算器

一个带小数操作的计算器

本人大二,一个Android初学者,看完书之后迫不及待的想写一个属于自己的小程序,想了几天写了个计算器,其中几天时间想了该写什么好,其余的时间就在想这个计算器代码该具体怎么实现…确实逻辑都是自己想的,有更好想法的大佬可以评论交流下。

设计的这个计算器可以实现double范围内100位数的加减乘除操作,包括小数操作,下面是运行界面。
在这里插入图片描述
话不多说,直接上代码,本计算器并不复杂,只需要两个文件即可。
并不复杂,只需要两个文件即可
下面是MainActivity代码

package example.com.calculator1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    int i,j,p;
    double re;
    String for1="",for2="";
    Button temp;
    double[] op=new double[100];
    int[] sym=new int[100];
    int[] buttons1 = new int[]{
            R.id.Button00,R.id.Button01,R.id.Button02, R.id.Button03, R.id.Button04,
            R.id.Button05, R.id.Button06, R.id.Button07,R.id.Button08, R.id.Button09
    };
    TextView tvScreem1;
    TextView tvScreem2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvScreem1=(TextView)findViewById(R.id.tvScreem1);
        tvScreem2=(TextView)findViewById(R.id.tvScreem2);

        for(int k=0;k<buttons1.length;k++){
            temp = (Button)findViewById(buttons1[k]);
            temp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch(v.getId()){
                        case R.id.Button00:
                            if(p==0){setNumble1(0);break;}
                            else{setNumble2(0);break;}
                        case R.id.Button01:
                            if(p==0){setNumble1(1);break;}
                            else{setNumble2(1);break;}
                        case R.id.Button02:
                            if(p==0){setNumble1(2);break;}
                            else{setNumble2(2);break;}
                        case R.id.Button03:
                            if(p==0){setNumble1(3);break;}
                            else{setNumble2(3);break;}
                        case R.id.Button04:
                            if(p==0){setNumble1(4);break;}
                            else{setNumble2(4);break;}
                        case R.id.Button05:
                            if(p==0){setNumble1(5);break;}
                            else{setNumble2(5);break;}
                        case R.id.Button06:
                            if(p==0){setNumble1(6);break;}
                            else{setNumble2(6);break;}
                        case R.id.Button07:
                            if(p==0){setNumble1(7);break;}
                            else{setNumble2(7);break;}
                        case R.id.Button08:
                            if(p==0){setNumble1(8);break;}
                            else{setNumble2(8);break;}
                        case R.id.Button09:
                            if(p==0){setNumble1(9);break;}
                            else{setNumble2(9);break;}
                        default:break;
                    }
                }
            });
        }

        Button Jia=(Button)findViewById(R.id.ButtonJia);
        Jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSymbol(1);
            }
        });
        Button Jian=(Button)findViewById(R.id.ButtonJian);
        Jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSymbol(2);
            }
        });
        Button Cheng=(Button)findViewById(R.id.ButtonCheng);
        Cheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSymbol(3);
            }
        });
        Button Chu=(Button)findViewById(R.id.ButtonChu);
        Chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSymbol(4);
            }
        });

        Button C=(Button)findViewById(R.id.ButtonC);
        C.setOnClickListener(new View.OnClickListener() {//清除
            @Override
            public void onClick(View v) {
                clear();
            }
        });
        Button Point=(Button)findViewById(R.id.ButtonPoint);//小数点
        Point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                p=-1;
                for1=for1+".";
                tvScreem1.setText(for1);
            }
        });
        Button Re=(Button)findViewById(R.id.ButtonResult);//上次计算的结果数
        Re.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                op[i]=re;
                for1=for1+String.valueOf(re);
                tvScreem1.setText(for1);
            }
        });

        Button Equal=(Button)findViewById(R.id.ButtonDeng);//等于,输出结果
        Equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(j=0;sym[j]!=0;j++){
                    switch(sym[j]){
                        case 3:op[j+1]=op[j]*op[j+1];op[j]=0;sym[j]=j>0?sym[j-1]:1;break;
                        case 4:if(op[j+1]==0) {
                            Toast.makeText(MainActivity.this, "计算错误", Toast.LENGTH_SHORT).show();
                            j=98;sym[99]=0;
                            break;
                        }else{
                            op[j+1]=op[j]/op[j+1];op[j]=0;sym[j]=j>0?sym[j-1]:1;break;
                        }
                        default:break;
                    }
                }if(j!=99){
                    re=op[0];
                    for(j=0;sym[j]!=0;j++){
                        if(sym[j]==1){
                            re=re+op[j+1];
                        }else{
                            re=re-op[j+1];
                        }
                    }if(re%1==0){
                        for2=String.valueOf(re);
                        tvScreem2.setText(for2);
                        Toast.makeText(MainActivity.this, "结果是"+for2, Toast.LENGTH_SHORT).show();
                    }else{
                        for2=String.valueOf(re);
                        tvScreem2.setText(for2);
                        Toast.makeText(MainActivity.this, "结果是"+re, Toast.LENGTH_SHORT).show();
                    }
                    for(j=0;j<=i;j++){
                        op[j]=0;sym[j]=0;
                    }
                    i=0;j=0;p=0;
                    for1=for2="";
                }
            }
        });
    }
    public void setNumble1(int n){//第一个操作数
        op[i]=op[i]*10+n;
        for1=for1+String.valueOf(n);
        tvScreem1.setText(for1);
    }
    public void setNumble2(int n){//第二个操作数
        op[i]=op[i]+n*Math.pow(10,p);p--;
        for1=for1+String.valueOf(n);
        tvScreem1.setText(for1);
    }
    public void setSymbol(int n){//标志位,判断何种操作
        sym[i]=n;i++;p=0;
        switch (n){
            case 1:for1=for1+"+";
                tvScreem1.setText(for1);break;
            case 2:for1=for1+"-";
                tvScreem1.setText(for1);break;
            case 3:for1=for1+"×";
                tvScreem1.setText(for1);break;
            case 4:for1=for1+"/";
                tvScreem1.setText(for1);break;
        }
    }
    public void clear(){//清除
        re=0;
        for(j=0;j<=i;j++){
            op[j]=0;sym[j]=0;
        }
        i=0;j=0;p=0;
        for1="";for2="";
        tvScreem1.setText("");
        tvScreem2.setText("");
        //清屏
    }
}

下面是activity_main.xml 文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvScreem1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:gravity="right"
        android:maxLines="1"
        android:ellipsize="start"
        android:background="#4682B4"
        android:textColor="#FFD700"/>
    <TextView
        android:id="@+id/tvScreem2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:gravity="right"
        android:background="#FFFF00"
        android:textColor="#FF0000"/>
    <!-- C =-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonC"
            android:text="C"
            android:textSize="25dp"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/ButtonDeng"
            android:text="="
            android:textSize="25dp"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"/>
    </LinearLayout>

    <!-- 7 8 9 / -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button07"
            android:text="7"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button08"
            android:text="8"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button09"
            android:text="9"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/ButtonChu"
            android:text="/"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1" />
    </LinearLayout>

    <!-- 4 5 6 × -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button04"
            android:text="4"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button05"
            android:text="5"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button06"
            android:text="6"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/ButtonCheng"
            android:text="×"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
    </LinearLayout>

    <!-- 1 2 3 - -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button01"
            android:text="1"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button02"
            android:text="2"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button03"
            android:text="3"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/ButtonJian"
            android:text="-"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
    </LinearLayout>

    <!-- .0 result + -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonPoint"
            android:text="."
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Button00"
            android:text="0"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/ButtonResult"
            android:text="result"
            android:textSize="22dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:textAllCaps="false"/>
        <Button
            android:id="@+id/ButtonJia"
            android:text="+"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

另外别忘了在AndroidManifest.xml里注册主类

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.com.calculator1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在最后附上apk文件,大家可以下载学习!
Android带小数计算器

发布了11 篇原创文章 · 获赞 7 · 访问量 1195

猜你喜欢

转载自blog.csdn.net/leangx86/article/details/98110232