计算器实现(加减乘除)

本文详细介绍了如何从零开始手动实现一个计算器,涵盖了加法、减法、乘法和除法的基本运算逻辑,包括计算器的界面布局和计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

计算器的布局:

<?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"
    android:background="#000000"
    tools:context="com.zhku.exp3_1.MainActivity">
    <!-- 输入框-->
    <TextView
        android:id="@+id/input"
        android:text="0"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="#000000"
        android:textSize="30sp"
        android:paddingRight="5dp"
        android:gravity="bottom|right"
        android:background="@drawable/white_bg" />

    <!-- 按钮-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_7"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="7"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_8"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="8"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_9"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="9"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_clear"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="C"
            style="@style/ButtonStyle" />

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_4"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="4"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_5"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="5"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_6"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="6"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_plus"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="+"
            style="@style/ButtonStyle" />

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_1"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="1"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="2"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_3"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="3"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_minus"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="-"
            style="@style/ButtonStyle" />

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_0"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="0"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_equal"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="="
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_divide"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="÷"
            style="@style/ButtonStyle" />
        <Button
            android:id="@+id/btn_multiply"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:text="×"
            style="@style/ButtonStyle" />

    </LinearLayout>

</LinearLayout>
styles.xml(按钮的样式)

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="ButtonBaseStyle">   <!-- 按钮基样式 -->
           <item name="android:layout_width">40dp</item>
           <item name="android:layout_height">50dp</item>
           <item name="android:layout_margin">10dp</item>
           <item name="android:textSize">30sp</item>
    </style>
    <style name="ButtonStyle" parent="ButtonBaseStyle">  
           <!--一般按钮样式,继承按钮基样式 ButtonBaseStyle-->
           <item name="android:background">@drawable/white_bg</item>
           <item name="android:layout_weight">1</item>
    </style>
</resources>
White_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
      <!-- 边缘为圆形半径(圆角矩形) -->
        <corners android:radius="5dp" />
        <!-- 填充色 -->
        <solid android:color="#ffffff" />
</shape>
MainActivity.xml

package com.exam.exp3_1;

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


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_0;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;
    private Button btn_clear;
    private Button btn_plus;
    private Button btn_divide;
    private Button btn_multiply;
    private Button btn_minus;
    private Button btn_equal;
    private TextView input;
    private boolean clear_flag;   //清空标志

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_multiply = (Button) findViewById(R.id.btn_multiply);
        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_divide = (Button) findViewById(R.id.btn_divide);
        btn_equal = (Button) findViewById(R.id.btn_equal);
        input = (TextView) findViewById(R.id.input);

        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_equal.setOnClickListener(this);
        input.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str = input.getText().toString();  //获取按钮的显示字符
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:

                if (clear_flag) {
                    clear_flag = false;
                    str = "";
                    input.setText("");
                }
                input.setText(str + ((TextView) v).getText());
                break;

            case R.id.btn_plus:
            case R.id.btn_multiply:
            case R.id.btn_minus:
            case R.id.btn_divide:
                if (clear_flag) {
                    clear_flag = false;
                    str = "";
                    input.setText("");
                }
                input.setText(str + " " + ((TextView) v).getText() + " ");
                break;

            case R.id.btn_clear:
                clear_flag = false;
                input.setText("");
                break;

            case R.id.btn_equal:
                getResult();
                clear_flag = true;
                break;
        }
    }

    private void getResult() {
        String exp = input.getText().toString();
        Log.d("获得的字符串表达式",exp);
        if (exp == null||exp.equals("")) {
            return ;
        }
        if (!exp.contains(" ")) {
            return;
        }
        clear_flag = true;
        double result = 0;
        String s1 = exp.substring(0, exp.indexOf(" "));  //第一个字符串
        Log.d("第一个操作数分割的字符",s1);
        String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
        Log.i("运算操作符",op);
        String s2 = exp.substring(exp.indexOf(" ") + 3); //第二个
        Log.d("第二个操作数分割的字符",s2);
        Log.i("运算开始","进入乘法");
        if (!s1.equals("") && !s2.equals("")) {
            double d1 = Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            if (op.equals("+")) {
                result = d1 + d2;
                input.setText(result+"");
            } else if (op.equals("-")) {
                result = d1 - d2;
                input.setText(result+"");
            } else if (op.equals("×")) {
                result = d1 * d2;
                input.setText(result + "");
            } else if (op.equals("÷")) {
                if (d2 == 0) {
                    result = 0;
                    input.setText(result+"");
                } else {
                    result = d1 / d2;
                    input.setText(result+"");
                }
            }
        }
        Log.i("最后结果",result+"");
    }
}







java编写的计算器,能实现加减乘除。 //***该梦幻计算器与Windows附件自带计算器的标准版功能、界面相仿***// //******但还不支持键盘操作,如有需要,下一次等级将满足你**********// import java.awt.*; import java.lang.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.Component.*; import java.awt.color.*; import java.text.DecimalFormat; //*****************************************************************************// //*****************************************************************************// public class Calculator implements ActionListener //导入动作监听接口 { //******************************// //*****设计计算器界面的单位*****// JFrame frame; //定义框架 JTextField textAnswer; //定义输出显示框 JPanel panel, panel1, panel2, panel3;//定义面板作为子容器,从而达到要求的界面效果 JMenuBar mainMenu; JTextField textMemory; JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状 JButton buttonBk, buttonCe, buttonC; JButton button[]; JButton buttonMC, buttonMR, buttonMS, buttonMAdd; JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,buttonDiv, buttonMod; JButton buttonSqrt, buttonDao, buttonEqual; JMenu editMenu, viewMenu, helpMenu; JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal,myCollege; DecimalFormat df; //设置数据输出精度 boolean clickable; //控制当前能否按键 double memoryd; //使用内存中存储的数字 int memoryi; double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd) short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算 boolean clear = false; String copy; //做复制用 JTextArea help; //帮助 JScrollPane scrollHelp; static boolean One =true; //******************// //*****构造函数开始*****// public Calculator() { clickable = true; answerd = 0; frame = new JFrame("梦幻计算器--SWE08008"); frame.setResizable(false);//不允许改变计算器的大小 frame.setLocation(250,150);//设置计算器的起始位置 frame.setBackground(Color.YELLOW); df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值