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