Android 简易计算器

package com.example.calculate;


import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{


Button btn0=null;
    Button btn1=null;
    Button btn2=null;
    Button btn3=null;
    Button btn4=null;
    Button btn5=null;
    Button btn6=null;
    Button btn7=null;
    Button btn8=null;
    Button btn9=null;
    Button btnC=null;
    Button btnDiv=null;
    Button btnPls=null;
    Button btnDel=null;
    Button btnMv=null;
    Button btnSum=null;
    Button btnKH=null;
    Button btnZF=null;
    Button btnEq=null;
    Button btnDot=null;
    TextView input=null;
    TextView output=null;
    Button btn[] = {btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9};
    int ID[] = {R.id.button0,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9};
    
    double num1=0,num2=0;
    double Result=0;//计算结果
    int op = 0; //造作数
    boolean isClickEq = false;
boolean flag = false; //括号左右边
//EditText edt2 = (EditText) findViewById(R.id.editText2);


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//edt2.setBackgroundColor(Color.CYAN);
for(int i=0;i<10;i++){
btn[i] = (Button)findViewById(ID[i]);
}
btnC = (Button)findViewById(R.id.buttonC);
btnDiv = (Button)findViewById(R.id.buttonDiv);
btnPls = (Button)findViewById(R.id.buttonPls);
btnDel = (Button)findViewById(R.id.buttonDel);
btnMv = (Button)findViewById(R.id.buttonMv);
btnSum = (Button)findViewById(R.id.buttonSum);
btnKH = (Button)findViewById(R.id.buttonKH);
btnEq = (Button)findViewById(R.id.buttonEq);
btnDot = (Button)findViewById(R.id.buttonDot);
btnZF = (Button)findViewById(R.id.buttonZF);
input = (TextView)findViewById(R.id.editText1);
output = (TextView)findViewById(R.id.editText2);

for(int i=0;i<10;i++){
btn[i].setOnClickListener(this);
}
btnC.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnPls.setOnClickListener(this);
btnDel.setOnClickListener(this);
btnMv.setOnClickListener(this);
btnSum.setOnClickListener(this);
btnKH.setOnClickListener(this);
btnEq.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnZF.setOnClickListener(this);
input.setOnClickListener(this);
output.setOnClickListener(this);

}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = input.getText().toString();
switch(v.getId()){
case R.id.buttonDel:
String mystr = input.getText().toString();
try{
input.setText(mystr.substring(0,mystr.length()-1));
}catch(Exception e){
input.setText("");
}
break;
case R.id.buttonC:
input.setText(null);
input.setEnabled(true);
output.setText(null);
break;
case R.id.button0:
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
case R.id.button5:
case R.id.button6:
case R.id.button7:
case R.id.button8:
case R.id.button9:
case R.id.buttonDot:
if(str!="0")
input.setText(str+((Button)v).getText());
else input.setText(((Button)v).getText());
break;

case R.id.buttonSum:
case R.id.buttonMv:
case R.id.buttonPls:
case R.id.buttonDiv:
input.setText(str+((Button)v).getText());
break;
case R.id.buttonKH:   //括号
if(flag == false){
input.setText(str+"(");
flag = true;
}
else{
input.setText(str+")");
flag = false;
}
break;
case R.id.buttonZF: //正负号
input.setText("-("+str+")");
break;
case R.id.buttonEq: //等于号
input.setText(str+"=");
input.setEnabled(false);
String exp = input.getText().toString().substring(0, input.getText().toString().length()-1);
output.setText(getResult(exp));
break;
}
}
private String getResult(String exp){
double result = 0.0;

// 如果表达式中有括号则递归计算
if(exp.contains("(")){
int left=exp.lastIndexOf("(");
int right=exp.indexOf(")");
String subExp = exp.substring(left+1, right);
String res = getResult(subExp); //计算子表达式
exp = exp.substring(0, left)+res+exp.substring(right+1);
exp = getResult(exp);
}
//格式化表达式
//String newExp = formatExp(exp);
List<Character> opts = getOptions(exp);
List<Double> nums = getNums(exp);
//处理乘除加减
for(int i=0;i<opts.size();i++){
char opt = opts.get(i);
if(opt=='/'||opt=='*'){
opts.remove(i);
double d1 = nums.remove(i);
double d2 = nums.remove(i);
if(opt == '/'){
d1 = d1/d2;
}else d1 = d1*d2;
nums.add(i,d1);
//i--;
}
}
while(!opts.isEmpty()){
char opt = opts.remove(0);
double d1 = nums.remove(0);
double d2 = nums.remove(0);
if(opt == '+'){
d1=d1+d2;
}else{
d1=d1-d2;
}
nums.add(0, d1);
}
result = nums.get(0);
return String.valueOf(result);
}
//获取表达式中所有运算符
private static List<Character> getOptions(String exp){
List<Character> opts = new ArrayList<Character>();
StringTokenizer st = new StringTokenizer(exp,"@.0123456789");
while(st.hasMoreTokens()){
opts.add(st.nextToken().charAt(0));
}
return opts;
}
//获取表达式中所有数字
private static List<Double> getNums(String exp){
List<Double> nums = new ArrayList<Double>();
StringTokenizer st = new StringTokenizer(exp,"+-*/");
while(st.hasMoreTokens()){
String num = st.nextToken();
if(num.contains("@")){
num = "-"+num.substring(1);
}
nums.add(Double.parseDouble(num));
}
return nums;
}
//格式化表达式(将负号替换成‘@’符号)
private static String formatExp(String exp){
if(exp.matches(".*[+-/*]")){
exp = exp.substring(0,exp.length()-1);
}
String res = exp;
        if (exp.charAt(0) == '-') {     
            res = "@" + res.substring(1);
        }
        /*for (int i = 1; i < res.length(); i++) {
            if (res.charAt(i) == '-' && (res.charAt(i - 1) == '/' || res.charAt(i - 1) == '*')) {  
                res = res.substring(0, i) + "@" + res.substring(i + 1);
            }
        }*/
        return res;


}

}



布局文件如下:


<?xml version="1.0"?>


-<RelativeLayout android:useDefaultMargins="true" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">




-<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical">


<TextView android:layout_height="wrap_content" android:layout_width="60dip" android:text="@+string/name"/>


<LinearLayout android:layout_height="0px" android:layout_width="0px" android:focusableInTouchMode="true" android:focusable="true"/>


<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="end|bottom" android:id="@+id/editText1"/>


<TextView android:layout_height="wrap_content" android:layout_width="60dip" android:text="@+string/result"/>


<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="end|bottom" android:id="@+id/editText2" android:ems="10"/>




-<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout">




-<TableRow android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableRow1">


<Button android:layout_height="wrap_content" android:text="C" android:id="@+id/buttonC" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="/" android:id="@+id/buttonDiv" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="*" android:id="@+id/buttonPls" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="del" android:id="@+id/buttonDel" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


</TableRow>




-<TableRow android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableRow2">


<Button android:layout_height="wrap_content" android:text="7" android:id="@+id/button7" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="8" android:id="@+id/button8" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="9" android:id="@+id/button9" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="-" android:id="@+id/buttonMv" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


</TableRow>




-<TableRow android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableRow3">


<Button android:layout_height="wrap_content" android:text="4" android:id="@+id/button4" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="5" android:id="@+id/button5" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="6" android:id="@+id/button6" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="+" android:id="@+id/buttonSum" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


</TableRow>




-<TableRow android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableRow4">


<Button android:layout_height="wrap_content" android:text="1" android:id="@+id/button1" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="2" android:id="@+id/button2" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="3" android:id="@+id/button3" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="( )" android:id="@+id/buttonKH" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


</TableRow>




-<TableRow android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableRow5">


<Button android:layout_height="wrap_content" android:text="0" android:id="@+id/button0" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="." android:id="@+id/buttonDot" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="+/-" android:id="@+id/buttonZF" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


<Button android:layout_height="wrap_content" android:text="=" android:id="@+id/buttonEq" android:background="@drawable/login_button_selector" android:padding="0.5dip"/>


</TableRow>


</TableLayout>


</LinearLayout>


</RelativeLayout>


猜你喜欢

转载自blog.csdn.net/did_you/article/details/78453306