源码下载
闲来无事,用android做一个计算器玩玩,想着做一个私密空间,表面是计算器,按下原先设定的算式后可以跳转到私密界面,当想想有些费事,以后若还有闲散时间,再来实现。
我实现了更完善的计算器,功能更多,逻辑更完善,欢迎访问!点击这里
界面是这样的,采用了GridLayout布局,很容易实现。代码如下
对应的string.xml代码文件如下:
简易计算器
CE
Backspace
7
8
9
*
4
5
6
-
1
2
3
+
0
.
=
/
输入算式进行计算
到这里,计算器的界面布局已经实现,下面就是最重要的逻辑处理了。逻辑处理分为两部分:算式输入的强制排错和算式的结果计算。算式输入的强制排错就是对算式输入的规范,比如不能连续输入++,**,23.23.23,之类的无效算式;算式的结果计算就是根据输入的算式(我将输入的算式保存在string对象里)计算出结果。代码如下:
package com.example.ffy.other;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
private Button[] buttons = new Button[18];
private int[] ids = new int[]{R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4,R.id.bt5,R.id.bt6,R.id.bt7,
R.id.bt8,R.id.bt9,R.id.bt10,R.id.bt11,R.id.bt12,R.id.bt13,R.id.bt14,R.id.bt15,R.id.bt16,R.id.bt17,R.id.bt18
};
private TextView textView;
private String expression = "";
private boolean end = false;
private int countOperate=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.calculator);
for(int i=0;i1){ //算式长度大于1
expression = expression.substring(0,expression.length()-1);//退一格
int i = expression.length()-1;
char tmp = expression.charAt(i); //获得最后一个字符
char tmpFront = tmp;
for(;i>=0;i--){ //向前搜索最近的 +-*/和.,并退出
tmpFront = expression.charAt(i);
if(tmpFront=='.'||tmpFront=='+'||tmpFront=='-'||tmpFront=='*'||tmpFront=='/'){
break;
}
}
// Toast.makeText(this, "tmp = "+tmp, Toast.LENGTH_SHORT).show();
if(tmp>='0'&&tmp<='9'){ //最后一个字符为数字,则识别数赋值为0
countOperate=0;
}else if(tmp==tmpFront&&tmpFront!='.') countOperate=2; //如果为+-*/,赋值为2
else if(tmpFront=='.') countOperate=1; //如果前面有小数点赋值为1
}else if(expression.length()==1){
expression = "";
}
}else if(current.equals(".")){
if(expression.equals("")||countOperate==2){
expression+="0"+current;
countOperate = 1; //小数点按过之后赋值为1
}
if(countOperate==0){
expression+=".";
countOperate = 1;
}
}else if(current.equals("+")||current.equals("-")||current.equals("*")||current.equals("/")){
if(countOperate==0){
expression+=current;
countOperate = 2; // +-*/按过之后赋值为2
}
}else if(current.equals("=")){ //按下=时,计算结果并显示
double result = (double) Math.round(count()*100)/100;
expression+="="+result+"(结果只保留两位小数)";
end = true; //此次计算结束
}
else{//此处是当退格出现2+0时,用current的值替代0
if(expression.length()>=1){
char tmp1 = expression.charAt(expression.length()-1);
if(tmp1=='0'&&expression.length()==1){
expression = expression.substring(0,expression.length()-1);
}
else if(tmp1=='0'&&expression.length()>1){
char tmp2 = expression.charAt(expression.length()-2);
if(tmp2=='+'||tmp2=='-'||tmp2=='*'||tmp2=='/'){
expression = expression.substring(0,expression.length()-1);
}
}
}
expression+=current;
if(countOperate==2||countOperate==1) countOperate=0;
}
// Toast.makeText(this, "countOperate:"+countOperate, Toast.LENGTH_SHORT).show();
textView.setText(expression); //显示出来
}
//计算逻辑,求expression表达式的值
private double count(){
double result=0;
double tNum=1,lowNum=0.1,num=0;
char tmp=0;
int operate = 1; //识别+-*/,为+时为正数,为-时为负数,为×时为-2/2,为/时为3/-3;
boolean point = false;
for(int i=0;i
源码下载