Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="Advanced Calculator", layout="centered")
|
| 5 |
+
|
| 6 |
+
st.title("Advanced Calculator App")
|
| 7 |
+
|
| 8 |
+
# Initialize memory
|
| 9 |
+
if "memory" not in st.session_state:
|
| 10 |
+
st.session_state.memory = 0.0
|
| 11 |
+
|
| 12 |
+
# =========================
|
| 13 |
+
# Expression Calculator
|
| 14 |
+
# =========================
|
| 15 |
+
st.subheader("Expression Calculator")
|
| 16 |
+
expression = st.text_input("Enter expression (e.g., 2 + 3 * 4)")
|
| 17 |
+
|
| 18 |
+
if st.button("Calculate Expression"):
|
| 19 |
+
try:
|
| 20 |
+
result = eval(expression)
|
| 21 |
+
st.success(f"Result: {result}")
|
| 22 |
+
except:
|
| 23 |
+
st.error("Invalid expression")
|
| 24 |
+
|
| 25 |
+
st.divider()
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# Standard Calculator
|
| 29 |
+
# =========================
|
| 30 |
+
st.subheader("Standard Operations")
|
| 31 |
+
|
| 32 |
+
operation = st.selectbox(
|
| 33 |
+
"Select Operation",
|
| 34 |
+
[
|
| 35 |
+
"Add",
|
| 36 |
+
"Subtract",
|
| 37 |
+
"Multiply",
|
| 38 |
+
"Divide",
|
| 39 |
+
"Power",
|
| 40 |
+
"Modulus",
|
| 41 |
+
"Square Root"
|
| 42 |
+
],
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if operation != "Square Root":
|
| 46 |
+
num1 = st.number_input("Enter first number", value=0.0)
|
| 47 |
+
num2 = st.number_input("Enter second number", value=0.0)
|
| 48 |
+
else:
|
| 49 |
+
num1 = st.number_input("Enter number", value=0.0)
|
| 50 |
+
|
| 51 |
+
if st.button("Compute"):
|
| 52 |
+
try:
|
| 53 |
+
if operation == "Add":
|
| 54 |
+
result = num1 + num2
|
| 55 |
+
elif operation == "Subtract":
|
| 56 |
+
result = num1 - num2
|
| 57 |
+
elif operation == "Multiply":
|
| 58 |
+
result = num1 * num2
|
| 59 |
+
elif operation == "Divide":
|
| 60 |
+
result = num1 / num2
|
| 61 |
+
elif operation == "Power":
|
| 62 |
+
result = num1 ** num2
|
| 63 |
+
elif operation == "Modulus":
|
| 64 |
+
result = num1 % num2
|
| 65 |
+
elif operation == "Square Root":
|
| 66 |
+
result = math.sqrt(num1)
|
| 67 |
+
|
| 68 |
+
st.success(f"Result: {result}")
|
| 69 |
+
|
| 70 |
+
except ZeroDivisionError:
|
| 71 |
+
st.error("Cannot divide by zero")
|
| 72 |
+
except:
|
| 73 |
+
st.error("Error in calculation")
|
| 74 |
+
|
| 75 |
+
st.divider()
|
| 76 |
+
|
| 77 |
+
# =========================
|
| 78 |
+
# Memory Functions
|
| 79 |
+
# =========================
|
| 80 |
+
st.subheader("Memory Functions")
|
| 81 |
+
|
| 82 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 83 |
+
|
| 84 |
+
with col1:
|
| 85 |
+
if st.button("M+"):
|
| 86 |
+
st.session_state.memory += num1
|
| 87 |
+
st.success("Added to memory")
|
| 88 |
+
|
| 89 |
+
with col2:
|
| 90 |
+
if st.button("M-"):
|
| 91 |
+
st.session_state.memory -= num1
|
| 92 |
+
st.success("Subtracted from memory")
|
| 93 |
+
|
| 94 |
+
with col3:
|
| 95 |
+
if st.button("MR"):
|
| 96 |
+
st.info(f"Memory: {st.session_state.memory}")
|
| 97 |
+
|
| 98 |
+
with col4:
|
| 99 |
+
if st.button("MC"):
|
| 100 |
+
st.session_state.memory = 0.0
|
| 101 |
+
st.warning("Memory cleared")
|
| 102 |
+
|
| 103 |
+
st.divider()
|
| 104 |
+
|
| 105 |
+
st.caption("Built with Streamlit • Deploy on Hugging Face Spaces")
|