Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# Title of the app
|
| 5 |
+
st.title("Scientific Calculator Application")
|
| 6 |
+
st.subheader("Perform advanced mathematical operations.")
|
| 7 |
+
|
| 8 |
+
# Operation selection
|
| 9 |
+
operation = st.selectbox(
|
| 10 |
+
"Select an operation:",
|
| 11 |
+
[
|
| 12 |
+
"Addition", "Subtraction", "Multiplication", "Division",
|
| 13 |
+
"Square Root", "Exponentiation", "Sine", "Cosine", "Tangent",
|
| 14 |
+
"Logarithm (Base 10)", "Logarithm (Natural)", "Factorial"
|
| 15 |
+
]
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Input fields for numbers
|
| 19 |
+
num1 = st.text_input("Enter the first number:")
|
| 20 |
+
|
| 21 |
+
if operation not in ["Sine", "Cosine", "Tangent", "Logarithm (Base 10)", "Logarithm (Natural)", "Factorial"]:
|
| 22 |
+
num2 = st.text_input("Enter the second number:")
|
| 23 |
+
|
| 24 |
+
# Button to trigger calculation
|
| 25 |
+
if st.button("Calculate"):
|
| 26 |
+
try:
|
| 27 |
+
# Handle different operations
|
| 28 |
+
if operation in ["Addition", "Subtraction", "Multiplication", "Division"]:
|
| 29 |
+
num1 = float(num1)
|
| 30 |
+
num2 = float(num2)
|
| 31 |
+
|
| 32 |
+
if operation == "Addition":
|
| 33 |
+
result = num1 + num2
|
| 34 |
+
st.success(f"The result of {num1} + {num2} is: {result}")
|
| 35 |
+
elif operation == "Subtraction":
|
| 36 |
+
result = num1 - num2
|
| 37 |
+
st.success(f"The result of {num1} - {num2} is: {result}")
|
| 38 |
+
elif operation == "Multiplication":
|
| 39 |
+
result = num1 * num2
|
| 40 |
+
st.success(f"The result of {num1} * {num2} is: {result}")
|
| 41 |
+
elif operation == "Division":
|
| 42 |
+
if num2 != 0:
|
| 43 |
+
result = num1 / num2
|
| 44 |
+
st.success(f"The result of {num1} / {num2} is: {result}")
|
| 45 |
+
else:
|
| 46 |
+
st.error("Error: Division by zero is not allowed!")
|
| 47 |
+
|
| 48 |
+
elif operation == "Square Root":
|
| 49 |
+
num1 = float(num1)
|
| 50 |
+
if num1 >= 0:
|
| 51 |
+
result = math.sqrt(num1)
|
| 52 |
+
st.success(f"The square root of {num1} is: {result}")
|
| 53 |
+
else:
|
| 54 |
+
st.error("Error: Square root of a negative number is not allowed!")
|
| 55 |
+
|
| 56 |
+
elif operation == "Exponentiation":
|
| 57 |
+
num1 = float(num1)
|
| 58 |
+
num2 = float(num2)
|
| 59 |
+
result = math.pow(num1, num2)
|
| 60 |
+
st.success(f"The result of {num1} ^ {num2} is: {result}")
|
| 61 |
+
|
| 62 |
+
elif operation == "Sine":
|
| 63 |
+
num1 = float(num1)
|
| 64 |
+
result = math.sin(math.radians(num1))
|
| 65 |
+
st.success(f"The sine of {num1}° is: {result}")
|
| 66 |
+
|
| 67 |
+
elif operation == "Cosine":
|
| 68 |
+
num1 = float(num1)
|
| 69 |
+
result = math.cos(math.radians(num1))
|
| 70 |
+
st.success(f"The cosine of {num1}° is: {result}")
|
| 71 |
+
|
| 72 |
+
elif operation == "Tangent":
|
| 73 |
+
num1 = float(num1)
|
| 74 |
+
result = math.tan(math.radians(num1))
|
| 75 |
+
st.success(f"The tangent of {num1}° is: {result}")
|
| 76 |
+
|
| 77 |
+
elif operation == "Logarithm (Base 10)":
|
| 78 |
+
num1 = float(num1)
|
| 79 |
+
if num1 > 0:
|
| 80 |
+
result = math.log10(num1)
|
| 81 |
+
st.success(f"The log10 of {num1} is: {result}")
|
| 82 |
+
else:
|
| 83 |
+
st.error("Error: Logarithm of non-positive numbers is undefined.")
|
| 84 |
+
|
| 85 |
+
elif operation == "Logarithm (Natural)":
|
| 86 |
+
num1 = float(num1)
|
| 87 |
+
if num1 > 0:
|
| 88 |
+
result = math.log(num1)
|
| 89 |
+
st.success(f"The natural logarithm of {num1} is: {result}")
|
| 90 |
+
else:
|
| 91 |
+
st.error("Error: Logarithm of non-positive numbers is undefined.")
|
| 92 |
+
|
| 93 |
+
elif operation == "Factorial":
|
| 94 |
+
num1 = int(num1)
|
| 95 |
+
if num1 >= 0:
|
| 96 |
+
result = math.factorial(num1)
|
| 97 |
+
st.success(f"The factorial of {num1} is: {result}")
|
| 98 |
+
else:
|
| 99 |
+
st.error("Error: Factorial is only defined for non-negative integers.")
|
| 100 |
+
|
| 101 |
+
except ValueError:
|
| 102 |
+
st.error("Invalid input! Please enter numeric values.")
|