第29篇 Android Studio无括号计算器(三)逻辑代码及资源文件
3.逻辑代码及资源
3.1.strings.xml
设置一些数据:
<resources>
<string name="app_name">简易计算器</string>
<string name="btn_clear_name">C</string>
<string name="btn_delete_name">D</string>
<string name="btn_add_name">+</string>
<string name="btn_sub_name">-</string>
<string name="btn_mul_name">*</string>
<string name="btn_div_name">/</string>
<string name="btn_1_name">1</string>
<string name="btn_2_name">2</string>
<string name="btn_3_name">3</string>
<string name="btn_4_name">4</string>
<string name="btn_5_name">5</string>
<string name="btn_6_name">6</string>
<string name="btn_7_name">7</string>
<string name="btn_8_name">8</string>
<string name="btn_9_name">9</string>
<string name="btn_0_name">0</string>
<string name="btn_point_name">.</string>
<string name="btn_equal_name">=</string>
</resources>
3.2.逻辑代码
就是设置按钮的监听事件。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
int i_before;
int i_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i_before = 0;
i_back = 0;
initData();
}
protected void initData() {
Button btn_0 = findViewById(R.id.btn_0);
Button btn_1 = findViewById(R.id.btn_1);
Button btn_2 = findViewById(R.id.btn_2);
Button btn_3 = findViewById(R.id.btn_3);
Button btn_4 = findViewById(R.id.btn_4);
Button btn_5 = findViewById(R.id.btn_5);
Button btn_6 = findViewById(R.id.btn_6);
Button btn_7 = findViewById(R.id.btn_7);
Button btn_8 = findViewById(R.id.btn_8);
Button btn_9 = findViewById(R.id.btn_9);
Button btn_add = findViewById(R.id.btn_add);
Button btn_sub = findViewById(R.id.btn_sub);
Button btn_mul = findViewById(R.id.btn_mul);
Button btn_div = findViewById(R.id.btn_div);
Button btn_point = findViewById(R.id.btn_point);
Button btn_equal = findViewById(R.id.btn_equal);
Button btn_clear = findViewById(R.id.btn_clear);
Button btn_delete = findViewById(R.id.btn_delete);
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_add.setOnClickListener(this);
btn_sub.setOnClickListener(this);
btn_mul.setOnClickListener(this);
btn_div.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_delete.setOnClickListener(this);
}
@Override
public void onClick(View view) {
EditText edit_formula = findViewById(R.id.edit_formula);
String str_formula = edit_formula.getText().toString();
if(view.getId() == R.id.btn_equal){
if(str_formula.equals("")){
Toast.makeText(MainActivity.this,"请先输入算式",Toast.LENGTH_SHORT).show();
}
else{
str_formula = compute(str_formula,"*","/");
str_formula = compute(str_formula,"+","-");
edit_formula.setText(str_formula);
}
}
else if(view.getId() == R.id.btn_add || view.getId() == R.id.btn_sub ||
view.getId() == R.id.btn_mul || view.getId() == R.id.btn_div){
if(str_formula.equals("")){
if(view.getId() == R.id.btn_sub){
Button btn_char = findViewById(view.getId());
String str_endChar = btn_char.getText().toString();
str_formula = str_formula + str_endChar;
edit_formula.setText(str_formula);
}
else{
Toast.makeText(MainActivity.this,"请先输入数字",Toast.LENGTH_SHORT).show();
}
}
else{
String str_end = str_formula.substring(str_formula.length() - 1);
boolean end_isOperator = str_end.equals("+") || str_end.equals("-") ||str_end.equals("*")||str_end.equals("/");
if(!end_isOperator){
Button btn_char = findViewById(view.getId());
String str_endChar = btn_char.getText().toString();
str_formula = str_formula + str_endChar;
edit_formula.setText(str_formula);
}
}
}
else if(view.getId() == R.id.btn_point){
if(str_formula.equals("")){
Toast.makeText(MainActivity.this,"请先输入数字",Toast.LENGTH_SHORT).show();
}
else{
String str = before(str_formula.length(),str_formula);
int count = 0;
for(int i = 0;i < str.length();i++){
if(str.charAt(i) == '.'){
count++;
}
}
if(count >= 1){
Toast.makeText(MainActivity.this,"一个数不能使用多个小数点",Toast.LENGTH_SHORT).show();
}
else{
Button btn_char = findViewById(view.getId());
String str_endChar = btn_char.getText().toString();
str_formula = str_formula + str_endChar;
edit_formula.setText(str_formula);
}
}
}
else if(view.getId() == R.id.btn_clear){
edit_formula.setText("");
}
else if(view.getId() == R.id.btn_delete){
if(str_formula.equals("")){
Toast.makeText(MainActivity.this,"无可删除数据",Toast.LENGTH_SHORT).show();
}
else{
str_formula = str_formula.substring(0,str_formula.length() - 1);
edit_formula.setText(str_formula);
}
}
else{
Button btn_char = findViewById(view.getId());
String str_endChar = btn_char.getText().toString();
str_formula = str_formula + str_endChar;
edit_formula.setText(str_formula);
}
}
protected String compute(String formal, String operator1, String operator2){
int i = 1;
while (i < formal.length()){
String str1 = String.valueOf(formal.charAt(i));
if(str1.equals(operator1) || str1.equals(operator2)){
String str2 = before(i,formal);
String str3 = back(i,formal);
if(str2.equals("") || str3.equals("")){
i++;
continue;
}
String s1 = formal.substring(0,i_before);
if(operator1.equals("+") && i_before > 0){
str2 = formal.substring(0,i);
s1 = "";
}
String s2 = getResult(Double.parseDouble(str2),Double.parseDouble(str3),formal.charAt(i));
String s3 = formal.substring(i_back);
formal = s1 + s2 + s3;
i = i_before;
}
else{
i++;
}
}
return formal;
}
protected String before(int index,String formal){
int i = index - 1;
while(i >= 0){
if((formal.charAt(i) >= '0' && formal.charAt(i) <= '9') || formal.charAt(i) == '.'){
i--;
}
else{
break;
}
}
i_before = i + 1;
return formal.substring(i + 1,index);
}
protected String back(int index,String formal){
int i = index + 1;
while(i < formal.length()){
if((formal.charAt(i) >= '0' && formal.charAt(i) <= '9') || formal.charAt(i) == '.'){
i++;
}
else{
break;
}
}
i_back = i;
return formal.substring(index + 1,i);
}
protected String getResult(double number1,double number2,char operator){
double result = 0;
switch(operator){
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
break;
}
DecimalFormat df = new DecimalFormat("#.0");
return df.format(result);
}
}
3.3.结果
就是这样了,设置监听事件没什么说的了。
|