Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
|
| 5 |
+
|
| 6 |
+
st.title("🧮 Advanced Calculator")
|
| 7 |
+
st.write("A simple and powerful calculator built with Streamlit")
|
| 8 |
+
|
| 9 |
+
# Sidebar for mode selection
|
| 10 |
+
mode = st.sidebar.selectbox(
|
| 11 |
+
"Select Calculator Mode",
|
| 12 |
+
["Basic", "Scientific", "Expression"]
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# ---------------- BASIC CALCULATOR ----------------
|
| 16 |
+
if mode == "Basic":
|
| 17 |
+
st.subheader("Basic Calculator")
|
| 18 |
+
|
| 19 |
+
num1 = st.number_input("Enter first number", value=0.0)
|
| 20 |
+
num2 = st.number_input("Enter second number", value=0.0)
|
| 21 |
+
|
| 22 |
+
operation = st.selectbox(
|
| 23 |
+
"Choose operation",
|
| 24 |
+
["+", "-", "*", "/", "**", "%"]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if st.button("Calculate"):
|
| 28 |
+
try:
|
| 29 |
+
if operation == "+":
|
| 30 |
+
result = num1 + num2
|
| 31 |
+
elif operation == "-":
|
| 32 |
+
result = num1 - num2
|
| 33 |
+
elif operation == "*":
|
| 34 |
+
result = num1 * num2
|
| 35 |
+
elif operation == "/":
|
| 36 |
+
if num2 == 0:
|
| 37 |
+
st.error("Division by zero is not allowed")
|
| 38 |
+
result = None
|
| 39 |
+
else:
|
| 40 |
+
result = num1 / num2
|
| 41 |
+
elif operation == "**":
|
| 42 |
+
result = num1 ** num2
|
| 43 |
+
elif operation == "%":
|
| 44 |
+
result = num1 % num2
|
| 45 |
+
|
| 46 |
+
if result is not None:
|
| 47 |
+
st.success(f"Result: {result}")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error("Something went wrong")
|
| 51 |
+
|
| 52 |
+
# ---------------- SCIENTIFIC CALCULATOR ----------------
|
| 53 |
+
elif mode == "Scientific":
|
| 54 |
+
st.subheader("Scientific Calculator")
|
| 55 |
+
|
| 56 |
+
number = st.number_input("Enter a number", value=0.0)
|
| 57 |
+
|
| 58 |
+
operation = st.selectbox(
|
| 59 |
+
"Choose function",
|
| 60 |
+
["sin", "cos", "tan", "sqrt", "log", "exp"]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if st.button("Calculate"):
|
| 64 |
+
try:
|
| 65 |
+
if operation == "sin":
|
| 66 |
+
result = math.sin(math.radians(number))
|
| 67 |
+
elif operation == "cos":
|
| 68 |
+
result = math.cos(math.radians(number))
|
| 69 |
+
elif operation == "tan":
|
| 70 |
+
result = math.tan(math.radians(number))
|
| 71 |
+
elif operation == "sqrt":
|
| 72 |
+
if number < 0:
|
| 73 |
+
st.error("Cannot calculate square root of negative number")
|
| 74 |
+
result = None
|
| 75 |
+
else:
|
| 76 |
+
result = math.sqrt(number)
|
| 77 |
+
elif operation == "log":
|
| 78 |
+
if number <= 0:
|
| 79 |
+
st.error("Logarithm input must be positive")
|
| 80 |
+
result = None
|
| 81 |
+
else:
|
| 82 |
+
result = math.log(number)
|
| 83 |
+
elif operation == "exp":
|
| 84 |
+
result = math.exp(number)
|
| 85 |
+
|
| 86 |
+
if result is not None:
|
| 87 |
+
st.success(f"Result: {result}")
|
| 88 |
+
|
| 89 |
+
except Exception:
|
| 90 |
+
st.error("Invalid calculation")
|
| 91 |
+
|
| 92 |
+
# ---------------- EXPRESSION CALCULATOR ----------------
|
| 93 |
+
elif mode == "Expression":
|
| 94 |
+
st.subheader("Expression Calculator")
|
| 95 |
+
st.write("Example: `3 + 5 * (2 - 1) ** 2`")
|
| 96 |
+
|
| 97 |
+
expression = st.text_input("Enter expression")
|
| 98 |
+
|
| 99 |
+
if st.button("Evaluate"):
|
| 100 |
+
try:
|
| 101 |
+
allowed_chars = "0123456789+-*/().% "
|
| 102 |
+
if all(char in allowed_chars for char in expression):
|
| 103 |
+
result = eval(expression)
|
| 104 |
+
st.success(f"Result: {result}")
|
| 105 |
+
else:
|
| 106 |
+
st.error("Invalid characters in expression")
|
| 107 |
+
|
| 108 |
+
except Exception:
|
| 109 |
+
st.error("Invalid expression")
|
| 110 |
+
|
| 111 |
+
st.markdown("---")
|
| 112 |
+
st.caption("Built with Streamlit • Deployable on Hugging Face Spaces")
|