高级方程计算器java?

时间:2015-10-10 04:22:15

标签: java android

我正在为Android创建一个计算器应用。 我之前从未真正创建过计算器,因此我只知道如何进行基本计算。我现在做方程的方式是当我按下数字按钮(1,2,3等)时,该数字被添加到一个名为theEquation的字符串中。当我按下一个操作按钮(+, - ,/,*)时,会将方程解析成一个名为" a"然后清除了等式。按下" ="按下更多数字后它将theEquation设置为另一个名为" b"的双变量。然后它有一个基于操作的case / switch,可以对它进行加,减,乘或除。 这对于一个基本的计算器来说很好,但是我想制作一个高级计算器来做这样的方程式:" 5 + 6 - 4/50"。我已经尝试了几个小时这样做,但无法理解我应该如何做到这一点。

任何帮助将不胜感激。 顺便说一句,这是我的代码:

public class MainActivity extends Activity
{
// MESSY CLASS :(
private TextView answerTextView;
private Button number1, number2, number3, number4, number5, number6, number7, number8, number9, number0;
private Button equalsBtn, plusBtn, subBtn, mulBtn, divBtn, clrBtn, delBtn;

private String theEquation = "";
private double answer = 0;

// temp fields
private double a = 0, b = 0;
private int operator = 0;

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

    answerTextView = (TextView) findViewById(R.id.answerTextView);
    number1 = (Button) findViewById(R.id.number1);
    number2 = (Button) findViewById(R.id.number2);
    number3 = (Button) findViewById(R.id.number3);
    number4 = (Button) findViewById(R.id.number4);
    number5 = (Button) findViewById(R.id.number5);
    number6 = (Button) findViewById(R.id.number6);
    number7 = (Button) findViewById(R.id.number7);
    number8 = (Button) findViewById(R.id.number8);
    number9 = (Button) findViewById(R.id.number9);
    number0 = (Button) findViewById(R.id.number0);

    equalsBtn = (Button) findViewById(R.id.equalsBtn);
    plusBtn = (Button) findViewById(R.id.plusBtn);
    subBtn = (Button) findViewById(R.id.subBtn);
    mulBtn = (Button) findViewById(R.id.mulBtn);
    divBtn = (Button) findViewById(R.id.divBtn);
    clrBtn = (Button) findViewById(R.id.btnClr);
    delBtn = (Button) findViewById(R.id.delBtn);

    numberPresses();
    operationPresses();
}

public void numberPresses()
{
    number1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "1";
            answerTextView.setText(theEquation);
        }
    });
    number2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "2";
            answerTextView.setText(theEquation);
        }
    });
    number3.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "3";
            answerTextView.setText(theEquation);
        }
    });
    number4.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "4";
            answerTextView.setText(theEquation);
        }
    });
    number5.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "5";
            answerTextView.setText(theEquation);
        }
    });
    number6.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "6";
            answerTextView.setText(theEquation);
        }
    });
    number7.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "7";
            answerTextView.setText(theEquation);
        }
    });
    number8.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "8";
            answerTextView.setText(theEquation);
        }
    });
    number9.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "9";
            answerTextView.setText(theEquation);
        }
    });
    number0.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation += "0";
            answerTextView.setText(theEquation);
        }
    });
}

public void operationPresses()
{
    plusBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 1;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    subBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 2;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    mulBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 3;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    divBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            a = Double.parseDouble(theEquation);
            operator = 4;
            theEquation = "";
            answerTextView.setText(theEquation);
        }
    });
    clrBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            theEquation = "";
            a = 0;
            b = 0;
            answerTextView.setText(theEquation);
        }
    });
    delBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (theEquation.length() != 0)
                theEquation = theEquation.substring(0, theEquation.length() - 1);
            answerTextView.setText(theEquation);
        }
    });
    equalsBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            b = Double.parseDouble(theEquation);
            try
            {
                switch (operator)
                {
                case 1:
                    answer = a + b;
                    break;
                case 2:
                    answer = a - b;
                    break;
                case 3:
                    answer = a * b;
                    break;
                case 4:
                    answer = a / b;
                    break;
                default:
                    answer = 0;
                    break;
                }
            } catch (Exception e)
            {
                theEquation = "Error!";
                e.printStackTrace();
            }
            theEquation = "" + answer;
            answerTextView.setText(theEquation);
        }
    });
}
}

0 个答案:

没有答案