amberroohee's picture
Create app.py
f20c848 verified
import streamlit as st
import math
st.set_page_config(page_title="Advanced Calculator", layout="centered")
st.title("Advanced Calculator App")
# Initialize memory
if "memory" not in st.session_state:
st.session_state.memory = 0.0
# =========================
# Expression Calculator
# =========================
st.subheader("Expression Calculator")
expression = st.text_input("Enter expression (e.g., 2 + 3 * 4)")
if st.button("Calculate Expression"):
try:
result = eval(expression)
st.success(f"Result: {result}")
except:
st.error("Invalid expression")
st.divider()
# =========================
# Standard Calculator
# =========================
st.subheader("Standard Operations")
operation = st.selectbox(
"Select Operation",
[
"Add",
"Subtract",
"Multiply",
"Divide",
"Power",
"Modulus",
"Square Root"
],
)
if operation != "Square Root":
num1 = st.number_input("Enter first number", value=0.0)
num2 = st.number_input("Enter second number", value=0.0)
else:
num1 = st.number_input("Enter number", value=0.0)
if st.button("Compute"):
try:
if operation == "Add":
result = num1 + num2
elif operation == "Subtract":
result = num1 - num2
elif operation == "Multiply":
result = num1 * num2
elif operation == "Divide":
result = num1 / num2
elif operation == "Power":
result = num1 ** num2
elif operation == "Modulus":
result = num1 % num2
elif operation == "Square Root":
result = math.sqrt(num1)
st.success(f"Result: {result}")
except ZeroDivisionError:
st.error("Cannot divide by zero")
except:
st.error("Error in calculation")
st.divider()
# =========================
# Memory Functions
# =========================
st.subheader("Memory Functions")
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("M+"):
st.session_state.memory += num1
st.success("Added to memory")
with col2:
if st.button("M-"):
st.session_state.memory -= num1
st.success("Subtracted from memory")
with col3:
if st.button("MR"):
st.info(f"Memory: {st.session_state.memory}")
with col4:
if st.button("MC"):
st.session_state.memory = 0.0
st.warning("Memory cleared")
st.divider()
st.caption("Built with Streamlit • Deploy on Hugging Face Spaces")