设计一个安卓计算器,实现基本加减乘除,开方,平方,倒数运算,阶乘运算,求任意底的对数运算,任意数的N次方运算,正弦,余弦,正切,余切运算等功能
时间: 2024-05-17 17:15:03 浏览: 169
设计一个安卓计算器需要考虑到UI设计和后台计算逻辑设计两部分。下面是一个简单的设计:
### UI设计
- 使用grid布局,将按钮排列成一个二维数组,方便管理
- 使用TextView作为显示面板,显示用户输入的数字和计算结果
- 使用button作为用户输入的按钮,包括数字、运算符和其他功能键,如清空键、等于键等
- 对于一些需要输入多个数字的运算,如任意数的N次方,可以使用弹出框来输入数字
### 后台计算逻辑设计
- 使用栈来实现基本四则运算,运算符栈和数字栈分别存储运算符和数字
- 实现开方、平方、倒数、阶乘等数学函数,使用Java自带的Math库
- 实现任意底的对数运算,使用换底公式 loga(b) = logx(b) / logx(a),其中x为任意底
- 实现任意数的N次方运算,使用Math.pow()函数
- 实现正弦、余弦、正切、余切运算,使用Java自带的Math库
综上所述,安卓计算器的设计需要涉及到UI设计和后台计算逻辑设计两个部分,UI设计需要考虑使用grid布局、TextView和button实现,后台计算逻辑设计需要实现基本四则运算、数学函数和三角函数等功能。
相关问题
设计一个安卓计算器,实现基本加减乘除,开方,平方,倒数运算,阶乘运算,求任意底的对数运算,任意数的N次方运算,正弦,余弦,正切,余切运算实现代码
好的,下面是一个简单的安卓计算器的实现代码,包括基本的加减乘除、开方、平方、倒数、阶乘、对数、幂、正弦、余弦、正切和余切等运算:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView display;
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private Button btnAdd, btnSub, btnMul, btnDiv, btnEqu, btnDot, btnClr;
private Button btnSqrt, btnSqr, btnRec, btnFac, btnLog, btnPow, btnSin, btnCos, btnTan, btnCot;
private double num1, num2, result;
private String operator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
btn0 = findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
btnAdd = findViewById(R.id.btnAdd);
btnSub = findViewById(R.id.btnSub);
btnMul = findViewById(R.id.btnMul);
btnDiv = findViewById(R.id.btnDiv);
btnEqu = findViewById(R.id.btnEqu);
btnDot = findViewById(R.id.btnDot);
btnClr = findViewById(R.id.btnClr);
btnSqrt = findViewById(R.id.btnSqrt);
btnSqr = findViewById(R.id.btnSqr);
btnRec = findViewById(R.id.btnRec);
btnFac = findViewById(R.id.btnFac);
btnLog = findViewById(R.id.btnLog);
btnPow = findViewById(R.id.btnPow);
btnSin = findViewById(R.id.btnSin);
btnCos = findViewById(R.id.btnCos);
btnTan = findViewById(R.id.btnTan);
btnCot = findViewById(R.id.btnCot);
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnEqu.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnClr.setOnClickListener(this);
btnSqrt.setOnClickListener(this);
btnSqr.setOnClickListener(this);
btnRec.setOnClickListener(this);
btnFac.setOnClickListener(this);
btnLog.setOnClickListener(this);
btnPow.setOnClickListener(this);
btnSin.setOnClickListener(this);
btnCos.setOnClickListener(this);
btnTan.setOnClickListener(this);
btnCot.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str = display.getText().toString();
switch (v.getId()) {
case R.id.btn0:
display.setText(str + "0");
break;
case R.id.btn1:
display.setText(str + "1");
break;
case R.id.btn2:
display.setText(str + "2");
break;
case R.id.btn3:
display.setText(str + "3");
break;
case R.id.btn4:
display.setText(str + "4");
break;
case R.id.btn5:
display.setText(str + "5");
break;
case R.id.btn6:
display.setText(str + "6");
break;
case R.id.btn7:
display.setText(str + "7");
break;
case R.id.btn8:
display.setText(str + "8");
break;
case R.id.btn9:
display.setText(str + "9");
break;
case R.id.btnAdd:
num1 = Double.parseDouble(str);
operator = "+";
display.setText("");
break;
case R.id.btnSub:
num1 = Double.parseDouble(str);
operator = "-";
display.setText("");
break;
case R.id.btnMul:
num1 = Double.parseDouble(str);
operator = "*";
display.setText("");
break;
case R.id.btnDiv:
num1 = Double.parseDouble(str);
operator = "/";
display.setText("");
break;
case R.id.btnEqu:
num2 = Double.parseDouble(str);
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
display.setText(String.valueOf(result));
break;
case R.id.btnDot:
if (!str.contains(".")) {
display.setText(str + ".");
}
break;
case R.id.btnClr:
display.setText("");
break;
case R.id.btnSqrt:
num1 = Double.parseDouble(str);
result = Math.sqrt(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnSqr:
num1 = Double.parseDouble(str);
result = num1 * num1;
display.setText(String.valueOf(result));
break;
case R.id.btnRec:
num1 = Double.parseDouble(str);
result = 1 / num1;
display.setText(String.valueOf(result));
break;
case R.id.btnFac:
num1 = Double.parseDouble(str);
result = factorial(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnLog:
num1 = Double.parseDouble(str);
result = Math.log10(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnPow:
num1 = Double.parseDouble(str);
operator = "^";
display.setText("");
break;
case R.id.btnSin:
num1 = Double.parseDouble(str);
result = Math.sin(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnCos:
num1 = Double.parseDouble(str);
result = Math.cos(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnTan:
num1 = Double.parseDouble(str);
result = Math.tan(num1);
display.setText(String.valueOf(result));
break;
case R.id.btnCot:
num1 = Double.parseDouble(str);
result = 1 / Math.tan(num1);
display.setText(String.valueOf(result));
break;
}
}
private double factorial(double n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}
```
需要在布局文件中添加对应的控件,并设置相应的ID和属性。上面的代码实现了基本的计算器功能,可以根据需要进行扩展和优化。
设计一个安卓计算器要求实现功能 1实现基本的加减乘除运算 2实现开方、平方,倒数运算、阶乘 运算、求任意底的对数运算、任 数的N次方运算 3正弦,余弦,正切,余切运算的源程序代码
以下是一个实现上述功能的安卓计算器的源代码示例:
```
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CalculatorActivity extends AppCompatActivity {
private TextView inputTextView;
private TextView resultTextView;
private Button clearButton;
private Button deleteButton;
private Button squareButton;
private Button sqrtButton;
private Button reciprocalButton;
private Button factorialButton;
private Button logButton;
private Button sinButton;
private Button cosButton;
private Button tanButton;
private Button cotButton;
private Button addBtn;
private Button subtractBtn;
private Button multiplyBtn;
private Button divideBtn;
private Button equalBtn;
private Button decimalBtn;
private Button zeroBtn;
private Button oneBtn;
private Button twoBtn;
private Button threeBtn;
private Button fourBtn;
private Button fiveBtn;
private Button sixBtn;
private Button sevenBtn;
private Button eightBtn;
private Button nineBtn;
private StringBuilder inputStr = new StringBuilder();
private double result = 0.0;
private boolean isDecimal = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
inputTextView = (TextView) findViewById(R.id.input_text_view);
resultTextView = (TextView) findViewById(R.id.result_text_view);
clearButton = (Button) findViewById(R.id.clear_button);
deleteButton = (Button) findViewById(R.id.delete_button);
squareButton = (Button) findViewById(R.id.square_button);
sqrtButton = (Button) findViewById(R.id.sqrt_button);
reciprocalButton = (Button) findViewById(R.id.reciprocal_button);
factorialButton = (Button) findViewById(R.id.factorial_button);
logButton = (Button) findViewById(R.id.log_button);
sinButton = (Button) findViewById(R.id.sin_button);
cosButton = (Button) findViewById(R.id.cos_button);
tanButton = (Button) findViewById(R.id.tan_button);
cotButton = (Button) findViewById(R.id.cot_button);
addBtn = (Button) findViewById(R.id.add_button);
subtractBtn = (Button) findViewById(R.id.subtract_button);
multiplyBtn = (Button) findViewById(R.id.multiply_button);
divideBtn = (Button) findViewById(R.id.divide_button);
equalBtn = (Button) findViewById(R.id.equal_button);
decimalBtn = (Button) findViewById(R.id.decimal_button);
zeroBtn = (Button) findViewById(R.id.zero_button);
oneBtn = (Button) findViewById(R.id.one_button);
twoBtn = (Button) findViewById(R.id.two_button);
threeBtn = (Button) findViewById(R.id.three_button);
fourBtn = (Button) findViewById(R.id.four_button);
fiveBtn = (Button) findViewById(R.id.five_button);
sixBtn = (Button) findViewById(R.id.six_button);
sevenBtn = (Button) findViewById(R.id.seven_button);
eightBtn = (Button) findViewById(R.id.eight_button);
nineBtn = (Button) findViewById(R.id.nine_button);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr = new StringBuilder();
result = 0.0;
isDecimal = false;
inputTextView.setText("");
resultTextView.setText("");
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
char lastChar = inputStr.charAt(inputStr.length() - 1);
if (lastChar == '.') {
isDecimal = false;
}
inputStr.deleteCharAt(inputStr.length() - 1);
inputTextView.setText(inputStr.toString());
}
}
});
squareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = num * num;
resultTextView.setText(String.valueOf(result));
}
}
});
sqrtButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = Math.sqrt(num);
resultTextView.setText(String.valueOf(result));
}
}
});
reciprocalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = 1.0 / num;
resultTextView.setText(String.valueOf(result));
}
}
});
factorialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
int num = Integer.parseInt(inputStr.toString());
result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
resultTextView.setText(String.valueOf(result));
}
}
});
logButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = Math.log10(num);
resultTextView.setText(String.valueOf(result));
}
}
});
sinButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = Math.sin(num);
resultTextView.setText(String.valueOf(result));
}
}
});
cosButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = Math.cos(num);
resultTextView.setText(String.valueOf(result));
}
}
});
tanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = Math.tan(num);
resultTextView.setText(String.valueOf(result));
}
}
});
cotButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result = 1.0 / Math.tan(num);
resultTextView.setText(String.valueOf(result));
}
}
});
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result += num;
inputStr = new StringBuilder();
inputTextView.setText("");
}
}
});
subtractBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result -= num;
inputStr = new StringBuilder();
inputTextView.setText("");
}
}
});
multiplyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result *= num;
inputStr = new StringBuilder();
inputTextView.setText("");
}
}
});
divideBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
result /= num;
inputStr = new StringBuilder();
inputTextView.setText("");
}
}
});
equalBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inputStr.length() > 0) {
double num = Double.parseDouble(inputStr.toString());
switch (operation) {
case ADD:
result += num;
break;
case SUBTRACT:
result -= num;
break;
case MULTIPLY:
result *= num;
break;
case DIVIDE:
result /= num;
break;
default:
break;
}
inputStr = new StringBuilder();
inputTextView.setText("");
resultTextView.setText(String.valueOf(result));
}
}
});
decimalBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isDecimal) {
if (inputStr.length() == 0) {
inputStr.append("0");
}
inputStr.append(".");
inputTextView.setText(inputStr.toString());
isDecimal = true;
}
}
});
zeroBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("0");
inputTextView.setText(inputStr.toString());
}
});
oneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("1");
inputTextView.setText(inputStr.toString());
}
});
twoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("2");
inputTextView.setText(inputStr.toString());
}
});
threeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("3");
inputTextView.setText(inputStr.toString());
}
});
fourBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("4");
inputTextView.setText(inputStr.toString());
}
});
fiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("5");
inputTextView.setText(inputStr.toString());
}
});
sixBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("6");
inputTextView.setText(inputStr.toString());
}
});
sevenBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("7");
inputTextView.setText(inputStr.toString());
}
});
eightBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("8");
inputTextView.setText(inputStr.toString());
}
});
nineBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputStr.append("9");
inputTextView.setText(inputStr.toString());
}
});
}
}
```
阅读全文