File size: 2,731 Bytes
1f04759
baec084
1f04759
baec084
1f04759
baec084
 
 
 
 
 
 
1f04759
 
baec084
 
1f04759
 
baec084
 
 
 
 
 
 
 
1f04759
baec084
1f04759
 
baec084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np

st.set_page_config(page_title="Scientific Calculator", page_icon="🧮", layout="centered")

st.title("🧮 Scientific Calculator")

# --- Session states for memory & history ---
if "memory" not in st.session_state:
    st.session_state.memory = 0
if "history" not in st.session_state:
    st.session_state.history = []

# Input fields
num1 = st.number_input("Enter first number:", format="%.6f")
num2 = st.number_input("Enter second number (optional for unary ops):", format="%.6f", value=0.0)

# Operation selection
operation = st.selectbox(
    "Select operation:",
    [
        "Add", "Subtract", "Multiply", "Divide",
        "Power", "Square Root (num1)", "Logarithm (num1)",
        "Sine (num1)", "Cosine (num1)", "Tangent (num1)"
    ]
)

# Perform calculation
result = None
if st.button("Calculate"):
    try:
        if operation == "Add":
            result = num1 + num2
        elif operation == "Subtract":
            result = num1 - num2
        elif operation == "Multiply":
            result = num1 * num2
        elif operation == "Divide":
            if num2 != 0:
                result = num1 / num2
            else:
                st.error("Division by zero is not allowed.")
        elif operation == "Power":
            result = num1 ** num2
        elif operation == "Square Root (num1)":
            result = np.sqrt(num1)
        elif operation == "Logarithm (num1)":
            if num1 > 0:
                result = np.log(num1)
            else:
                st.error("Logarithm is only defined for positive numbers.")
        elif operation == "Sine (num1)":
            result = np.sin(np.radians(num1))
        elif operation == "Cosine (num1)":
            result = np.cos(np.radians(num1))
        elif operation == "Tangent (num1)":
            result = np.tan(np.radians(num1))

        if result is not None:
            st.success(f"Result: {result}")
            st.session_state.history.append(f"{operation}: {result}")

    except Exception as e:
        st.error(f"Error: {str(e)}")

# --- Memory Functions ---
st.subheader("Memory Functions")
col1, col2, col3 = st.columns(3)

with col1:
    if st.button("M+"):
        if result is not None:
            st.session_state.memory += result
with col2:
    if st.button("M-"):
        if result is not None:
            st.session_state.memory -= result
with col3:
    if st.button("MC"):
        st.session_state.memory = 0

st.info(f"Memory: {st.session_state.memory}")

# --- History ---
st.subheader("Calculation History")
if st.session_state.history:
    for h in st.session_state.history[-10:][::-1]:  # show last 10
        st.write(h)
else:
    st.write("No calculations yet.")