计算器应用程序适用于一个edittext中的两个输入,但不适用于两个以上的输入

时间:2015-10-21 20:41:11

标签: android

我的代码为单个5+10=15中的两个输入即editText提供了答案,但是对于两个以上的输入,即5+10*2=20,如果我在显示1+的某个值后第二次点击运算符++ 2。如何在值之后仅显示一次以及如何计算editText

的完整方程式

我的代码如下:

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
Button reset,root,div,mul,del,four,five,six,seven,eight,nine,one,two,three,minus,plus,zero,deci,eql,percent;
int op1,op2;
String first,second;
   String str ="";
    Character op ='q';
    String displayStr="";
    double num,numtemp,temp;

EditText mCalculatorDisplay1;
    DecimalFormat df = new DecimalFormat("@###########");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mCalculatorBrain = new CalculatorBrain();
        mCalculatorDisplay1 = (EditText) findViewById(R.id.edittext1);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        zero = (Button) findViewById(R.id.zero);
        plus = (Button) findViewById(R.id.plus);
        minus = (Button) findViewById(R.id.minus);
        mul = (Button) findViewById(R.id.multi);
        div = (Button) findViewById(R.id.div);
        del = (Button) findViewById(R.id.delete);
        eql = (Button) findViewById(R.id.equalTo);
        deci = (Button) findViewById(R.id.decimal);
        root = (Button) findViewById(R.id.root);
        reset = (Button) findViewById(R.id.cancel);
        percent = (Button) findViewById(R.id.percent);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        plus.setOnClickListener(this);
        minus.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
        del.setOnClickListener(this);
        eql.setOnClickListener(this);
        deci.setOnClickListener(this);
        root.setOnClickListener(this);
        reset.setOnClickListener(this);
        percent.setOnClickListener(this);
//  mCalculatorDisplay1.setKeyListener(DigitsKeyListener.getInstance(true, true));
    }
    private void perform() {
        // TODO Auto-generated method stub
        str = "";
        numtemp = num;

    }
    private void insert(int j) {
        // TODO Auto-generated method stub
        str = str + Integer.toString(j);
        num = Double.valueOf(str);
        displayStr += Integer.toString(j);
        mCalculatorDisplay1.setText(displayStr);

    }

    private void calculate() {
        // TODO Auto-generated method stub
         first=String.valueOf(numtemp);
         second=String.valueOf(num);
        if(op == '+'){
            num = numtemp+num;
        }
        else if(op == '-')
            num = numtemp-num;
        else if(op == '/')
            num = numtemp/num;
        else if(op == '*')
            num = numtemp*num;
else if (op=='%')
            num = numtemp*num/100;
        else if (op=='√') {

            num = Math.sqrt(num);
        }
        mCalculatorDisplay1.setText(first + op + second + "\n"+ "=" + num );



    }

    private void insert(String j) {
        //Insert the  values by clicking button
        str = str+String.valueOf(j);
      String  num = String.valueOf(str);
        displayStr += String.valueOf(j);
        mCalculatorDisplay1.setText(displayStr);
    }



        @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.one:
                insert(1);
                break;
            case R.id.two:
                insert(2);
                break;
            case R.id.three:
                insert(3);
                break;
            case R.id.four:
                insert(4);
                break;
            case R.id.five:
                insert(5);
                break;
            case R.id.six:
                insert(6);
                break;
            case R.id.seven:
                insert(7);
                break;
            case R.id.eight:
                insert(8);
                break;
            case R.id.nine:
                insert(9);
                break;
            case R.id.decimal:
               insert(".");
                break;
            case R.id.percent:
                perform();
                op = '%';
                displayStr += "%";
                break;
            case R.id.zero:
                insert(0);
                break;
            case R.id.plus:
//                if (displayStr.contains("+")){
//                    op = '+';
//                    displayStr+="";
//                    perform();
//                    mCalculatorDisplay1.setText(displayStr);
//                }
//                else
                perform();
                op = '+';
                displayStr+= "+";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.minus:
                perform();
                op = '-';
                displayStr += "-";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.multi:
                perform();
                op = '*';
                displayStr += "*";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.div:
                perform();
                op = '/';
                displayStr += "/";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.cancel:
                str="";
                displayStr = "";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.root:
                perform();
                op = '√';
                displayStr += "√";
                mCalculatorDisplay1.setText(displayStr);
                break;
            case R.id.delete:
               str="";
                displayStr  ="";
                displayStr=mCalculatorDisplay1.getText().toString();
                if (displayStr.length() >1 ) {
                    displayStr = displayStr.substring(0, displayStr.length() - 1);
                    mCalculatorDisplay1.setText(displayStr);
                }
                else if (displayStr.length() <=1 ) {
                    displayStr  ="";
                    mCalculatorDisplay1.setText( displayStr);
                }
//                    mCalculatorDisplay1.setText(displayStr.substring(0,displayStr.length()-1));

                break;
            case R.id.equalTo:
                calculate();
                break;
        }

        }
}

1 个答案:

答案 0 :(得分:0)

您应该使用exp4j。它是java的表达式求值程序。

为了您的目的

  1. 从editText

  2. 获取字符串
  3. 将字符串传递给以下段

    Calculable calc = new ExpressionBuilder("5+10*2").build()
    double result=calc.calculate();