这是我首个使用java写的app,也在学习郭霖老师的第一行代码和李刚老师的疯狂java讲义之时,进行的练习之作,刚刚学习java和android,有些地方可能并没有做的很完善,希望大家可以多多包涵。
compile 'com.android.support:percent:24.2.1'
"http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
-- Customize your theme here. -->
<item name="windowActionBar">falseitem>
<item name="windowNoTitle">trueitem>
style>
resources>
<resources>
<string name="app_name">冰炭不投de小计算器string>
resources>
android:allowBackup="true"
android:icon="@mipmap/图片名字" //修改这句
android:label="@string/app_name"
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView;
private StringBuilder pending=new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);//类似定义和赋值
Button button0 = (Button)findViewById(R.id.button0);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
Button button6 = (Button)findViewById(R.id.button6);
Button button7 = (Button)findViewById(R.id.button7);
Button button8 = (Button)findViewById(R.id.button8);
Button button9 = (Button)findViewById(R.id.button9);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
Button buttonSign = (Button)findViewById(R.id.buttonSign);
Button buttonMul = (Button)findViewById(R.id.buttonMul);
Button buttonDiv = (Button)findViewById(R.id.buttonDiv);
Button buttonEqual = (Button)findViewById(R.id.buttonEqual);
Button buttonDot= (Button)findViewById(R.id.buttonDot);
Button buttonRightBracket = (Button)findViewById(R.id.buttonRightBracket);
Button buttonLeftBracket = (Button)findViewById(R.id.buttonLeftBracket);
Button buttonBack = (Button)findViewById(R.id.buttonBack);
Button buttonAC = (Button)findViewById(R.id.buttonAC);
button0.setOnClickListener(this);//调用监听器
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSign.setOnClickListener(this);
buttonMul.setOnClickListener(this);
buttonDiv.setOnClickListener(this);
buttonEqual.setOnClickListener(this);
buttonDot.setOnClickListener(this);
buttonRightBracket.setOnClickListener(this);
buttonLeftBracket.setOnClickListener(this);
buttonBack.setOnClickListener(this);
buttonAC.setOnClickListener(this);
}
@Override
//监听器
public void onClick(View v){
//当前字符串的最后一个和倒数第二个
int last = 0;
int last_q =0;
if(pending.length()!=0)
{
last = pending.codePointAt(pending.length()-1);
if (pending.length()>1)
last_q =pending.codePointAt(pending.length()-2);
}
switch (v.getId()) {
case R.id.button0:
if(last!=')'){
pending = pending.append("0");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"0");
}
textView.setText(pending);
break;
case R.id.button1:
if(last!=')'){
pending = pending.append("1");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"1");
}
textView.setText(pending);
break;
case R.id.button2:
if(last!=')'){
pending = pending.append("2");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"2");
}
textView.setText(pending);
break;
case R.id.button3:
if(last!=')'){
pending = pending.append("3");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"3");
}
textView.setText(pending);
break;
case R.id.button4:
if(last!=')'){
pending = pending.append("4");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"4");
}
textView.setText(pending);
break;
case R.id.button5:
if(last!=')'){
pending = pending.append("5");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"5");
}
textView.setText(pending);
break;
case R.id.button6:
if(last!=')'){
pending = pending.append("6");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"6");
}
textView.setText(pending);
break;
case R.id.button7:
if(last!=')'){
pending = pending.append("7");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"7");
}
textView.setText(pending);
break;
case R.id.button8:
if(last!=')'){
pending = pending.append("8");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"8");
}
textView.setText(pending);
break;
case R.id.button9:
if(last!=')'){
pending = pending.append("9");
}
else {
pending = pending.replace(pending.length()-1,pending.length(),"9");
}
textView.setText(pending);
break;
case R.id.buttonAdd: // +加
if(last>='0' &&last<='9' ||last==')') {
pending = pending.append("+");
}
else if(pending.length()>1&&last!='('){
pending = pending.replace(pending.length()-1,pending.length(),"+");
}
if(pending.length()>1&&last=='-'&&(last_q=='*'||last_q=='/')){
pending = pending.replace(pending.length()-2,pending.length(),"+");
}
textView.setText(pending);
break;
case R.id.buttonSign:// -减
if(last>='0' &&last<='9' ||last==')'||last=='*'||last=='/'||pending.length()==0) {
pending = pending.append("-");
}
else if(pending.length()>1&&last!='('){
pending = pending.replace(pending.length()-1,pending.length(),"-");
}
if(pending.length()>1&&last=='-'&&(last_q=='*'||last_q=='/')){
pending = pending.replace(pending.length()-2,pending.length(),"-");
}
textView.setText(pending);
break;
case R.id.buttonMul: // *乘
if(last>='0' &&last<='9' ||last==')') {
pending = pending.append("*");
}
else if(pending.length()>1&&last!='(')
{
pending = pending.replace(pending.length()-1,pending.length(),"*");
}
if(pending.length()>1&&last=='-'&&(last_q=='*'||last_q=='/')){
pending = pending.replace(pending.length()-2,pending.length(),"*");
}
textView.setText(pending);
break;
case R.id.buttonDiv: // /除
if(last>='0' &&last<='9' ||last==')') {
pending = pending.append("/");
}
else if(pending.length()>1&&last!='('){
pending = pending.replace(pending.length()-1,pending.length(),"/");
}
if(last=='-'&&(last_q=='*'||last_q=='/')){
pending = pending.replace(pending.length()-2,pending.length(),"/");
}
textView.setText(pending);
break;
case R.id.buttonEqual: // =等于
if((last>='0' &&last<='9' ||last==')')&&judje2()==0) {
InfixInToDuffix inf = new InfixInToDuffix();
String jieguo;
String a="0";
try {
a = inf.toSuffix(pending);
jieguo=inf.dealEquation(a);
}
catch (Exception ex){
jieguo = "出错";
}
textView.setText(pending+"="+jieguo);
pending = pending.delete(0, pending.length());
if(Character.isDigit(jieguo.charAt(0))) {
pending = pending.append(jieguo);
}
}
break;
case R.id.buttonDot:// 。小数点
if(last>='0' &&last<='9'&&judje1()) {
pending = pending.append(".");
textView.setText(pending);
}
break;
case R.id.buttonRightBracket:// )右括号
if((last>='0' &&last<='9'||last==')')&&judje2()==1) {
pending = pending.append(")");
textView.setText(pending);
}
break;
case R.id.buttonLeftBracket:// (左括号
if((last<'0' ||last>'9')&&last!='.'&&last!=')') {
pending = pending.append("(");
textView.setText(pending);
}
break;
case R.id.buttonBack: //删除
if(pending.length()!=0) {
pending = pending.delete(pending.length()-1,pending.length());
textView.setText(pending);
}
break;
case R.id.buttonAC: //清空
pending = pending.delete(0, pending.length());
textView.setText(pending);
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;
}
}
public class InfixInToDuffix {
//使用集合定义好符号的运算优先级别
private static final Mapbasic =new HashMap();
static {
basic.put('-',1);
basic.put('+', 1);
basic.put('*', 2);
basic.put('/', 2);
basic.put('(', 0);//在运算中 ()的优先级最高,但是此处因程序中需要 故设置为0
}
//将中缀表达式转换为后缀表达式
public String toSuffix(StringBuilder infix){
List queue = new ArrayList(); //定义队列 用于存储 数字 以及最后的 后缀表达式
List stack = new ArrayList(); //定义栈 用于存储 运算符 最后stack中会被 弹空
char[] charArr = infix.substring(0,infix.length()).trim().toCharArray(); //字符数组 用于拆分数字或符号
String standard = "*/+-()"; //判定标准 将表达式中会出现的运算符写出来
char ch = '&'; //在循环中用来保存 字符数组的当前循环变量的 这里仅仅是初始化一个值 没有意义
int len = 0; //用于记录字符长度 【例如100*2,则记录的len为3 到时候截取字符串的前三位就是数字】
for (int i = 0; i < charArr.length; i++) {//开始迭代
int last=0;
if(i>0) {
last = charArr[i-1];
}
ch = charArr[i]; //保存当前迭代变量
if(Character.isDigit(ch)) { //如果当前变量为 数字
len++;
}else if(ch == '.'){ //如果当前变量为 . 会出现在小数里面
len++;
}else if(ch == '-' && (last =='*'||last=='/'||i==0)){ //如果当前变量为 . 会出现在小数里面
len++;
continue;
}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()) { //如果栈不为empty
int size = stack.size() - 1; //获取栈的大小-1 即代表栈最后一个元素的下标
boolean flag = false; //设置标志位
while (size >= 0 && ch == ')' && stack.get(size) != '(') { //若当前ch为右括号,则 栈里元素从栈顶一直弹出,直到弹出到 左括号
queue.add(String.valueOf(stack.remove(size))); //注意此处条件:ch并未入栈,所以并未插入队列中;同样直到找到左括号的时候,循环结束了,所以左括号也不会放入队列中【也就是:后缀表达式中不会出现括号】
size--; //size-- 保证下标永远在栈最后一个元素【栈中概念:指针永远指在栈顶元素】
flag = true; //设置标志位为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))); //同样 此处也是remove()方法,既能得到要获取的元素,也能将栈中元素移除掉
size--;
}
}
if(ch != ')') { //若当前元素不是右括号
stack.add(ch); //就要保证这个符号 入栈
} else { //否则就要出栈 栈内符号
stack.remove(stack.size() - 1);
}
}
if(i == charArr.length - 1) { //如果已经走到了 中缀表达式的最后一位
if(len > 0) { //如果len>0 就截取数字
queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
}
int size = stack.size() - 1; //size表示栈内最后一个元素下标
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(); //用于计算时 存储运算过程的集合【例如list中当前放置 100 20 5 / 则取出20/5 最终将结果4存入list 此时list中结果为 100 4 】
for (int i = 0; i < arr.length; i++) { //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数 都是size-2
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; //如果是数字 直接放进list中
}
}
return list.size() == 1 ? list.get(0) : "运算失败" ; //最终list中仅有一个结果,否则就是算错了
}
}
在之后的陆续测试中,由学长发现其中的不足之处,对于浮点数的计算,容易发生精度丢失,比如说3*1.3=3.900000000000000000001,对于这种情况我使用了BigDecimal类对运算进行了改进。更改后代码如下(只更改了InfixInToDuffix类下的dealEquation方法)
public String dealEquation(String equation){
String [] arr = equation.split(", "); //根据, 拆分字符串
List list = new ArrayList(); //用于计算时 存储运算过程的集合【例如list中当前放置 100 20 5 / 则取出20/5 最终将结果4存入list 此时list中结果为 100 4 】
for (int i = 0; i < arr.length; i++) { //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数 都是size-2
int size = list.size();
switch (arr[i]) {
case "+": BigDecimal a = new BigDecimal(list.remove(size-2)).add(new BigDecimal(list.remove(size-2))); list.add(String.valueOf(a)); break;
case "-": BigDecimal b = new BigDecimal(list.remove(size-2)).subtract(new BigDecimal(list.remove(size-2))); list.add(String.valueOf(b)); break;
case "*": BigDecimal c = new BigDecimal(list.remove(size-2)).multiply(new BigDecimal(list.remove(size-2))); list.add(String.valueOf(c)); break;
case "/": BigDecimal d = new BigDecimal(list.remove(size-2)).divide(new BigDecimal(list.remove(size-2)),10,BigDecimal.ROUND_HALF_UP); list.add(String.valueOf(d)); break;
default: list.add(arr[i]); break; //如果是数字 直接放进list中
}
}
return list.size() == 1 ? list.get(0) : "运算失败" ; //最终list中仅有一个结果,否则就是算错了
}