File size: 1,033 Bytes
17fdc44
136eb56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17fdc44
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
import streamlit as st
import numpy as np
import math

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

st.title("🧠 Advanced Calculator")
st.markdown("Perform advanced mathematical calculations securely and interactively.")

expression = st.text_input("Enter your expression (e.g., sin(30) + log(10) + 2^3):")

# Safe math function map
allowed_names = {
    k: v for k, v in math.__dict__.items() if not k.startswith("__")
}
allowed_names.update({
    'np': np,
    'sqrt': np.sqrt,
    'pow': pow,
    'abs': abs
})

def evaluate_expression(expr):
    try:
        # Replace ^ with ** for Python syntax
        expr = expr.replace("^", "**")
        result = eval(expr, {"__builtins__": {}}, allowed_names)
        return result
    except Exception as e:
        return f"❌ Error: {e}"

if expression:
    result = evaluate_expression(expression)
    st.subheader("🧾 Result:")
    st.code(result, language='text')

st.markdown("---")
st.caption("Built using Streamlit | © 2025 AdvancedCalc")