Spaces:
Sleeping
Sleeping
File size: 2,726 Bytes
3d454e4 | 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 | import streamlit as st
import math
# App title
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
st.title("🧮 Advanced Calculator")
st.write("An advanced calculator with percentage & history")
# Initialize history in session state
if "history" not in st.session_state:
st.session_state.history = []
# Select operation
operation = st.selectbox(
"Choose an operation",
[
"Addition (+)",
"Subtraction (-)",
"Multiplication (*)",
"Division (/)",
"Power (x^y)",
"Modulus (%)",
"Square Root (√x)",
"Percentage (x % of y)"
]
)
# Inputs
if operation == "Square Root (√x)":
num = st.number_input("Enter number", value=0.0)
elif operation == "Percentage (x % of y)":
x = st.number_input("Enter percentage value", value=0.0)
y = st.number_input("Enter total value", value=0.0)
else:
num1 = st.number_input("Enter first number", value=0.0)
num2 = st.number_input("Enter second number", value=0.0)
# Calculate button
if st.button("Calculate"):
try:
if operation == "Addition (+)":
result = num1 + num2
record = f"{num1} + {num2} = {result}"
elif operation == "Subtraction (-)":
result = num1 - num2
record = f"{num1} - {num2} = {result}"
elif operation == "Multiplication (*)":
result = num1 * num2
record = f"{num1} * {num2} = {result}"
elif operation == "Division (/)":
if num2 == 0:
st.error("Division by zero is not allowed")
st.stop()
result = num1 / num2
record = f"{num1} / {num2} = {result}"
elif operation == "Power (x^y)":
result = num1 ** num2
record = f"{num1} ^ {num2} = {result}"
elif operation == "Modulus (%)":
result = num1 % num2
record = f"{num1} % {num2} = {result}"
elif operation == "Square Root (√x)":
if num < 0:
st.error("Cannot calculate square root of negative number")
st.stop()
result = math.sqrt(num)
record = f"√{num} = {result}"
elif operation == "Percentage (x % of y)":
result = (x / 100) * y
record = f"{x}% of {y} = {result}"
# Display result
st.success(f"Result: {result}")
# Save history
st.session_state.history.append(record)
except Exception as e:
st.error(f"Error: {e}")
# Show history
st.subheader("📜 Calculation History")
if st.session_state.history:
for h in st.session_state.history:
st.write(h)
else:
st.info("No calculations yet")
|