如何在计算器中添加平方根函数?

时间:2016-05-18 10:05:34

标签: android calculator square-root

我为Android制作了这个计算器应用程序。它接收一个数字,运算符然后是另一个数字。我已经尝试添加一个平方根函数,但它要么不起作用,要么崩溃。主要问题是在数字之前不能输入运算符,并且平方根运算符需要后面的数字,所以我现在删除了平方根。我怎样才能解决这个问题?我在下面添加了我的代码.public class Calculator扩展了AppCompatActivity {

private TextView _screen;
private String display = "";
private String currentOperator = "";
private String result = "";

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

    _screen = (TextView) findViewById(R.id.textView);
    _screen.setText(display);
}

private void updateScreen() {

    _screen.setText(display);

}

public void onClickNumber(View view) {

    if (result != "") {

        clear();
        updateScreen();

    }

    Button b = (Button) view;
    display += b.getText();
    updateScreen();

}

public void onClickDecimal(View view) {

    if (result != "") {

        clear();
        updateScreen();

    }

    Button b = (Button) view;
    display += b.getText();
    updateScreen();

}

public void onClickBackspace(View view) {

    int length = display.length();

    if (length >= 1) {

        display = display.substring(0, length - 1);
        updateScreen();

    }

    else {

        Toast toast = Toast.makeText(this, "Please enter some numbers first", Toast.LENGTH_SHORT);
        toast.show();

    }

}

private boolean isOperator(char op) {

    switch (op) {

        case '+':
            return true;

        case '-':
            return true;

        case 'x':
            return true;

        case '÷':
            return true;

        case '%':
            return true;

        case '^':
            return true;

        case '√':
            return true;

        default:
            return false;

    }

}

public void onClickOperator(View view) {

    if (display == "") return;

    Button b = (Button) view;


    if (result != "") {

        String _display = result;
        clear();
        display = _display;

    }

    if (currentOperator != "") {

        if (isOperator(display.charAt(display.length() - 1))) {

            display = display.replace(display.charAt(display.length() - 1), b.getText().charAt(0));
            updateScreen();
            return;

        }

        else {

            getResult();
            display = result;
            result = "";

        }

        currentOperator = b.getText().toString();

    }

    display += b.getText();
    currentOperator = b.getText().toString();
    updateScreen();

}

private void clear() {

    display = "";
    currentOperator = "";
    result = "";

}

public void onClickClear(View view) {

    clear();
    updateScreen();

}

private double operate(String a, String b, String op) {

    switch (op) {

        case "+":
            return Double.valueOf(a) + Double.valueOf(b);

        case "-":
            return Double.valueOf(a) - Double.valueOf(b);

        case "x":
            return Double.valueOf(a) * Double.valueOf(b);

        case "÷":
            return Double.valueOf(a) / Double.valueOf(b);

        case "%":
            return Double.valueOf(a) % Double.valueOf(b);

        case "^":
            return Math.pow(Double.valueOf(a), Double.valueOf(b));

        case "√":
            return Math.sqrt(Double.valueOf(a));

        default:
            return -1;


    }

}

private boolean getResult() {

    DecimalFormat res = new DecimalFormat("#,###,###,###.######");

    if (currentOperator == "") return false;

    String[] operation = display.split(Pattern.quote(currentOperator));

    if (operation.length < 2) return false;

            result = String.valueOf(res.format(operate(operation[0], operation[1], currentOperator)));

    return true;

}

public void onClickEquals(View view) {

    int length = result.length();

    if (display == "") return;

    if(!getResult()) return;

    if (length <= 10) {

        _screen.setText(String.valueOf(result));

    }

    else {

        result = result.substring(0, 9);
        _screen.setText(String.valueOf(result));

    }

}`

1 个答案:

答案 0 :(得分:0)

您可以使用: -

double x = 2.45;
System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));

结果: -

sqrt(2.45) is 1.565