Create scxientific_calculator
Browse files- scxientific_calculator +95 -0
scxientific_calculator
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# Define a function for the scientific calculator
|
| 5 |
+
def scientific_calculator():
|
| 6 |
+
st.title("Scientific Calculator")
|
| 7 |
+
st.write("This is a simple scientific calculator with various functions.")
|
| 8 |
+
|
| 9 |
+
# Display the operations menu
|
| 10 |
+
operation = st.selectbox(
|
| 11 |
+
"Choose an operation:",
|
| 12 |
+
["Addition", "Subtraction", "Multiplication", "Division",
|
| 13 |
+
"Exponentiation", "Square Root", "Sine", "Cosine", "Tangent", "Logarithm"]
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
if operation == "Addition":
|
| 17 |
+
num1 = st.number_input("Enter first number:", value=0.0)
|
| 18 |
+
num2 = st.number_input("Enter second number:", value=0.0)
|
| 19 |
+
if st.button("Calculate"):
|
| 20 |
+
result = num1 + num2
|
| 21 |
+
st.write(f"Result: {result}")
|
| 22 |
+
|
| 23 |
+
elif operation == "Subtraction":
|
| 24 |
+
num1 = st.number_input("Enter first number:", value=0.0)
|
| 25 |
+
num2 = st.number_input("Enter second number:", value=0.0)
|
| 26 |
+
if st.button("Calculate"):
|
| 27 |
+
result = num1 - num2
|
| 28 |
+
st.write(f"Result: {result}")
|
| 29 |
+
|
| 30 |
+
elif operation == "Multiplication":
|
| 31 |
+
num1 = st.number_input("Enter first number:", value=0.0)
|
| 32 |
+
num2 = st.number_input("Enter second number:", value=0.0)
|
| 33 |
+
if st.button("Calculate"):
|
| 34 |
+
result = num1 * num2
|
| 35 |
+
st.write(f"Result: {result}")
|
| 36 |
+
|
| 37 |
+
elif operation == "Division":
|
| 38 |
+
num1 = st.number_input("Enter first number:", value=0.0)
|
| 39 |
+
num2 = st.number_input("Enter second number:", value=0.0)
|
| 40 |
+
if st.button("Calculate"):
|
| 41 |
+
if num2 != 0:
|
| 42 |
+
result = num1 / num2
|
| 43 |
+
st.write(f"Result: {result}")
|
| 44 |
+
else:
|
| 45 |
+
st.write("Error: Cannot divide by zero!")
|
| 46 |
+
|
| 47 |
+
elif operation == "Exponentiation":
|
| 48 |
+
base = st.number_input("Enter base number:", value=0.0)
|
| 49 |
+
exponent = st.number_input("Enter exponent:", value=0.0)
|
| 50 |
+
if st.button("Calculate"):
|
| 51 |
+
result = math.pow(base, exponent)
|
| 52 |
+
st.write(f"Result: {result}")
|
| 53 |
+
|
| 54 |
+
elif operation == "Square Root":
|
| 55 |
+
num = st.number_input("Enter number:", value=0.0)
|
| 56 |
+
if st.button("Calculate"):
|
| 57 |
+
if num >= 0:
|
| 58 |
+
result = math.sqrt(num)
|
| 59 |
+
st.write(f"Result: {result}")
|
| 60 |
+
else:
|
| 61 |
+
st.write("Error: Cannot take square root of negative number!")
|
| 62 |
+
|
| 63 |
+
elif operation == "Sine":
|
| 64 |
+
angle = st.number_input("Enter angle in degrees:", value=0.0)
|
| 65 |
+
if st.button("Calculate"):
|
| 66 |
+
angle_rad = math.radians(angle)
|
| 67 |
+
result = math.sin(angle_rad)
|
| 68 |
+
st.write(f"Result: {result}")
|
| 69 |
+
|
| 70 |
+
elif operation == "Cosine":
|
| 71 |
+
angle = st.number_input("Enter angle in degrees:", value=0.0)
|
| 72 |
+
if st.button("Calculate"):
|
| 73 |
+
angle_rad = math.radians(angle)
|
| 74 |
+
result = math.cos(angle_rad)
|
| 75 |
+
st.write(f"Result: {result}")
|
| 76 |
+
|
| 77 |
+
elif operation == "Tangent":
|
| 78 |
+
angle = st.number_input("Enter angle in degrees:", value=0.0)
|
| 79 |
+
if st.button("Calculate"):
|
| 80 |
+
angle_rad = math.radians(angle)
|
| 81 |
+
result = math.tan(angle_rad)
|
| 82 |
+
st.write(f"Result: {result}")
|
| 83 |
+
|
| 84 |
+
elif operation == "Logarithm":
|
| 85 |
+
num = st.number_input("Enter number:", value=0.0)
|
| 86 |
+
if st.button("Calculate"):
|
| 87 |
+
if num > 0:
|
| 88 |
+
result = math.log(num)
|
| 89 |
+
st.write(f"Result: {result}")
|
| 90 |
+
else:
|
| 91 |
+
st.write("Error: Logarithm undefined for non-positive numbers!")
|
| 92 |
+
|
| 93 |
+
# Call the scientific calculator function
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
scientific_calculator()
|