最近需要完成一个简单计算器的设计,思索了之后完成了一下的设计。
package k.caculator;
import java.math.BigDecimal;
/**
* Created by Administrator on 2017/4/1.
*/
public class clu {
BigDecimal number1;
BigDecimal number2;
//sub
BigDecimal clulatesub(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.subtract(number2);
}
//add
BigDecimal clulateadd(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.add(number2);
}
//multiply
BigDecimal clulatemultiply(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.multiply(number2);
}
//divde
BigDecimal clulatedivde(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.divide(number2);
}
//平方
BigDecimal clulatem(String h1){
number1 = new BigDecimal(h1);
return number1.multiply(number1);
}
//阶乘
double clulatex(String h1){
int i = Integer.valueOf(h1).intValue();
double result=1;
while(i>=1){
result = i*result;
i--;
}
return result;
}
//sin
double clulatesin(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.sin(i);
return result;
}
//cos
double clulatecos(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.cos(i);
return result;
}
//tan
double clulatetan(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.tan(i);
return result;
}
}
package k.caculator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView rsText = null; //显示器
String num1, num2;
BigDecimal Result; //计算结果
int op = 0; //判断操作数,
boolean isClickEqu = false; //判断是否按了“=”按钮
clu haha = new clu();//定义类实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//默认软键盘不弹出
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//让editText失去焦点,使用EditText的clearFocus方法
// EditText edit = (EditText)findViewById(R.id.show);
// edit.clearFocus();
// //让软键盘始终不显示
// EditText edit = (EditText)findViewById(R.id.show);
// edit.setInputType(InputType.TYPE_NULL);
//功能按钮
rsText = (TextView) findViewById(R.id.show);
Button b_delete = (Button) findViewById(R.id.delete);
Button b_back = (Button) findViewById(R.id.back);
Button b_jia = (Button) findViewById(R.id.jia);
Button b_jian = (Button) findViewById(R.id.jian);
Button b_cheng = (Button) findViewById(R.id.chen);
Button b_chu = (Button) findViewById(R.id.chu);
Button b_dengyu = (Button) findViewById(R.id.dengyu);
Button b_dian = (Button) findViewById(R.id.dian);
//高级按钮
Button bt = (Button) findViewById(R.id.left);
bt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(MainActivity.this,second.class);
MainActivity.this.startActivity(intent);
}
});
//数字按钮
Button b0 = (Button) findViewById(R.id.b0);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button b4 = (Button) findViewById(R.id.b4);
Button b5 = (Button) findViewById(R.id.b5);
Button b6 = (Button) findViewById(R.id.b6);
Button b7 = (Button) findViewById(R.id.b7);
Button b8 = (Button) findViewById(R.id.b8);
Button b9 = (Button) findViewById(R.id.b9);
//listener
rsText.setOnClickListener(this);
b_delete.setOnClickListener(this);
b_back.setOnClickListener(this);
b_jia.setOnClickListener(this);
b_jian.setOnClickListener(this);
b_cheng.setOnClickListener(this);
b_chu.setOnClickListener(this);
b_dengyu.setOnClickListener(this);
b_dian.setOnClickListener(this);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//AC和back
case R.id.back:
String myStr = rsText.getText().toString();
try {
rsText.setText(myStr.substring(0, myStr.length() - 1));
} catch (Exception e) {
rsText.setText("");
}
break;
case R.id.delete:
rsText.setText(null);
break;
//b0--9
case R.id.b0:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString = rsText.getText().toString();
myString += "0";
rsText.setText(myString);
break;
case R.id.b1:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString1 = rsText.getText().toString();
myString1 += "1";
rsText.setText(myString1);
break;
case R.id.b2:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString2 = rsText.getText().toString();
myString2 += "2";
rsText.setText(myString2);
break;
case R.id.b3:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString3 = rsText.getText().toString();
myString3 += "3";
rsText.setText(myString3);
break;
case R.id.b4:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString4 = rsText.getText().toString();
myString4 += "4";
rsText.setText(myString4);
break;
case R.id.b5:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString5 = rsText.getText().toString();
myString5 += "5";
rsText.setText(myString5);
break;
case R.id.b6:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString6 = rsText.getText().toString();
myString6 += "6";
rsText.setText(myString6);
break;
case R.id.b7:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString7 = rsText.getText().toString();
myString7 += "7";
rsText.setText(myString7);
break;
case R.id.b8:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString8 = rsText.getText().toString();
myString8 += "8";
rsText.setText(myString8);
break;
case R.id.b9:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString9 = rsText.getText().toString();
myString9 += "9";
rsText.setText(myString9);
break;
case R.id.dian:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString0 = rsText.getText().toString();
myString0 += ".";
rsText.setText(myString0);
break;
//b+,-,=
case R.id.jia:
String myStringAdd = rsText.getText().toString();
if (myStringAdd.equals(null)) {
return;
}
num1 = myStringAdd;
rsText.setText(null);
op = 1;
isClickEqu = false;
break;
case R.id.jian:
String myStringSub = rsText.getText().toString();
if (myStringSub.equals(null)) {
return;
}
num1 = myStringSub;
rsText.setText(null);
op = 2;
isClickEqu = false;
break;
case R.id.chen:
String myStringMul = rsText.getText().toString();
if (myStringMul.equals(null)) {
return;
}
num1 = myStringMul;
rsText.setText(null);
op = 3;
isClickEqu = false;
break;
case R.id.chu:
String myStringDiv = rsText.getText().toString();
if (myStringDiv.equals(null)) {
return;
}
num1 = myStringDiv;
rsText.setText(null);
op = 4;
isClickEqu = false;
break;
case R.id.dengyu:
String myStringEqu = rsText.getText().toString();
if (myStringEqu.equals(null)) {
return;
}
num2 = myStringEqu;
rsText.setText(null);
switch (op) {
case 0:
double i = Integer.valueOf(num2).intValue();
Result = BigDecimal.valueOf(i);
break;
case 1:
Result = haha.clulateadd(num1,num2);
break;
case 2:
Result = haha.clulatesub(num1,num2);;
break;
case 3:
Result = haha.clulatemultiply(num1,num2);;
break;
case 4:
double num2ch = Integer.valueOf(num2).intValue();
if(num2ch==0){
rsText.setText("格式输入错误!");
return;
}
Result = haha.clulatedivde(num1,num2);;
break;
default:
Result = BigDecimal.valueOf('0');
break;
}
rsText.setText(String.valueOf(Result));
isClickEqu = true;
break;
default:
break;
}
}
//定义一个变量,来表示是否退出
private static boolean isExit = false;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};
//点击两次返回键退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
//点击两次返回键退出
private void exit() {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
// 利用handler延迟发送更改状态信息
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
finish();
System.exit(0);
}
}
}
//高级界面的Java文件
package k.caculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.math.BigDecimal;
public class second extends AppCompatActivity implements View.OnClickListener{
TextView rsText = null; //显示器
String num1, num2;
BigDecimal Result; //计算结果
int op = 0; //判断操作数,
boolean isClickEqu = false; //判断是否按了“=”按钮
clu haha = new clu();//定义类实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//功能按钮
rsText = (TextView) findViewById(R.id.show);
Button b_clear = (Button) findViewById(R.id.clear);//不能够清除
Button b_back = (Button) findViewById(R.id.back);
Button b_m = (Button) findViewById(R.id.m);
Button b_x = (Button) findViewById(R.id.jiechen);
Button b_sin = (Button) findViewById(R.id.sin);
Button b_cos = (Button) findViewById(R.id.cos);
Button b_tan = (Button) findViewById(R.id.tan);
Button b_dian = (Button) findViewById(R.id.dian);
Button b_dengyu = (Button) findViewById(R.id.dengyu);
//返回按钮
Button b_re = (Button) findViewById(R.id.re);
//返回按钮功能
b_re.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(second.this,MainActivity.class);
second.this.startActivity(intent);
}
});
//数字按钮
Button b0 = (Button) findViewById(R.id.b0);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button b4 = (Button) findViewById(R.id.b4);
Button b5 = (Button) findViewById(R.id.b5);
Button b6 = (Button) findViewById(R.id.b6);
Button b7 = (Button) findViewById(R.id.b7);
Button b8 = (Button) findViewById(R.id.b8);
Button b9 = (Button) findViewById(R.id.b9);
//listener
rsText.setOnClickListener(this);
b_m.setOnClickListener(this);
b_back.setOnClickListener(this);
b_sin.setOnClickListener(this);
b_cos.setOnClickListener(this);
b_tan.setOnClickListener(this);
b_x.setOnClickListener(this);
b_dengyu.setOnClickListener(this);
b_dian.setOnClickListener(this);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//AC和back
case R.id.back:
String myStr = rsText.getText().toString();
try {
rsText.setText(myStr.substring(0, myStr.length() - 1));
} catch (Exception e) {
rsText.setText("");
}
break;
case R.id.clear:
rsText.setText(null);
break;
//b0--9
case R.id.b0:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString = rsText.getText().toString();
myString += "0";
rsText.setText(myString);
break;
case R.id.b1:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString1 = rsText.getText().toString();
myString1 += "1";
rsText.setText(myString1);
break;
case R.id.b2:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString2 = rsText.getText().toString();
myString2 += "2";
rsText.setText(myString2);
break;
case R.id.b3:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString3 = rsText.getText().toString();
myString3 += "3";
rsText.setText(myString3);
break;
case R.id.b4:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString4 = rsText.getText().toString();
myString4 += "4";
rsText.setText(myString4);
break;
case R.id.b5:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString5 = rsText.getText().toString();
myString5 += "5";
rsText.setText(myString5);
break;
case R.id.b6:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString6 = rsText.getText().toString();
myString6 += "6";
rsText.setText(myString6);
break;
case R.id.b7:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString7 = rsText.getText().toString();
myString7 += "7";
rsText.setText(myString7);
break;
case R.id.b8:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString8 = rsText.getText().toString();
myString8 += "8";
rsText.setText(myString8);
break;
case R.id.b9:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString9 = rsText.getText().toString();
myString9 += "9";
rsText.setText(myString9);
break;
case R.id.dian:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString0 = rsText.getText().toString();
myString0 += ".";
rsText.setText(myString0);
break;
case R.id.m:
String myStringm = rsText.getText().toString();
if (myStringm.equals(null)) {
return;
}
num1 = myStringm;
rsText.setText(myStringm + "^2");
op = 1;
isClickEqu = false;
break;
case R.id.jiechen:
String myStringx = rsText.getText().toString();
if (myStringx.equals(null)) {
return;
}
num1 = myStringx;
rsText.setText(myStringx + "!");
op = 2;
isClickEqu = false;
break;
case R.id.sin:
String myStringsin = rsText.getText().toString();
if (myStringsin.equals(null)) {
return;
}
num1 = myStringsin;
rsText.setText("sin("+myStringsin + ")");
op = 3;
isClickEqu = false;
break;
case R.id.cos:
String myStringcos = rsText.getText().toString();
if (myStringcos.equals(null)) {
return;
}
num1 = myStringcos;
rsText.setText("cos("+myStringcos + ")");
op = 4;
isClickEqu = false;
break;
case R.id.tan:
String myStringtan = rsText.getText().toString();
if (myStringtan.equals(null)) {
return;
}
num1 = myStringtan;
rsText.setText("cos("+myStringtan + ")");
op = 5;
isClickEqu = false;
break;
case R.id.dengyu:
String myStringEqu = rsText.getText().toString();
if (myStringEqu.equals(null)) {
return;
}
num2 = myStringEqu;
rsText.setText(null);
switch (op) {
case 0://本身
double i = Integer.valueOf(num2).intValue();
Result = BigDecimal.valueOf(i);
break;
case 1://平方
Result = haha.clulatem(num1);
break;
case 2://阶乘
rsText.setText(String.valueOf(haha.clulatex(num1)));
return;
case 3: rsText.setText(String.valueOf(haha.clulatesin(num1)));;
return;
case 4:
rsText.setText(String.valueOf(haha.clulatecos(num1)));
return;
case 5:
rsText.setText(String.valueOf(haha.clulatetan(num1)));
return;
default:
Result = BigDecimal.valueOf('0');
break;
}
rsText.setText(String.valueOf(Result));
isClickEqu = true;
break;
default:
break;
}
}
}