Scientific - Doc Calculator
Scientific - Doc Calculator
Scientific - Doc Calculator
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class publiccalculator extends Applet implements ActionListener{
setLayout(null);
resize(500,400);
add(lblcasio);
add(lblfx);
add(lblnatural);
add(txtfield1);
add(abs);
add(X2);
add(X3);
add(log);
add(sqrt);
add(sin);
add(cos);
add(tan);
add(pie);
add(E);
add(seven);
add(eight);
add(nine);
add(DEL);
add(AC);
add(four);
add(five);
add(six);
add(multiply);
add(subtract);
add(one);
add(two);
add(three);
add(plus);
add(dive);
add(zero);
add(equals);
add(dot);
add(round);
lblcasio.setBounds(60,0,100,20);
lblfx.setBounds(350,0,150,20);
lblnatural.setBounds(160,25,200,20);
txtfield1.setBounds(100,50,240,20);
abs.setBounds(100,75,35,35);
X2.setBounds(150,75,35,35);
X3.setBounds(200,75,35,35);
log.setBounds(250,75,35,35);
sqrt.setBounds(300,75,35,35);
sin.setBounds(100,120,35,35);
cos.setBounds(150,120,35,35);
tan.setBounds(200,120,35,35);
pie.setBounds(250,120,35,35);
E.setBounds(300,120,35,35);
seven.setBounds(100,165,35,35);
eight.setBounds(150,165,35,35);
nine.setBounds(200,165,35,35);
AC.setBounds(250,165,85,35);
four.setBounds(100,210,35,35);
five.setBounds(150,210,35,35);
six.setBounds(200,210,35,35);
multiply.setBounds(250,210,35,35);
subtract.setBounds(300,210,35,35);
one.setBounds(100,255,35,35);
two.setBounds(150,255,35,35);
three.setBounds(200,255,35,35);
plus.setBounds(250,255,35,35);
dive.setBounds(300,255,35,35);
zero.setBounds(100,300,35,35);
equals.setBounds(150,300,85,35);
dot.setBounds(250,300,35,35);
round.setBounds(300,300,35,35);
equals.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
plus.addActionListener(this);
dive.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
multiply.addActionListener(this);
subtract.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
sin.addActionListener(this);
cos.addActionListener(this);
tan.addActionListener(this);
pie.addActionListener(this);
E.addActionListener(this);
abs.addActionListener(this);
X2.addActionListener(this);
X3.addActionListener(this);
log.addActionListener(this);
sqrt.addActionListener(this);
zero.addActionListener(this);
dot.addActionListener(this);
round.addActionListener(this);
txtfield1.setEnabled(false);
}
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.EmptyStackException;
import java.util.Stack;
import java.lang.Math;
public ScientificCalculator() {
frame = new JFrame("Scientific Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new BorderLayout());
String[] buttons = {
"7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "x^2",
"1", "2", "3", "-", "x^y",
"0", ".", "+/-", "+", "=",
"sin", "cos", "tan", "log", "ln"
};
frame.add(buttonPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
if ("0123456789.".contains(command)) {
input += command;
} else if ("+-*/".contains(command)) {
input += " " + command + " ";
} else if ("sqrt x^2 x^y sin cos tan log ln".contains(command)) {
input = command + "(" + input + ")";
} else if ("=".equals(command)) {
try {
input = evaluateExpression(input);
} catch (ArithmeticException ex) {
input = "Error";
}
} else if ("+/-".equals(command)) {
input = negateInput(input);
}
display.setText(input);
}
Code
import java.util.Scanner;
while (!exit) {
if (newCalculation) {
System.out.println("Scientific Calculator Menu:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Square Root");
System.out.println("6. Exponentiation");
System.out.println("7. Sine");
System.out.println("8. Cosine");
System.out.println("9. Tangent");
System.out.println("10. Logarithm (base 10)");
System.out.println("11. Natural Logarithm (ln)");
System.out.println("12. Memory Operations");
System.out.println("13. Exit");
System.out.print("Select an operation (1-13): ");
} else {
System.out.println("Result: " + result);
System.out.print("Select an operation (1-13 or 0 for new calculation): ");
}
switch (choice) {
case 0:
newCalculation = true;
break;
case 1:
System.out.print("Enter the number to add: ");
operand = scanner.nextDouble();
result += operand;
newCalculation = false;
break;
case 2:
System.out.print("Enter the number to subtract: ");
operand = scanner.nextDouble();
result -= operand;
newCalculation = false;
break;
case 3:
System.out.print("Enter the number to multiply by: ");
operand = scanner.nextDouble();
result *= operand;
newCalculation = false;
break;
case 4:
System.out.print("Enter the number to divide by: ");
operand = scanner.nextDouble();
if (operand != 0) {
result /= operand;
} else {
System.out.println("Error: Division by zero.");
}
newCalculation = false;
break;
case 5:
result = Math.sqrt(result);
newCalculation = false;
break;
case 6:
System.out.print("Enter the exponent: ");
operand = scanner.nextDouble();
result = Math.pow(result, operand);
newCalculation = false;
break;
case 7:
result = Math.sin(result);
newCalculation = false;
break;
case 8:
result = Math.cos(result);
newCalculation = false;
break;
case 9:
result = Math.tan(result);
newCalculation = false;
break;
case 10:
if (result > 0) {
result = Math.log10(result);
} else {
System.out.println("Error: Invalid input for logarithm.");
}
newCalculation = false;
break;
case 11:
if (result > 0) {
result = Math.log(result);
} else {
System.out.println("Error: Invalid input for natural logarithm.");
}
newCalculation = false;
break;
case 12:
System.out.println("Memory Menu:");
System.out.println("1. Store result in memory");
System.out.println("2. Recall memory");
System.out.println("3. Clear memory");
System.out.print("Select a memory operation (1-3): ");
int memoryChoice = scanner.nextInt();
switch (memoryChoice) {
case 1:
memory = result;
break;
case 2:
result = memory;
break;
case 3:
memory = 0;
break;
default:
System.out.println("Invalid memory operation.");
break;
}
break;
case 13:
exit = true;
break;
default:
System.out.println("Invalid choice.");
break;
}
}
System.out.println("Calculator closed.");
}
}
Output
Scientific Calculator Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square Root
6. Exponentiation
7. Sine
8. Cosine
9. Tangent
10. Logarithm (base 10)
11. Natural Logarithm (ln)
12. Memory Operations
13. Exit
Select an operation (1-13): 1
Enter the number to add: 5
Result: 5.0
Select an operation (1-13 or 0 for new calculation): 3
Enter the number to multiply by: 2
Result: 10.0
Select an operation (1-13 or 0 for new calculation): 7
Result: -0.5440211108893699
Select an operation (1-13 or 0 for new calculation): 12
Memory Menu:
1. Store result in memory
2. Recall memory
3. Clear memory
Select a memory operation (1-3): 1
Select an operation (1-13 or 0 for new calculation): 0
Scientific Calculator Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square Root
6. Exponentiation
7. Sine
8. Cosine
9. Tangent
10. Logarithm (base 10)
11. Natural Logarithm (ln)
12. Memory Operations
13. Exit
Select an operation (1-13 or 0 for new calculation): 2
Enter the number to subtract: 3
Result: 7.0
Select an operation (1-13 or 0 for new calculation): 13
Calculator closed.
<body>
<h1 style="color: green; text-align: center;">
Scientific Calculator</h1>
<form>
<table>
<tr>
<td colspan="6">
<input id="display" type="text">
</td>
</tr>
<tr>
<td><input type="button" value="1"
onclick="display.value += '1'"></td>
<td><input type="button" value="2"
onclick="display.value += '2'"></td>
<td><input type="button" value="3"
onclick="display.value += '3'"></td>
<td><input type="button" value="C"
onclick="display.value = ''"></td>
<td><input type="button" value="⌫"
onclick="backspace()"></td>
<td><input type="button" value="="
onclick="calculate()"></td>
</tr>
<tr>
<td><input type="button" value="4"
onclick="display.value += '4'"></td>
<td><input type="button" value="5"
onclick="display.value += '5'"></td>
<td><input type="button" value="6"
onclick="display.value += '6'"></td>
<td><input type="button" value="-"
onclick="display.value += '-'"></td>
<td><input type="button" value="%"
onclick="display.value += '%'"></td>
<td><input type="button" value="cos("
onclick="display.value += 'cos('"></td>
</tr>
<tr>
<td><input type="button" value="7"
onclick="display.value += '7'"></td>
<td><input type="button" value="8"
onclick="display.value += '8'"></td>
<td><input type="button" value="9"
onclick="display.value += '9'"></td>
<td><input type="button" value="x"
onclick="display.value += '*'"></td>
<td><input type="button" value="!"
onclick="display.value += '!'"></td>
<td><input type="button" value="sin("
onclick="display.value += 'sin('"></td>
</tr>
<tr>
<td><input type="button" value="."
onclick="display.value += '.'"></td>
<td><input type="button" value="0"
onclick="display.value += '0'"></td>
<td><input type="button" value=","
onclick="display.value += ','"></td>
<td><input type="button" value="+"
onclick="display.value += '+'"></td>
<td><input type="button" value="/"
onclick="display.value += '/'"></td>
<td><input type="button" value="tan("
onclick="display.value += 'tan('"></td>
</tr>
<tr>
<td><input type="button" value="E"
onclick="e()"></td>
<td><input type="button" value="pi"
onclick="pi()"></td>
<td><input type="button" value="^"
onclick="power()"></td>
<td><input type="button" value="("
onclick="display.value += '('"></td>
<td><input type="button" value=")"
onclick="display.value += ')'"></td>
<td><input type="button" value="log("
onclick="base10Log()"></td>
</tr>
<tr>
<td><input type="button" value="sqrt("
onclick="squareRoot()"></td>
<td><input type="button" value="ln2"
onclick="display.value += Math.LN2"></td>
<td><input type="button" value="log10("
onclick="base10Log()"></td>
<td><input type="button" value="l2e"
onclick="display.value += Math.LOG2E"></td>
<td><input type="button" value="l10e"
onclick="display.value += Math.LOG10E"></td>
<td><input type="button" value="exp("
onclick="display.value += 'exp('"></td>
</tr>
</table>
</form>
</body>
</html>
CSS
table {
border: 1px solid black;
margin-left: auto;
margin-right: auto;
}
input[type="button"] {
width: 100%;
padding: 20px 40px;
background-color: green;
color: white;
font-size: 24px;
font-weight: bold;
border: none;
border-radius: 5px;
}
input[type="text"] {
padding: 20px 240px;
font-size: 24px;
font-weight: bold;
border: none;
border-radius: 5px;
border: 2px solid black;
text-align: left;
}
display {
text-align: left;
}
Javascript
function backspace() {
let display = document.getElementById("display");
display.value = display.value.slice(0, -1);
}
function calculate() {
let display = document.getElementById("display");
let expression = display.value;
let result;
try {
// Convert trigonometric function inputs from degrees to radians
expression = expression.replace(/sin\(/g, 'sin(' + Math.PI / 180 +
'*');
expression = expression.replace(/cos\(/g, 'cos(' + Math.PI / 180 +
'*');
expression = expression.replace(/tan\(/g, 'tan(' + Math.PI / 180 +
'*');
result = math.evaluate(expression);
display.value = result;
} catch (error) {
display.value = "Error";
}
}
function squareRoot() {
let display = document.getElementById("display");
display.value += "sqrt(";
}
function base10Log() {
let display = document.getElementById("display");
display.value += "log(";
}
function pi() {
let display = document.getElementById("display");
display.value += "pi";
}
function e() {
let display = document.getElementById("display");
display.value += "e";
}
function power() {
let display = document.getElementById("display");
display.value += "^(";
}