File size: 4,631 Bytes
c27be71
 
b38af1d
c27be71
1575d0d
12b95e8
d0b1c0e
12b95e8
 
 
d0b1c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b95e8
 
 
 
 
b38af1d
c27be71
d0b1c0e
12b95e8
d0b1c0e
12b95e8
d0b1c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b95e8
 
 
1575d0d
 
 
 
 
 
 
 
 
 
d0b1c0e
1575d0d
 
 
 
 
 
 
 
 
 
 
d0b1c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1575d0d
12b95e8
d0b1c0e
12b95e8
d0b1c0e
 
c27be71
1575d0d
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# app.py
import streamlit as st
import math

st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide")

# Custom CSS for a more professional look
st.markdown(
    """
    <style>
    body {
        font-family: 'Arial', sans-serif; /* Modern font */
        background-color: #f4f4f4; /* Light background */
    }
    .calculator-container {
        border: 1px solid #ddd;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);
        background-color: white;
    }
    .input-group {
        margin-bottom: 15px;
    }
    .input-group label {
        font-weight: bold;
        margin-bottom: 5px;
    }
    .stButton>button {
        background-color: #007bff; /* Blue button */
        color: white;
        padding: 12px 20px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        width: 100%; /* Full width button */
    }
    .stButton>button:hover {
        background-color: #0056b3; /* Darker blue on hover */
    }
    .result-area {
        margin-top: 20px;
        padding: 15px;
        border: 1px solid #ddd;
        border-radius: 5px;
        background-color: #f9f9f9;
        font-size: 1.2em;
        font-weight: bold;
    }
    .error-message {
        color: red;
        margin-top: 10px;
    }
    </style>
    """,
    unsafe_allow_html=True,
)

st.title("Enhanced Calculator")

with st.container() as calculator_container:  # Enclose in a container for styling

    col1, col2 = st.columns([1, 1])  # Two columns for inputs and operations

    with col1:
        st.subheader("Input Numbers")
        num1 = st.number_input("First Number", value=0.0)
        num2 = st.number_input("Second Number", value=0.0)
        num3 = st.number_input("Third Number", value=0.0)

    with col2:
        st.subheader("Select Operation")
        operation = st.selectbox(
            "Operation",
            [
                "+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average",
                "Logarithm",  # Added Logarithm
                "Sin", "Cos", "Tan",  # Added Trigonometric functions
                "Absolute Value", #Added Absolute Value function
                "Factorial" #Added Factorial function
            ],
        )

    if st.button("Calculate"):
        try:
            if operation == "+":
                result = num1 + num2 + num3
            elif operation == "-":
                result = num1 - num2 - num3
            elif operation == "*":
                result = num1 * num2 * num3
            elif operation == "/":
                if num2 == 0 or num3 == 0:
                    raise ZeroDivisionError("Division by zero is not allowed.")
                else:
                    result = num1 / num2 / num3
            elif operation == "Power":
                result = num1**num2
            elif operation == "Square Root":
                if num1 < 0:
                    raise ValueError("Square root of a negative number is not allowed.")
                else:
                    result = math.sqrt(num1)
            elif operation == "Cube Root":
                result = num1**(1/3)
            elif operation == "Average":
                result = (num1 + num2 + num3) / 3
            elif operation == "Logarithm":
                if num1 <= 0:
                    raise ValueError("Logarithm of a non-positive number is not allowed.")
                else:
                    result = math.log(num1)
            elif operation == "Sin":
                result = math.sin(math.radians(num1)) # Convert to radians
            elif operation == "Cos":
                result = math.cos(math.radians(num1)) # Convert to radians
            elif operation == "Tan":
                result = math.tan(math.radians(num1)) # Convert to radians
            elif operation == "Absolute Value":
                result = abs(num1)
            elif operation == "Factorial":
                if num1 < 0 or not num1.is_integer():
                    raise ValueError("Factorial is only defined for non-negative integers.")
                else:
                     result = math.factorial(int(num1))

            st.markdown(f"<div class='result-area'>Result: {result}</div>", unsafe_allow_html=True)

        except (ZeroDivisionError, ValueError) as e:
            st.markdown(f"<div class='error-message'>{str(e)}</div>", unsafe_allow_html=True)
        except Exception as e:
            st.markdown(f"<div class='error-message'>An error occurred: {e}</div>", unsafe_allow_html=True)


st.markdown("---")
st.write("Made with Streamlit")