【Android】编写一个简易计算机
16lz
2021-12-20
1.界面
2.源代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> //第一列 //第二行 //第三行
MainActivity.java
package com.example.tanjy.ex2_9_1;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.util.regex.Pattern;public class MainActivity extends AppCompatActivity { private Button bt_num0,bt_num1,bt_num2,bt_num3,bt_num4,bt_num5,bt_num6,bt_num7,bt_num8,bt_num9,bt_clear,bt_div,bt_mult,bt_minus,bt_plus,bt_equal,bt_dot; private TextView et_calc; private String progress,currentSymbol; //currentSymbol用来记录当前使用的运算符 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViewAndListener(); } private void initViewAndListener() { bt_num0=(Button)findViewById(R.id.bt_num0); bt_num0.setOnClickListener(new clickNum()); bt_num1=(Button)findViewById(R.id.bt_num1); bt_num1.setOnClickListener(new clickNum()); bt_num2=(Button)findViewById(R.id.bt_num2); bt_num2.setOnClickListener(new clickNum()); bt_num3=(Button)findViewById(R.id.bt_num3); bt_num3.setOnClickListener(new clickNum()); bt_num4=(Button)findViewById(R.id.bt_num4); bt_num4.setOnClickListener(new clickNum()); bt_num5=(Button)findViewById(R.id.bt_num5); bt_num5.setOnClickListener(new clickNum()); bt_num6=(Button)findViewById(R.id.bt_num6); bt_num6.setOnClickListener(new clickNum()); bt_num7=(Button)findViewById(R.id.bt_num7); bt_num7.setOnClickListener(new clickNum()); bt_num8=(Button)findViewById(R.id.bt_num8); bt_num8.setOnClickListener(new clickNum()); bt_num9=(Button)findViewById(R.id.bt_num9); bt_num9.setOnClickListener(new clickNum()); bt_clear=(Button)findViewById(R.id.bt_clear); bt_clear.setOnClickListener(new clickOther()); bt_div=(Button)findViewById(R.id.bt_div); bt_div.setOnClickListener(new clickOther()); bt_mult=(Button)findViewById(R.id.bt_mult); bt_mult.setOnClickListener(new clickOther()); bt_minus=(Button)findViewById(R.id.bt_minus); bt_minus.setOnClickListener(new clickOther()); bt_plus=(Button)findViewById(R.id.bt_plus); bt_plus.setOnClickListener(new clickOther()); bt_equal=(Button)findViewById(R.id.bt_equal); bt_equal.setOnClickListener(new clickOther()); bt_dot=(Button)findViewById(R.id.bt_dot); bt_dot.setOnClickListener(new clickOther()); et_calc=(TextView)findViewById(R.id.et_calc); } //数字按钮监听器 class clickNum implements View.OnClickListener{ public void onClick(View v) { Button num = (Button) v; //获取用户点击的按钮对象 String clickNum = num.getText().toString(); //获取用户点击的数字 progress = et_calc.getText().toString(); //获取结果的值并赋值给progress if(v == bt_num0){ //按0 progress = dealRes(progress,clickNum); }else { //按1-9 progress = dealRes(progress,clickNum); } et_calc.setText(progress); } } //功能按钮监听器,可以加“.”的情况 class clickOther implements View.OnClickListener { public void onClick(View v) { Button num = (Button) v; String reg = "^(\\d+)|^(\\d+\\+|-|/|\\*\\d+)|^(\\d+\\.\\d+\\+|-|/|\\*\\d+)"; String symbol = num.getText().toString(); //获取用户点击的符号 progress = et_calc.getText().toString(); //获取结果的值并赋值给progress //按清除按钮 if (v == bt_clear) { progress = "0"; currentSymbol = ""; //按等号按钮 } else if (v == bt_equal) { String[] arr = progress.split("\\+|-|/|\\*"); if (progress.split("\\+|-|/|\\*").length != 1) { progress = splitStr(progress, currentSymbol); } //按“.”按钮 } else if (v == bt_dot) { //如果不符合可以加“.”的情况,则不变,否则加上“.” progress = !Pattern.matches(reg, progress) ? progress : dealRes(progress, symbol); //按加减乘除按钮 } else { //如果式子中存在“+*/”,进行运算;如果存在“-”且不是首位,进行运算 Boolean a = progress.indexOf('+') != -1 || progress.indexOf('*') != -1 || progress.indexOf('/') != -1; Boolean b = progress.indexOf('-') != -1; Boolean c = progress.lastIndexOf('-') != 0; if (a) { progress = splitStr(progress, currentSymbol); currentSymbol = num.getText().toString(); progress = progress + currentSymbol; } else if (b && c) { progress = splitStr(progress, currentSymbol); currentSymbol = num.getText().toString(); progress = progress + currentSymbol; } else { currentSymbol = num.getText().toString(); progress = progress + currentSymbol; } } et_calc.setText(progress); } } //分割字符串,进行计算,并返回计算结果 public String splitStr (String str , String calSymbol) { String []strArray = new String[20]; strArray = str.split("\\+|-|/|\\*"); //使用+-*/对式子进行分割 Double res = 0.0; switch (calSymbol) { //根据运算符号选取操作 case "+" : { if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) + Double.parseDouble(strArray[2]); else res = Double.parseDouble(strArray[0]) + Double.parseDouble(strArray[1]); currentSymbol = ""; break; } case "-" : { if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) - Double.parseDouble(strArray[2]); else res = str.indexOf('-') == 0 ? Double.parseDouble(str) : Double.parseDouble(strArray[0]) - Double.parseDouble(strArray[1]); currentSymbol = ""; break; } case "*" : { if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) * Double.parseDouble(strArray[2]); else res = Double.parseDouble(strArray[0]) * Double.parseDouble(strArray[1]); currentSymbol = ""; break; } case "/" : { if (strArray.length > 2) res = (- Double.parseDouble(strArray[1])) / Double.parseDouble(strArray[2]); else res = Double.parseDouble(strArray[0]) / Double.parseDouble(strArray[1]); currentSymbol = ""; break; } } return String.valueOf(res); } //判读字符串的组成,并返回结果 public String dealRes (String str,String clickSymbol){ String reValue = ""; //如果字符串长度为0,直接返回点击的字符 if (str.length() == 0) { reValue = clickSymbol; //如果字符串长度为为1,且等于0,且字符不为“.”,则返回点击的字符 } else if(str.length() == 1 && Integer.parseInt(str) == 0 && clickSymbol != bt_dot.getText().toString()) { reValue = clickSymbol ; //其他情况,直接拼接点击的字符字符 } else { reValue = str + clickSymbol; } return reValue; }}
3.运行结果
按2*3
再按“=”按钮
说明:本文参考链接:https://blog.csdn.net/qq_40121804/article/details/88607188
更多相关文章
- android 时间对话框
- beforeTextChanged,TextChanged,afterTextChanged的使用Android
- Android设置RadioButton在文字的右边
- android 根据TextView宽度 自动缩小字体大小 以适应其宽度
- Win8风格的按钮、ToggleButton、SeekBar
- android 滑动加载数据
- android 对话框
- android sqlite 增删查 demo
- Android(安卓)BaseAdapter如何获得每一项并添加按钮事件