Scientific Calculator Source Code In Java Free Download Official
// Inner class for expression evaluation using Shunting-yard algorithm private class ExpressionEvaluator public double evaluate(String expression) return evaluateExpression(expression); private double evaluateExpression(String expr) Stack<Double> values = new Stack<>(); Stack<Character> operators = new Stack<>(); for (int i = 0; i < expr.length(); i++) c == '.') StringBuilder sb = new StringBuilder(); while (i < expr.length() && (Character.isDigit(expr.charAt(i)) else if (c == '(') operators.push(c); else if (c == ')') while (operators.peek() != '(') values.push(applyOperation(operators.pop(), values.pop(), values.pop())); operators.pop(); else if (isOperator(c)) while (!operators.isEmpty() && hasPrecedence(c, operators.peek())) values.push(applyOperation(operators.pop(), values.pop(), values.pop())); operators.push(c); while (!operators.isEmpty()) values.push(applyOperation(operators.pop(), values.pop(), values.pop())); return values.pop(); private boolean isOperator(char c) private boolean hasPrecedence(char op1, char op2) op1 == '/' private double applyOperation(char op, double b, double a) switch (op) case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (b == 0) throw new ArithmeticException("Division by zero"); return a / b; case '%': return a % b; default: return 0;
public CalculatorEngine() this.memory = 0; scientific calculator source code in java free download
public String calculate(String expression, boolean isDegree) try Double.isInfinite(result)) return "Error"; // Round to 10 decimal places BigDecimal bd = new BigDecimal(result); bd = bd.setScale(10, RoundingMode.HALF_UP); // Remove trailing zeros String formatted = bd.toPlainString(); if (formatted.contains(".")) formatted = formatted.replaceAll("0*$", ""); formatted = formatted.replaceAll("\\.$", ""); return formatted; catch (Exception e) return "Error"; // Inner class for expression evaluation using Shunting-yard
public ScientificCalculator() engine = new CalculatorEngine(); initializeUI(); setTitle("Scientific Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); pack(); setLocationRelativeTo(null); private double evaluateExpression(String expr) Stack<
private JButton createStyledButton(String text)
public String calculateUnary(String operation, String value, boolean isDegree) try double num = Double.parseDouble(value); double result = 0; switch (operation) x return String.valueOf(result); catch (Exception e) return "Error";