Android Studio62进制计算器代码

xml代码

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/inputField1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter first number"/>

<EditText
    android:id="@+id/inputField2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter second number (for operations that require two inputs)"/>

<TextView
    android:id="@+id/resultTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Result will be shown here"/>

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

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

<Button
    android:id="@+id/multiplyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="×"/>

<Button
    android:id="@+id/divideButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="÷"/>

<Button
    android:id="@+id/sqrtButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="平方根"/>

<Button
    android:id="@+id/powerButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="幂运算"/>

<Button
    android:id="@+id/factorialButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="阶乘"/>

java代码

package com.example.a62jzjsq;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.math.BigDecimal;
import java.math.BigInteger;

public class MainActivity extends AppCompatActivity {

private EditText inputField1;
private EditText inputField2;
private TextView resultTextView;
private Button addButton;
private Button subtractButton;
private Button multiplyButton;
private Button divideButton;
private Button sqrtButton;
private Button powerButton;
private Button factorialButton;

private static final String BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

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

    inputField1 = findViewById(R.id.inputField1);
    inputField2 = findViewById(R.id.inputField2);
    resultTextView = findViewById(R.id.resultTextView);
    addButton = findViewById(R.id.addButton);
    subtractButton = findViewById(R.id.subtractButton);
    multiplyButton = findViewById(R.id.multiplyButton);
    divideButton = findViewById(R.id.divideButton);
    sqrtButton = findViewById(R.id.sqrtButton);
    powerButton = findViewById(R.id.powerButton);
    factorialButton = findViewById(R.id.factorialButton);

    addButton.setOnClickListener(v -> performOperation("add"));
    subtractButton.setOnClickListener(v -> performOperation("subtract"));
    multiplyButton.setOnClickListener(v -> performOperation("multiply"));
    divideButton.setOnClickListener(v -> performOperation("divide"));
    sqrtButton.setOnClickListener(v -> performOperation("sqrt"));
    powerButton.setOnClickListener(v -> performOperation("power"));
    factorialButton.setOnClickListener(v -> performOperation("factorial"));
}

private void performOperation(String operation) {
    String input1 = inputField1.getText().toString();
    String input2 = inputField2.getText().toString();


    try {
        BigDecimal result;
        switch (operation) {
            case "add":
                result = convertToDecimal(input1).add(convertToDecimal(input2));


                break;
            case "subtract":
                result = convertToDecimal(input1).subtract(convertToDecimal(input2));
                break;
            case "multiply":
                result = convertToDecimal(input1).multiply(convertToDecimal(input2));
                break;
            case "divide":
                result = convertToDecimal(input1).divide(convertToDecimal(input2), 10, BigDecimal.ROUND_HALF_UP);
                break;
            case "sqrt":
                result = BigDecimal.valueOf(Math.sqrt(convertToDecimal(input1).doubleValue()));
                break;
            case "power":
                BigDecimal base = convertToDecimal(input1);
                BigDecimal exponent = convertToDecimal(input2);
                result = BigDecimal.valueOf(Math.pow(base.doubleValue(), exponent.doubleValue()));
                break;
            case "factorial":
                BigInteger number = convertToDecimal(input1).toBigInteger();
                result = factorial(number);
                break;
            default:
                resultTextView.setText("Invalid operation");
                return;
        }

        resultTextView.setText(convertToBase62(result));


    } catch (NumberFormatException e) {
        resultTextView.setText("Invalid input");
    }
}

private BigDecimal convertToDecimal(String base62Number) {
    BigInteger decimalValue = BigInteger.ZERO;
    int power = 0;
    for (int i = base62Number.length() - 1; i >= 0; i--) {
        int charValue = BASE62_CHARS.indexOf(base62Number.charAt(i));
        decimalValue = decimalValue.add(BigInteger.valueOf(charValue).multiply(BigInteger.valueOf(62).pow(power)));
        power++;
    }
    return new BigDecimal(decimalValue);
}

private String convertToBase62(BigDecimal decimalNumber) {
    if (decimalNumber.equals(BigDecimal.ZERO)) {
        return "0";
    }

    StringBuilder base62Number = new StringBuilder();
    BigInteger number = decimalNumber.toBigInteger();
    while (number.compareTo(BigInteger.ZERO) > 0) {
        BigInteger remainder = number.mod(BigInteger.valueOf(62));
        base62Number.insert(0, BASE62_CHARS.charAt(remainder.intValue()));
        number = number.divide(BigInteger.valueOf(62));
    }
    return base62Number.toString();
}

private BigDecimal factorial(BigInteger number) {
    BigDecimal result = BigDecimal.ONE;
    for (BigInteger i = BigInteger.ONE; i.compareTo(number) <= 0; i = i.add(BigInteger.ONE)) {
        result = result.multiply(new BigDecimal(i));
    }
    return result;
}

}

以下是一个简单的 Android Studio 项目,可以将十进制数转换为二进制、八进制和十六进制: 1. 首先,在 Android Studio 中创建一个新项目,并在 MainActivity.java 文件中添加以下代码: ```java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button btnConvert; EditText etDecimal; TextView tvBinary, tvOctal, tvHexadecimal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnConvert = findViewById(R.id.btnConvert); etDecimal = findViewById(R.id.etDecimal); tvBinary = findViewById(R.id.tvBinary); tvOctal = findViewById(R.id.tvOctal); tvHexadecimal = findViewById(R.id.tvHexadecimal); btnConvert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int decimal = Integer.parseInt(etDecimal.getText().toString()); String binary = Integer.toBinaryString(decimal); String octal = Integer.toOctalString(decimal); String hexadecimal = Integer.toHexString(decimal).toUpperCase(); tvBinary.setText(binary); tvOctal.setText(octal); tvHexadecimal.setText(hexadecimal); } }); } } ``` 2. 接下来,在 activity_main.xml 文件中添加以下代码: ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/etDecimal" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter decimal number" android:inputType="number" /> <Button android:id="@+id/btnConvert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/etDecimal" android:text="Convert" /> <TextView android:id="@+id/tvBinary" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/btnConvert" android:text="Binary:" /> <TextView android:id="@+id/tvOctal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tvBinary" android:text="Octal:" /> <TextView android:id="@+id/tvHexadecimal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tvOctal" android:text="Hexadecimal:" /> </RelativeLayout> ``` 3. 运行应用程序,并输入十进制数,然后点击“Convert”按钮即可看到转换后的二进制、八进制和十六进制数。 注意:此应用程序只能处理小于 2^31 的十进制数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值