Problem in calculator in android studio

0

I'm a beginner and I'm developing a simple calculator in android studio, but it only shows the result of two values, eg 1 + 1 = 2. I would like to do several operations at the same time, eg 1 + 2 * 3/4. Follow the code

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import static android.R.attr.id;

public class MainActivity extends AppCompatActivity  {

    private double operador1;
    private double operador2;
    private double operadores;

   boolean mAdicao, mSubtacao, mMultiplicacao, mDivisao;

    private EditText txt_visor;


    private Button n1;
    private Button n2;
    private Button n3;
    private Button n4;
    private Button n5;
    private Button n6;
    private Button n7;
    private Button n8;
    private Button n9;
    private Button n0;
    private Button dois_zeros;

    private Button ponto;

    private Button soma;
    private Button subitracao;
    private Button multiplicacao;
    private Button divisao;
    private Button porcento;
    private Button potenciacao;
    private Button raiz;

    private Button igual;

    private Button limpar;

    private Button sair;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);







        txt_visor = (EditText) findViewById(R.id.txt_visor);

        n1 = (Button) findViewById(R.id.n1);
        n2 = (Button) findViewById(R.id.n2);
        n3 = (Button) findViewById(R.id.n3);
        n4 = (Button) findViewById(R.id.n4);
        n5 = (Button) findViewById(R.id.n5);
        n6 = (Button) findViewById(R.id.n6);
        n7 = (Button) findViewById(R.id.n7);
        n8 = (Button) findViewById(R.id.n8);
        n9 = (Button) findViewById(R.id.n9);
        n0 = (Button) findViewById(R.id.n0);
        dois_zeros = (Button) findViewById(R.id.dois_zeros);


        ponto = (Button) findViewById(R.id.ponto);

        soma = (Button) findViewById(R.id.soma);
        subitracao = (Button) findViewById(R.id.subitracao);
        multiplicacao = (Button) findViewById(R.id.multiplicacao);
        divisao = (Button) findViewById(R.id.divisao);
        porcento = (Button) findViewById(R.id.porcento);
        potenciacao = (Button) findViewById(R.id.potenciacao);
        raiz = (Button) findViewById(R.id.raiz);


        igual = (Button) findViewById(R.id.igual);

        limpar = (Button) findViewById(R.id.limpar);


        /* setando o número de cada botão*/

        n1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "1");
            }
        });


        n2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "2");
            }
        });

        n3.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "3");
            }
        });

        n4.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "4");
            }
        });

        n5.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "5");
            }
        });

        n6.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "6");
            }
        });

        n7.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "7");
            }
        });

        n8.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "8");
            }
        });

        n9.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "9");
            }
        });

        n0.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "0");
            }
        });

        dois_zeros.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                txt_visor.setText(txt_visor.getText() + "00");
            }
        });

        soma.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                if (txt_visor==null){
                    txt_visor.setText("");
                }else {
                    operador1 = Double.parseDouble(txt_visor.getText() + "");
                    mAdicao = true;
                    txt_visor.setText(null);


                }
            }
        });

        subitracao.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                if (txt_visor==null){
                    txt_visor.setText("");
                }else {
                    operador1 = Double.parseDouble(txt_visor.getText() + "");
                    mSubtacao = true;
                    txt_visor.setText(null);

                }
            }
        });

        multiplicacao.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                if (txt_visor==null){
                    txt_visor.setText("");
                }else {
                    operador1 = Double.parseDouble(txt_visor.getText() + "");
                    mMultiplicacao = true;
                    txt_visor.setText(null);

                }
            }
        });

        divisao.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                if (txt_visor==null){
                    txt_visor.setText("");
                }else {
                    operador1 = Double.parseDouble(txt_visor.getText() + "");
                    mDivisao = true;
                    txt_visor.setText(null);

                }
            }
        });

        igual.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            operador2 = Double.parseDouble(txt_visor.getText().toString()+"" );

                if (mAdicao==true){

                    txt_visor.setText(operador1 +operador2 +"");


                }
                if (mSubtacao ==true){

                    txt_visor.setText(operador1-operador2+"");
                }
            }
        });



    }
    
asked by anonymous 04.04.2017 / 20:21

1 answer

3

There are already questions and answers that can help you with this, because there are already libraries capable of receiving a String like "(2 * 6) +3" and returning the result of the expression.

Perhaps the easiest thing to do is to use a quoted JavaScript engine here , which does this.

See here for some solutions, such as using "BeanShell" interpreter, which can be used like this:

Interpreter interpreter = new Interpreter();
interpreter.eval("result = (7+21*6)/(32-27)");
System.out.println(interpreter.get("result"));

See here how to put BeanShell in Android Studio.

This another post may also help. See also MathEvaluator quoted here .

    
07.04.2017 / 22:59