1. 完成一个计算器的设计,可以以手机自带的计算器为参考。设计过程中,注意考虑界面的美观性,不同机型的适应性,以及功能的完备性。
2. 注意结合Activity的生命周期,考虑不同情况下计算器的界面状态。
3. 如有余力,可以考虑实现一个高精度科学计算型的计算器。
activity_main.xml
w
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_point;
Button btn_clear;
Button btn_del;
Button btn_plus;
Button btn_minus;
Button btn_multiply;
Button btn_divide;
Button btn_equal;
Button btn_left;
Button btn_right;
private TextView et_input;
private StringBuilder pending = new StringBuilder();
private void initView() {
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_plus = (Button) findViewById(R.id.btn_plus);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_multiply = (Button) findViewById(R.id.btn_multiply);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_equal = (Button) findViewById(R.id.btn_equal);
et_input = (TextView) findViewById(R.id.et_input);
btn_left = (Button) findViewById(R.id.btn_left);
btn_right = (Button) findViewById(R.id.btn_right);
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_point.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_left.setOnClickListener(this);
btn_right.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void onClick(View v) {
int last = 0;
if(pending.length()!=0)
{
last = pending.codePointAt(pending.length()-1);
}
switch (v.getId())
{
case R.id.btn_0:
pending = pending.append("0");
et_input.setText(pending);
break;
case R.id.btn_1:
pending = pending.append("1");
et_input.setText(pending);
break;
case R.id.btn_2:
pending = pending.append("2");
et_input.setText(pending);
break;
case R.id.btn_3:
pending = pending.append("3");
et_input.setText(pending);
break;
case R.id.btn_4:
pending = pending.append("4");
et_input.setText(pending);
break;
case R.id.btn_5:
pending = pending.append("5");
et_input.setText(pending);
break;
case R.id.btn_6:
pending = pending.append("6");
et_input.setText(pending);
break;
case R.id.btn_7:
pending = pending.append("7");
et_input.setText(pending);
break;
case R.id.btn_8:
pending = pending.append("8");
et_input.setText(pending);
break;
case R.id.btn_9:
pending = pending.append("9");
et_input.setText(pending);
break;
case R.id.btn_plus:
pending = pending.append("+");
et_input.setText(pending);
break;
case R.id.btn_minus:
pending = pending.append("-");
et_input.setText(pending);
break;
case R.id.btn_multiply:
pending = pending.append("*");
et_input.setText(pending);
break;
case R.id.btn_divide:
pending = pending.append("/");
et_input.setText(pending);
break;
case R.id.btn_point:
if (judje1())
{
pending = pending.append(".");
et_input.setText(pending);
}
break;
case R.id.btn_right:// )右括号
if((last>='0' &&last<='9'||last==')')&&judje2()==1)
{
pending = pending.append(")");
et_input.setText(pending);
}
break;
case R.id.btn_left:// (左括号
if((last!='(')||(last<='0' &&last>='9'))
{
pending = pending.append("(");
et_input.setText(pending);
}
break;
case R.id.btn_del: //删除
if (pending.length() != 0)
{
pending = pending.delete(pending.length() - 1, pending.length());
et_input.setText(pending);
}
break;
case R.id.btn_clear: //清空
pending = pending.delete(0, pending.length());
et_input.setText(pending);
break;
case R.id.btn_equal: // =等于
if ((pending.length() > 1))
{
Function inf = new Function();
String jieguo;
try
{
String a = inf.toSuffix(pending);
jieguo = inf.dealEquation(a);
}
catch (Exception ex)
{
jieguo = "出错";
}
et_input.setText(pending + "=" + jieguo);
pending = pending.delete(0, pending.length());
if (Character.isDigit(jieguo.charAt(0)))
{
pending = pending.append(jieguo);
}
}
break;
default:
break;
}
}
private boolean judje1() {
String a = "+-*/.";
int[] b = new int[a.length()];
int max;
for (int i = 0; i < a.length(); i++)
{
String c = "" + a.charAt(i);
b[i] = pending.lastIndexOf(c);
}
Arrays.sort(b);
if (b[a.length() - 1] == -1)
{
max = 0;
}
else
{
max = b[a.length() - 1];
}
if (pending.indexOf(".", max) == -1)
{
return true;
}
else
{
return false;
}
}
private int judje2(){
int a=0,b=0;
for(int i = 0 ; i < pending.length() ;i++)
{
if(pending.charAt(i)=='(' )
{
a++;
}
if(pending.charAt(i)==')' )
{
b++;
}
}
if(a == b)
return 0;
if(a > b)
return 1;
return 2;
}
}
Function.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.*;
public class Function {
private static final Map basic =new HashMap();
static {
basic.put('-',1);
basic.put('+', 1);
basic.put('*', 2);
basic.put('/', 2);
basic.put('(', 0);
}
public String toSuffix(StringBuilder infix){
List queue = new ArrayList();
List stack = new ArrayList();
char[] charArr = infix.substring(0,infix.length()).trim().toCharArray();
String standard = "*/+-()";
char ch = '&';
int len = 0;
for (int i = 0; i < charArr.length; i++)
{
ch = charArr[i];
if(Character.isDigit(ch))
{
len++;
}
else if(ch == '.')
{
len++;
}
else if(standard.indexOf(ch) != -1)
{
if(len > 0)
{
queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));
len = 0;
}
if(ch == '(')
{
stack.add(ch);
continue;
}
if (!stack.isEmpty())
{
int size = stack.size() - 1;
boolean flag = false;
while (size >= 0 && ch == ')' && stack.get(size) != '(')
{
queue.add(String.valueOf(stack.remove(size)));
size--;
flag = true;
}
if(ch==')'&&stack.get(size) == '(')
{
flag = true;
}
while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch))
{
queue.add(String.valueOf(stack.remove(size)));
size--;
}
}
if(ch != ')')
{
stack.add(ch);
}
else
{
stack.remove(stack.size() - 1);
}
}
if(i == charArr.length - 1)
{
if(len > 0)
{
queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
}
int size = stack.size() - 1;
while (size >= 0)
{
queue.add(String.valueOf(stack.remove(size)));
size--;
}
}
}
String a = queue.toString();
return a.substring(1,a.length()-1);
}
public String dealEquation(String equation){
String [] arr = equation.split(", ");
List list = new ArrayList();
for (int i = 0; i < arr.length; i++)
{
int size = list.size();
switch (arr[i])
{
case "+": double a = Double.parseDouble(list.remove(size-2))+ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(a)); break;
case "-": double b = Double.parseDouble(list.remove(size-2))- Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(b)); break;
case "*": double c = Double.parseDouble(list.remove(size-2))* Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(c)); break;
case "/": double d = Double.parseDouble(list.remove(size-2))/ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(d)); break;
default: list.add(arr[i]); break;
}
}
return list.size() == 1 ? list.get(0) : "运算失败" ;
}
}