File size: 2,686 Bytes
d35afe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import math

# Initialize history in session state
if "history" not in st.session_state:
    st.session_state.history = []

st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")

st.title("🧮 Advanced Calculator")
st.write("Select an operation from the menu below.")

# Menu
operation = st.selectbox(
    "Choose an operation",
    (
        "Addition",
        "Subtraction",
        "Multiplication",
        "Division",
        "Power (x^y)",
        "Modulus",
        "Square Root",
        "Percentage",
        "Floor Division"
    )
)

# Two-number operations
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:
    num = st.number_input("Enter number", value=0.0)

if st.button("Calculate"):

    if operation == "Addition":
        result = num1 + num2
        output = f"{num1} + {num2} = {result}"

    elif operation == "Subtraction":
        result = num1 - num2
        output = f"{num1} - {num2} = {result}"

    elif operation == "Multiplication":
        result = num1 * num2
        output = f"{num1} × {num2} = {result}"

    elif operation == "Division":
        if num2 != 0:
            result = num1 / num2
            output = f"{num1} ÷ {num2} = {result}"
        else:
            output = "❌ Error! Division by zero."

    elif operation == "Power (x^y)":
        result = num1 ** num2
        output = f"{num1} ^ {num2} = {result}"

    elif operation == "Modulus":
        result = num1 % num2
        output = f"{num1} % {num2} = {result}"

    elif operation == "Percentage":
        if num2 != 0:
            result = (num1 / num2) * 100
            output = f"{num1} is {result}% of {num2}"
        else:
            output = "❌ Error! Cannot divide by zero."

    elif operation == "Floor Division":
        if num2 != 0:
            result = num1 // num2
            output = f"{num1} // {num2} = {result}"
        else:
            output = "❌ Error! Division by zero."

    elif operation == "Square Root":
        if num >= 0:
            result = math.sqrt(num)
            output = f"√{num} = {result}"
        else:
            output = "❌ Error! Cannot take square root of negative number."

    st.success(output)
    st.session_state.history.append(output)


# History Section
st.subheader("📜 Calculation History")

if st.session_state.history:
    for item in st.session_state.history:
        st.write(item)
else:
    st.write("No calculations yet.")

# Clear History Button
if st.button("Clear History"):
    st.session_state.history = []
    st.success("History Cleared!")