File size: 3,445 Bytes
d1cc000
a96c0f4
d1cc000
5f79cea
 
cd6a774
5f79cea
0afd21b
a96c0f4
1c6887d
5f79cea
0afd21b
5f79cea
0afd21b
 
5f79cea
0afd21b
5f79cea
 
 
 
 
 
a96c0f4
5f79cea
 
 
0afd21b
 
1c6887d
 
5f79cea
 
 
 
 
0afd21b
5f79cea
 
 
 
 
 
 
 
 
a96c0f4
 
0afd21b
cd6a774
5f79cea
cd6a774
 
5f79cea
 
1c6887d
 
a96c0f4
cd6a774
 
a96c0f4
cd6a774
 
a96c0f4
cd6a774
1c6887d
 
 
5f79cea
 
 
1c6887d
5f79cea
 
cd6a774
 
 
 
5f79cea
 
 
 
 
 
 
 
 
 
 
 
 
 
0afd21b
5f79cea
 
0afd21b
 
 
 
5f79cea
cd6a774
 
0afd21b
1c6887d
 
 
5f79cea
1c6887d
5f79cea
d1cc000
1c6887d
0afd21b
5f79cea
 
 
 
 
 
 
 
0afd21b
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import streamlit as st
import math

# Page config
st.set_page_config(page_title="Scientific Calculator", layout="centered")

# --- CSS Styling ---
st.markdown("""
    <style>
    .calculator-container {
        background-color: #f9f9f9;
        padding: 20px;
        border-radius: 16px;
        max-width: 400px;
        margin: auto;
        box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    .display {
        background-color: #ffffff;
        color: #222;
        font-size: 28px;
        padding: 10px 15px;
        border-radius: 10px;
        text-align: right;
        border: 1px solid #ddd;
        margin-bottom: 10px;
        height: 60px;
        overflow-x: auto;
    }
    .stButton > button {
        width: 100%;
        height: 40px;
        font-size: 14px;
        padding: 0;
        margin: 2px 0;
        border-radius: 8px;
    }
    .history-box {
        background: #f0f0f0;
        border-radius: 8px;
        padding: 10px;
        max-height: 150px;
        overflow-y: auto;
        font-size: 14px;
        margin-top: 20px;
        color: #444;
    }
    </style>
""", unsafe_allow_html=True)

# --- State ---
if "expression" not in st.session_state:
    st.session_state.expression = ""
if "history" not in st.session_state:
    st.session_state.history = []

# --- Functions ---
def add(symbol):
    st.session_state.expression += str(symbol)

def clear():
    st.session_state.expression = ""

def calculate():
    try:
        expr = st.session_state.expression
        expr = expr.replace("^", "**").replace("√", "math.sqrt")
        expr = expr.replace("π", str(math.pi)).replace("e", str(math.e))
        expr = expr.replace("ln", "math.log").replace("log", "math.log10")
        for func in ["sin", "cos", "tan"]:
            expr = expr.replace(func, f"math.{func}")
        result = eval(expr)
        result_str = f"{st.session_state.expression} = {result}"
        st.session_state.history.insert(0, result_str)
        st.session_state.expression = str(result)
    except:
        st.session_state.expression = "Error"

def handle_input():
    expr = st.session_state["input_expr"]
    st.session_state.expression = expr

# --- Layout ---
st.markdown("<div class='calculator-container'>", unsafe_allow_html=True)

# Input via keyboard
st.text_input("Type your expression (keyboard supported):", value=st.session_state.expression, key="input_expr", on_change=handle_input)

# Display area
st.markdown(f"<div class='display'>{st.session_state.expression}</div>", unsafe_allow_html=True)

# Button layout
button_rows = [
    ["sin(", "cos(", "tan(", "√("],
    ["log(", "ln(", "π", "e"],
    ["7", "8", "9", "/"],
    ["4", "5", "6", "*"],
    ["1", "2", "3", "-"],
    ["0", ".", "^", "+"],
    ["(", ")", "C", "="],
]

for row in button_rows:
    cols = st.columns(4)
    for i, label in enumerate(row):
        if label == "=":
            cols[i].button(label, on_click=calculate)
        elif label == "C":
            cols[i].button(label, on_click=clear)
        else:
            cols[i].button(label, on_click=add, args=(label,))

# History section
if st.session_state.history:
    st.markdown("<h5>History</h5>", unsafe_allow_html=True)
    st.markdown("<div class='history-box'>", unsafe_allow_html=True)
    for entry in st.session_state.history[:10]:
        st.markdown(f"- {entry}")
    st.markdown("</div>", unsafe_allow_html=True)

st.markdown("</div>", unsafe_allow_html=True)