简体   繁体   English

如何在计算器中执行多项操作?

[英]How can I make multiple operations work in calculator?

I made simple calculator for android for sake of learning but its buggy. 为了学习,我为Android制作了一个简单的计算器,但它有很多问题。 All number keys just append respective number to TextView . 所有数字键仅append各自的数字appendTextView Equals button does the job of adding/subtracting/divide/multiply. “等于”按钮完成加/减/除/乘运算。 But the problem is, it can do only one at a time, if I mix plus operation with minus, it will crash. 但是问题是,它一次只能执行一次,如果我将加号操作与减号混合使用,它将崩溃。 Here is code of equals button performing addition: 这是等号按钮执行加法的代码:

equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        result.getText();
       String expression = (result.getText()).toString();

        if(expression.contains("+")){
        String[] numbers = expression.split("\\+");
        String no1 = numbers[0];
        String no2 = numbers[1];

        Double numb1 = Double.valueOf(no1);
        Double numb2 = Double.valueOf(no2);
        Double added = numb1+numb2;

        theresult = String.valueOf(added);
        result.setText(theresult);


        }
    }
});

as you can see above, another problem arises. 如您在上面看到的,另一个问题出现了。 It cant handle more than one + sign, how can I make it work to add x amount of numbers? 它不能处理多个+号,我如何使它加上x个数字呢? Sorry I am new to programming, instead of just making it work, I want to learn how it will work so please explain too if possible. 抱歉,我刚开始编程,不只是想让它工作,而是想学习它如何工作,所以请尽可能解释一下。 Thanks. 谢谢。

利用堆栈进行多种操作,可以使用此算法http://www.smccd.net/accounts/hasson/C++2Notes/ArithmeticParsing.html

If its crashes then best idea is to post logcat error too. 如果它崩溃了,那么最好的办法就是也发布logcat错误。

Now the problem; 现在的问题; you are separating the expression based on + only. 您仅基于+分隔表达式。 so If you add - then the second part of the array would contain - also which can't be converted into double. 因此,如果添加-则数组的第二部分将包含-也不能转换为double。 Here you are getting the error. 在这里您得到错误。

So best idea is to separate the digit only part based on all the expressions such as + , - , * and / . 因此,最好的办法是根据所有表达式(例如+-*/将仅数字部分分开。 Once all the expression has been separated then only you can convert the remaining values to string and perform the action based on respective expression. 一旦所有表达式都被分离,则只有您可以将剩余的值转换为字符串并根据相应的表达式执行操作。

You will have to write some code for all this. 您将必须为此编写一些代码。

Now resources that could help you. 现在可以帮助您的资源。 (Second is shorter approach) (第二种是较短的方法)

  1. Evaluating a math expression given in string form 评估以字符串形式给出的数学表达式

  2. How to parse a mathematical expression given as a string and return a number? 如何解析以字符串形式给出的数学表达式并返回数字?

This is best suited as a comment, I decided to put it as an answer just to not get it lost since there are already some answers. 这最适合用作评论,我决定将其作为答案,以免丢失,因为已经有了一些答案。

Have a look at this question ; 看看这个问题 ; the accepted answer might not be what you want because you want to learn, but there is an answer by boan that is possibly what you are looking for. 接受的答案可能不是您想要的,因为您想学习,但是boan可能会提供您所需的答案。 I am not copying it here because it is already on SO 我不在这里复制它,因为它已经在SO上了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM