Spaces:
Sleeping
Sleeping
File size: 3,725 Bytes
14dbf7a 2921b04 14dbf7a 2921b04 14dbf7a 2921b04 14dbf7a 2921b04 14dbf7a 2921b04 572616b bd38b70 |
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 |
import streamlit as st
import math
# Title of the application
st.title('Advanced Calculator')
# Sidebar for choosing operation
operation = st.sidebar.selectbox(
"Choose operation",
("Basic", "Advanced", "Trigonometric", "Logarithmic", "Factorial", "Percentage", "Memory")
)
# User input for numbers
num1 = st.number_input('Enter first number', value=0.0)
num2 = st.number_input('Enter second number (optional)', value=0.0)
# Memory variable (to store previous result)
if 'memory' not in st.session_state:
st.session_state.memory = 0.0
# Basic Operations
if operation == "Basic":
basic_op = st.selectbox("Choose basic operation", ("Add", "Subtract", "Multiply", "Divide"))
if basic_op == "Add":
result = num1 + num2
elif basic_op == "Subtract":
result = num1 - num2
elif basic_op == "Multiply":
result = num1 * num2
elif basic_op == "Divide":
if num2 != 0:
result = num1 / num2
else:
result = "Error: Division by Zero"
# Advanced Operations
elif operation == "Advanced":
advanced_op = st.selectbox("Choose advanced operation", ("Power", "Square Root"))
if advanced_op == "Power":
result = num1 ** num2
elif advanced_op == "Square Root":
if num1 >= 0:
result = math.sqrt(num1)
else:
result = "Error: Cannot take square root of negative number"
# Trigonometric Operations
elif operation == "Trigonometric":
trig_op = st.selectbox("Choose trigonometric function", ("Sine", "Cosine", "Tangent"))
angle_in_degrees = st.checkbox("Use degrees instead of radians?")
if trig_op == "Sine":
if angle_in_degrees:
result = math.sin(math.radians(num1))
else:
result = math.sin(num1)
elif trig_op == "Cosine":
if angle_in_degrees:
result = math.cos(math.radians(num1))
else:
result = math.cos(num1)
elif trig_op == "Tangent":
if angle_in_degrees:
result = math.tan(math.radians(num1))
else:
result = math.tan(num1)
# Logarithmic Operations
elif operation == "Logarithmic":
log_op = st.selectbox("Choose logarithmic operation", ("Log Base 10", "Natural Log"))
if log_op == "Log Base 10":
if num1 > 0:
result = math.log10(num1)
else:
result = "Error: Logarithm only defined for positive numbers"
elif log_op == "Natural Log":
if num1 > 0:
result = math.log(num1)
else:
result = "Error: Logarithm only defined for positive numbers"
# Factorial
elif operation == "Factorial":
if num1 >= 0 and num1 == int(num1):
result = math.factorial(int(num1))
else:
result = "Error: Factorial is only defined for non-negative integers"
# Percentage Calculation
elif operation == "Percentage":
percentage_op = st.selectbox("Choose percentage operation", ("Find Percentage", "Find Percentage of"))
if percentage_op == "Find Percentage":
result = (num1 * num2) / 100
elif percentage_op == "Find Percentage of":
result = (num1 * 100) / num2
# Memory Feature
elif operation == "Memory":
memory_op = st.selectbox("Choose memory operation", ("Store Result", "Recall Result", "Clear Memory"))
if memory_op == "Store Result":
st.session_state.memory = result
result = f"Result stored in memory: {st.session_state.memory}"
elif memory_op == "Recall Result":
result = f"Memory contains: {st.session_state.memory}"
elif memory_op == "Clear Memory":
st.session_state.memory = 0.0
result = "Memory cleared"
# Display the result
st.write(f"Result: {result}")
|