1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import android.widget.TextView;
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv_result; private String firstNum = ""; private String secondNum = "";
private String operator = "";
private String result = "";
private String showText = "";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator); tv_result = findViewById(R.id.tv_result); findViewById(R.id.btn_cancel).setOnClickListener(this); findViewById(R.id.btn_divide).setOnClickListener(this); findViewById(R.id.btn_multiply).setOnClickListener(this); findViewById(R.id.btn_clear).setOnClickListener(this); findViewById(R.id.btn_seven).setOnClickListener(this); findViewById(R.id.btn_eight).setOnClickListener(this); findViewById(R.id.btn_nine).setOnClickListener(this); findViewById(R.id.btn_plus).setOnClickListener(this); findViewById(R.id.btn_four).setOnClickListener(this); findViewById(R.id.btn_five).setOnClickListener(this); findViewById(R.id.btn_six).setOnClickListener(this); findViewById(R.id.btn_minus).setOnClickListener(this); findViewById(R.id.btn_one).setOnClickListener(this); findViewById(R.id.btn_two).setOnClickListener(this); findViewById(R.id.btn_three).setOnClickListener(this); findViewById(R.id.ib_sqrt).setOnClickListener(this); findViewById(R.id.btn_reciprocal).setOnClickListener(this); findViewById(R.id.btn_zero).setOnClickListener(this); findViewById(R.id.btn_dot).setOnClickListener(this); findViewById(R.id.btn_equal).setOnClickListener(this); }
@Override public void onClick(View view) { String inputText;
if(view.getId() == R.id.ib_sqrt) { inputText = "√";
} else { inputText = ((TextView)view).getText().toString(); } if(view.getId() == R.id.btn_clear) { clear(); } else if(view.getId() == R.id.btn_cancel) { } else if(view.getId() == R.id.btn_plus) { operator = inputText; refreshText(showText+operator); } else if(view.getId() == R.id.btn_minus) { operator = inputText; refreshText(showText+operator); } else if(view.getId() == R.id.btn_multiply) { operator = inputText; refreshText(showText+operator); } else if(view.getId() == R.id.btn_divide) { operator = inputText; refreshText(showText+operator); } else if(view.getId() == R.id.btn_equal) { double calculate_result = calculate_four(); refreshOperate(String.valueOf(calculate_four())); refreshText(showText+"="+result);
} else if(view.getId() == R.id.ib_sqrt) { double sqrt_result = Math.sqrt(Double.parseDouble(firstNum)); refreshOperate(String.valueOf(sqrt_result)); refreshText(showText+"√="+result); } else if(view.getId() == R.id.btn_reciprocal) { double reciprocal_result = 1/Double.parseDouble(firstNum); refreshOperate(String.valueOf(reciprocal_result)); refreshText(showText+"/="+result); } else { if (result.length() > 0 && operator.equals("")) { clear(); }
if(operator.equals("")) { firstNum = firstNum + inputText; } else { secondNum = secondNum + inputText; } if (showText.equals("0") && !inputText.equals(".")) { refreshText(inputText);
} else { refreshText(showText+inputText); }
}
}
private double calculate_four() { switch (operator) { case "+": return Double.parseDouble(firstNum) + Double.parseDouble(secondNum); case "-": return Double.parseDouble(firstNum) - Double.parseDouble(secondNum); case "*": return Double.parseDouble(firstNum) * Double.parseDouble(secondNum); case "/": return Double.parseDouble(firstNum) / Double.parseDouble(secondNum); default: return 0; }
}
private void refreshText(String text) { showText = text;
tv_result.setText(showText); } private void refreshOperate(String new_result) { result = new_result; firstNum = result; secondNum = ""; operator = "";
} private void clear() { refreshOperate(""); refreshText("0");
} }
|