修复了部分bug,主要解决负数之间运算所出现的问题,以及这个版本可以实现连续的输入,例1+2+3,输入1+2后,不需要先点=再加3,继续加3即可。上个版本链接:https://www.jianshu.com/p/b239bc5e84c3
负数解决方案
之前只要第一个数是负数运算时就会报错,原因在于切分cText字符串的时候会判定字符串开头的负号是运算符“-”,所以无法得到要运算的两个数。解决的途径是设置一个用于判断是否为负号的布尔类型的值(isMinus),在求结果之前进行对cText进行判断,是否是以“-”开的头,如果是的话,就去掉“-”并且置isMinus为true。函数getNum()通过isMinus判断是否给第一个数赋“-”,getResult()调用getNum()得两个值再进行运算。
Boolean isMinus = false; //用于判断式子开头是否为负号
if(cText.startsWith("-")){
cText = cText.substring(1);//将开头的负号去掉
isMinus = true;//第一个数是负数
private double[] getNum(String[] s,boolean isMinus){
double[] num =new double[2];
if (isMinus) {
num[0] = -Double.parseDouble(s[0]);//如果第一个数是负数,加上符号
}
else {
num[0] = Double.parseDouble(s[0]);
}
num[1] = Double.parseDouble(s[1]);
return num;
}
连续输入的解决方案
设置一个计数器count,每按一次加减乘除运算符就加一,当大于等于二时,显示前面两个数的计算结果加上运算符,即出现第二个运算符时,执行一遍“=”,再加上第二运算符。这样cText中至多有一个运算符,使得不会影响到cText的拆分。
case R.id.bt_add:
count++;
if (count < 2) {
textView.setText(textView.getText() + "+");
cText = cText + "+";
}
else {//count>=2 意味着有两个运算符
try {
textView.setText(getResult(cText) + "+");//先求结果打印再加“+”
cText = getResult(cText) + "+";
count = 1;
}catch (Exception e){
textView.setText(textView.getText());
}
}
break;
布局没改变,代码不贴了
MainActivity.java
package com.example.gaza.calculator;
import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity implements OnClickListener {
private Button bt_num1;
private Button bt_num2;
private Button bt_num3;
private Button bt_num4;
private Button bt_num5;
private Button bt_num6;
private Button bt_num7;
private Button bt_num8;
private Button bt_num9;
private Button bt_num0;
private Button bt_clear;
private Button bt_add;
private Button bt_divide;
private Button bt_result;
private Button bt_multiply;
private Button bt_point;
private Button bt_reduce;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViewAndListener();
}
//初始化 并且注册监听接口
private void initViewAndListener(){
bt_num1 = (Button) findViewById(R.id.bt_num1);
bt_num1.setOnClickListener(this);
bt_num2 = (Button) findViewById(R.id.bt_num2);
bt_num2.setOnClickListener(this);
bt_num3 = (Button) findViewById(R.id.bt_num3);
bt_num3.setOnClickListener(this);
bt_num4 = (Button) findViewById(R.id.bt_num4);
bt_num4.setOnClickListener(this);
bt_num5 = (Button) findViewById(R.id.bt_num5);
bt_num5.setOnClickListener(this);
bt_num6 = (Button) findViewById(R.id.bt_num6);
bt_num6.setOnClickListener(this);
bt_num7 = (Button) findViewById(R.id.bt_num7);
bt_num7.setOnClickListener(this);
bt_num8 = (Button) findViewById(R.id.bt_num8);
bt_num8.setOnClickListener(this);
bt_num9 = (Button) findViewById(R.id.bt_num9);
bt_num9.setOnClickListener(this);
bt_num0 = (Button) findViewById(R.id.bt_num0);
bt_num0.setOnClickListener(this);
bt_clear = (Button) findViewById(R.id.bt_clear);
bt_clear.setOnClickListener(this);
bt_add = (Button) findViewById(R.id.bt_add);
bt_add.setOnClickListener(this);
bt_divide = (Button) findViewById(R.id.bt_divide);
bt_divide.setOnClickListener(this);
bt_reduce = (Button) findViewById(R.id.bt_reduce);
bt_reduce.setOnClickListener(this);
bt_result = (Button) findViewById(R.id.bt_result);
bt_result.setOnClickListener(this);
bt_point = (Button) findViewById(R.id.bt_point);
bt_point.setOnClickListener(this);
bt_multiply = (Button) findViewById(R.id.bt_multiply);
bt_multiply.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
}
private String cText = "";//用于存要计算的式子
private int count=0; //计数器,用于判断cText中的运算符个数
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_num0:
textView.setText(textView.getText() + "0");
cText = cText + "0";
break;
case R.id.bt_num1:
textView.setText(textView.getText() + "1");
cText = cText + "1";
break;
case R.id.bt_num2:
textView.setText(textView.getText() + "2");
cText = cText + "2";
break;
case R.id.bt_num3:
textView.setText(textView.getText() + "3");
cText = cText + "3";
break;
case R.id.bt_num4:
textView.setText(textView.getText() + "4");
cText = cText + "4";
break;
case R.id.bt_num5:
textView.setText(textView.getText() + "5");
cText = cText + "5";
break;
case R.id.bt_num6:
textView.setText(textView.getText() + "6");
cText = cText + "6";
break;
case R.id.bt_num7:
textView.setText(textView.getText() + "7");
cText = cText + "7";
break;
case R.id.bt_num8:
textView.setText(textView.getText() + "8");
cText = cText + "8";
break;
case R.id.bt_num9:
textView.setText(textView.getText() + "9");
cText = cText + "9";
break;
case R.id.bt_add:
count++;
if (count < 2) {
textView.setText(textView.getText() + "+");
cText = cText + "+";
}
else {//count>=2 意味着有两个运算符
try {
textView.setText(getResult(cText) + "+");//先求结果打印再加“+”
cText = getResult(cText) + "+";
count = 1;
}catch (Exception e){
textView.setText(textView.getText());
}
}
break;
case R.id.bt_reduce:
if (cText == ""){
textView.setText("-");
cText +="-";
}
else {
count++;
if (count < 2) {
textView.setText(textView.getText() + "-");
cText = cText + "-";
} else {
try {
textView.setText(getResult(cText) + "-");
cText = getResult(cText) + "-";
count = 1;
}
catch(Exception x){
textView.setText(textView.getText());
}
}
}
break;
case R.id.bt_multiply:
count++;
if (count < 2) {
textView.setText(textView.getText() + "*");
cText = cText + "*";
}
else {
try {
textView.setText(getResult(cText) + "*");
cText = getResult(cText) + "*";
count = 1;
}
catch (Exception e){
textView.setText(textView.getText());
}
}
break;
case R.id.bt_divide:
count++;
if (count < 2) {
textView.setText(textView.getText() + "/");
cText = cText + "/";
}
else {
try {
textView.setText(getResult(cText) + "/");
cText = getResult(cText) + "/";
count = 1;
}
catch (Exception e){
textView.setText(textView.getText());
}
}
break;
case R.id.bt_point:
textView.setText(textView.getText() + ".");
cText = cText + ".";
break;
case R.id.bt_clear:
textView.setText("");
cText = "";
count = 0;
break;
case R.id.bt_result:
textView.setText(getResult(cText));
cText = getResult(cText);
count = 0;
break;
}
// System.out.println(cText);
}
//求结果
public String getResult(String cText){
Boolean isMinus = false; //用于判断式子开头是否为负号
if(cText.startsWith("-")){
cText = cText.substring(1);//将开头的负号去掉
isMinus = true;//第一个数是负数
}
if (cText.contains("+")){
String[] s = cText.split("\\+");//切分字符串 得到只有两个数字字符串的数组
double[] num = getNum(s,isMinus);
return String.valueOf(num[0] + num[1]);
}
else if (cText.contains("-")){
String[] s = cText.split("\\-");
double[] num = getNum(s,isMinus);
return String.valueOf(num[0] - num[1]);
}
else if (cText.contains("*")){
String[] s = cText.split("\\*");
double one,two;
double[] num = getNum(s,isMinus);
return String.valueOf(num[0] * num[1]);
}
else if (cText.contains("/")){
String[] s = cText.split("/");
double[] num = getNum(s,isMinus);
return String.valueOf(num[0] / num[1]);
}
else return "0";
}
//得到需要运算的两个数
private double[] getNum(String[] s,boolean isMinus){
double[] num =new double[2];
if (isMinus) {
num[0] = -Double.parseDouble(s[0]);//如果第一个数是负数,加上符号
}
else {
num[0] = Double.parseDouble(s[0]);
}
num[1] = Double.parseDouble(s[1]);
return num;
}
}