设计一个安卓计算器要求实现功能 1实现基本的加减乘除运算 2实现开方、平方,倒数运算、阶乘 运算、求任意底的对数运算、任 数的N次方运算 3正弦,余弦,正切,余切运算的源程序代码
时间: 2024-06-09 08:05:28 浏览: 125
以下是一个实现上述功能的安卓计算器的源代码示例:
```
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());
}
});
}
}
```
阅读全文