Android开发实现计算器

首先layout布局配置

计算器名称组件(可省略):

<TextView
       android:id="@+id/title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="计算器"
       android:textSize="30sp"
       android:textColor="#912F2F"
       android:layout_gravity="center"/>

显示过程和结果的文本框:

<TextView
       android:id="@+id/resultview"
       android:layout_width="match_parent"
       android:layout_height="150dp"
       android:background="#0A0909"
       android:layout_marginLeft="0dp"/>

各种Button组件:

 <!--第一行-->
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_weight="1"
       android:background="#2196F3">
  <!--回滚-->
      <Button
          android:id="@+id/CE_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="CE"
          android:textSize="20sp" />
 <!--符号“/”-->
      <Button
          android:id="@+id/divid_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="/"
          android:textSize="20sp" />
           <!--符号“*”->
      <Button
          android:id="@+id/multi_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="*"
          android:textSize="20sp" />
          <!--符号“C”(清空所有显示框中的内容)-->
      <Button
          android:id="@+id/C_btn"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="C"
          android:textSize="20sp" />
   </LinearLayout>
   <!--其他四行"7\8\9\+","4\5\6\-","1\2\3\√",“0\.\=”同上省略,-->

实例化各个组件对象

  private TextView resultview;
    private Button CE_btn,divid_btn,multi_btn,C_btn;
    private Button seven_btn,eight_btn,nine_btn,plus_btn;
    private Button four_btn,five_btn,six_btn,sub_btn;
    private Button one_btn,two_btn,three_btn,sqr_btn;
    private Button zero_btn,dot_btn,equal_btn;
    private String content="";
    private char[] ch;
    ...
    one_btn = findViewById(R.id.one_btn);
        two_btn = findViewById(R.id.two_btn);
        three_btn = findViewById(R.id.three_btn);
        four_btn = findViewById(R.id.four_btn);
        five_btn = findViewById(R.id.five_btn);
        ...
          dot_btn = findViewById(R.id.dot_btn);
        CE_btn = findViewById(R.id.CE_btn);
        sqr_btn = findViewById(R.id.sqr_btn);
    //注册监听器
     one_btn.setOnClickListener(l);
        two_btn.setOnClickListener(l);
        three_btn.setOnClickListener(l);
        ...
         dot_btn.setOnClickListener(l);
        CE_btn.setOnClickListener(l);
        sqr_btn.setOnClickListener(l);
View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.one_btn:
                    numberSqrt(content,one_btn);
                    content = resultview.getText().toString();
                    resultview.setText(content);
                    break;
                case R.id.two_btn:
                    numberSqrt(content,two_btn);
                    content = resultview.getText().toString();
                    resultview.setText(content);
                    break;
                    ...
                                  case R.id.C_btn:
                    content = "";
                    resultview.setText(content);
                    break;
                default:
                    break;
            }
        }
    };

实现计算器的主要方法递归运算(这个方法并不好一些功能几乎不可能实现):


//通过递归调用实现+-*/√运算
    public  double calculate(String content) {
        int index = content.indexOf("+");
        if (index != -1) {
            return calculate(content.substring(0, index)) + calculate(content.substring(index + 1));
        }
        index = content.indexOf("-");
        if (index != -1){
            if(index == 0)
                return 0-calculate(content.substring(index+1));
            return calculate(content.substring(0, index)) - calculate(content.substring(index+1));
        }
        index = content.indexOf("*");
        if (index != -1){
            return calculate(content.substring(0,index)) * calculate(content.substring(index+1));
        }
        index = content.indexOf("/");
        if (index != -1){
                return calculate(content.substring(0,index)) / calculate(content.substring((index + 1)));
        }
        index = content.indexOf("√");
        if (index !=-1){
            return Math.sqrt(calculate(content.substring(0,index)));
        }
           return  parseDouble(content);
    }

基本过程就是这样,另外,没有实现直接加、减、乘以,除以负数的情况,除以0后会显示Infinity等。
链接:https://pan.baidu.com/s/1LPwLd5T8a8YwbNZiuaPgew
提取码:lmgw

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值