android 简易计算器
思路:
将输入的表达式通过正则表达式分成数字数组和运算符数组,使用ArrayList来存储(方便删除)因为没有括号所以运算符优先级很好判断
例子:3+5*2-1index用来标记下标(虽然它一直都是0)每次计算的时候拿第一个运算符和第二个运算符做优先级比较,如果第一个运算符优先级高就计算第一个和第二个数字否则计算第二个和第三个数字,计算完后删除对应的运算符(ArrayList相当于一个链表,删除第一个节点后,下一个节点就变成了第一个节点,所以不需要关心下标的变化)
先来看一下效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/statement"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
android:textSize="30dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
android:textSize="30dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/clear"
android:text="C"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/divide"
android:text="/"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/multi"
android:text="*"
android:layout_weight="1"/>
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="--" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/seven"
android:text="7"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/eight"
android:text="8"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nine"
android:text="9"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sub"
android:text="-"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/four"
android:text="4"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/five"
android:text="5"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/six"
android:text="6"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/add"
android:text="+"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="0.8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/one"
android:text="1"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/percent"
android:text="%"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/two"
android:text="2"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/zero"
android:text="0"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/three"
android:text="3"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dot"
android:text="."
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Do"
android:text="="/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.example.in_t_dark.calculator;
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.ArrayList;
public class MainActivity extends AppCompatActivity {
String string="";
String ResultString="";
int falgs=0;// 1:numberdot 2:newnumber
char PreChar=' ';
char CurChar=' ';
int numberDot = 0;
int newnumber = 0;
ArrayList numbers = new ArrayList();
ArrayList operator = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button zero=findViewById(R.id.zero);
Button one=findViewById(R.id.one);
Button two=findViewById(R.id.two);
Button three=findViewById(R.id.three);
Button four=findViewById(R.id.four);
Button five=findViewById(R.id.five);
Button six=findViewById(R.id.six);
Button severn=findViewById(R.id.seven);
Button eight=findViewById(R.id.eight);
Button nine=findViewById(R.id.nine);
Button Do=findViewById(R.id.Do);
Button clear=findViewById(R.id.clear);
Button back=findViewById(R.id.back);
Button dot=findViewById(R.id.dot);
Button percent=findViewById(R.id.percent);
Button add=findViewById(R.id.add);
Button sub=findViewById(R.id.sub);
Button divide=findViewById(R.id.divide);
Button multi=findViewById(R.id.multi);
final TextView statement=findViewById(R.id.statement);
final TextView result=findViewById(R.id.result);
View.OnClickListener listen=new View.OnClickListener() {
@Override
public void onClick(View v) {
int id=v.getId();
if(id==R.id.clear){
string="";
operator.clear();
numbers.clear();
result.setText("");
statement.setText(string);
}
else if(id==R.id.back){
string=string.substring(0,string.length()-1);
statement.setText(string);
}
else if(id==R.id.Do){
for(String s:string.split("[\\+\\-\\*/]")){
if(s.substring(s.length()-1,s.length()).toCharArray()[0]=='%'){
if(s.length()>1) numbers.add(Double.valueOf(s.substring(0,s.length()-1))/100);
else numbers.add(Double.valueOf(0));
}
else
numbers.add(Double.valueOf(s));
}
for(char x:string.replaceAll("[\\d\\.%]","").toCharArray())
operator.add(x);
if(!Calculate())
result.setText("ERROR");
else {
result.setText(ResultString);
string=ResultString.toString();
numbers.clear();
}
}
else{
Button t=findViewById(id);
PreChar=CurChar;
CurChar=t.getText().toString().charAt(0);
if(!CheckErrors()){//CheckErrors还有点小问题 如果保证输入的表达式不会出错的话可以不需要判断了
string+=CurChar;
statement.setText(string);
}
}//else
}//onClick
};//OnClickListener
zero.setOnClickListener(listen);
one.setOnClickListener(listen);
two.setOnClickListener(listen);
three.setOnClickListener(listen);
four.setOnClickListener(listen);
five.setOnClickListener(listen);
six.setOnClickListener(listen);
severn.setOnClickListener(listen);
eight.setOnClickListener(listen);
nine.setOnClickListener(listen);
Do.setOnClickListener(listen);
clear.setOnClickListener(listen);
back.setOnClickListener(listen);
dot.setOnClickListener(listen);
percent.setOnClickListener(listen);
add.setOnClickListener(listen);
sub.setOnClickListener(listen);
divide.setOnClickListener(listen);
multi.setOnClickListener(listen);
}
boolean PriorityJudge(char a,char b){
if(a=='%')return true;
if(a=='+'||a=='-'){
if(b=='*'||b=='/')return false;
return true;
}
return true;
}
boolean CheckErrors(){
if(IsOperator(CurChar)){
newnumber=1;
if(string.length()==1)return true;
else if(IsOperator(PreChar)&&PreChar!='%')return true;
else if(CurChar=='.'){
if(numberDot == 1)return false;
else numberDot = 1;
}
}
return false;
}
boolean IsOperator(char c){
if(c=='%'||c=='+'||c=='-'||c=='*'||c=='/')return true;
return false;
}
double Out(double a,char b,double c){
switch (b){
case '+':
return a+c;
case '-':
return a-c;
case '*':
return a*c;
case '/':
return a/c;
}
return 0;
}
boolean Calculate(){
if(numbers.isEmpty()&&!operator.isEmpty())return false;
int index=0;
while(!operator.isEmpty()){
if(operator.size()>1){
if(PriorityJudge((char)operator.get(index),(char)operator.get(index+1))){
numbers.set(index,Out((double)numbers.get(index),(char)operator.get(index),(double)numbers.get(index+1)));
numbers.remove(index+1);
operator.remove(index);
}else{
numbers.set(index+1,Out((double)numbers.get(index+1),(char)operator.get(index+1),(double)numbers.get(index+2)));
numbers.remove(index+2);
operator.remove(index+1);
}
}//if
else{
numbers.set(index,Out((double)numbers.get(index),(char)operator.get(index),(double)numbers.get(index+1)));
numbers.remove(index+1);
operator.remove(index);
if(operator.isEmpty()&&(numbers.size()==1)){
if((int)((double)numbers.get(0))!=(double)numbers.get(0))
ResultString=String.valueOf(numbers.get(0));
else ResultString=String.valueOf((int)((double)numbers.get(0)));
return true;
}//if
}//else
}//while
if(!numbers.isEmpty()){
if(numbers.size()>1)return false;
if((int)((double)numbers.get(0))!=(double)numbers.get(0))
ResultString=String.valueOf(numbers.get(0));
else ResultString=String.valueOf((int)((double)numbers.get(0)));
return true;
}
return false;
}
}