Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import math | |
| st.set_page_config(page_title="Advanced Calculator", page_icon="🧮") | |
| st.title("🧮 Advanced Calculator") | |
| st.write("A simple and powerful calculator built with Streamlit") | |
| # Sidebar for mode selection | |
| mode = st.sidebar.selectbox( | |
| "Select Calculator Mode", | |
| ["Basic", "Scientific", "Expression"] | |
| ) | |
| # ---------------- BASIC CALCULATOR ---------------- | |
| if mode == "Basic": | |
| st.subheader("Basic Calculator") | |
| num1 = st.number_input("Enter first number", value=0.0) | |
| num2 = st.number_input("Enter second number", value=0.0) | |
| operation = st.selectbox( | |
| "Choose operation", | |
| ["+", "-", "*", "/", "**", "%"] | |
| ) | |
| if st.button("Calculate"): | |
| try: | |
| if operation == "+": | |
| result = num1 + num2 | |
| elif operation == "-": | |
| result = num1 - num2 | |
| elif operation == "*": | |
| result = num1 * num2 | |
| elif operation == "/": | |
| if num2 == 0: | |
| st.error("Division by zero is not allowed") | |
| result = None | |
| else: | |
| result = num1 / num2 | |
| elif operation == "**": | |
| result = num1 ** num2 | |
| elif operation == "%": | |
| result = num1 % num2 | |
| if result is not None: | |
| st.success(f"Result: {result}") | |
| except Exception as e: | |
| st.error("Something went wrong") | |
| # ---------------- SCIENTIFIC CALCULATOR ---------------- | |
| elif mode == "Scientific": | |
| st.subheader("Scientific Calculator") | |
| number = st.number_input("Enter a number", value=0.0) | |
| operation = st.selectbox( | |
| "Choose function", | |
| ["sin", "cos", "tan", "sqrt", "log", "exp"] | |
| ) | |
| if st.button("Calculate"): | |
| try: | |
| if operation == "sin": | |
| result = math.sin(math.radians(number)) | |
| elif operation == "cos": | |
| result = math.cos(math.radians(number)) | |
| elif operation == "tan": | |
| result = math.tan(math.radians(number)) | |
| elif operation == "sqrt": | |
| if number < 0: | |
| st.error("Cannot calculate square root of negative number") | |
| result = None | |
| else: | |
| result = math.sqrt(number) | |
| elif operation == "log": | |
| if number <= 0: | |
| st.error("Logarithm input must be positive") | |
| result = None | |
| else: | |
| result = math.log(number) | |
| elif operation == "exp": | |
| result = math.exp(number) | |
| if result is not None: | |
| st.success(f"Result: {result}") | |
| except Exception: | |
| st.error("Invalid calculation") | |
| # ---------------- EXPRESSION CALCULATOR ---------------- | |
| elif mode == "Expression": | |
| st.subheader("Expression Calculator") | |
| st.write("Example: `3 + 5 * (2 - 1) ** 2`") | |
| expression = st.text_input("Enter expression") | |
| if st.button("Evaluate"): | |
| try: | |
| allowed_chars = "0123456789+-*/().% " | |
| if all(char in allowed_chars for char in expression): | |
| result = eval(expression) | |
| st.success(f"Result: {result}") | |
| else: | |
| st.error("Invalid characters in expression") | |
| except Exception: | |
| st.error("Invalid expression") | |
| st.markdown("---") | |
| st.caption("Built with Streamlit • Deployable on Hugging Face Spaces") | |