File size: 2,510 Bytes
f20c848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")