File size: 2,785 Bytes
9de00ba
094ab23
 
9de00ba
094ab23
 
9de00ba
094ab23
9de00ba
094ab23
 
 
 
 
 
 
 
 
 
 
 
 
9de00ba
094ab23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import math

st.set_page_config(page_title="Scientific Calculator", layout="centered")
st.title("🔬 Advanced Scientific Calculator")

st.markdown("Use the fields below to calculate using scientific functions or direct expressions.")

calc_mode = st.radio("Choose Mode", ["Button-Based Calculator", "Expression Evaluator"])

if calc_mode == "Expression Evaluator":
    expr = st.text_input("Enter expression (e.g. 2 * np.sin(np.pi/4) + np.log10(100))")
    if st.button("Evaluate Expression"):
        try:
            result = eval(expr, {"__builtins__": None}, {
                "np": np, "math": math,
                "sin": np.sin, "cos": np.cos, "tan": np.tan,
                "log": np.log, "log10": np.log10,
                "sqrt": np.sqrt, "pi": np.pi, "e": np.e,
                "pow": np.power, "factorial": math.factorial
            })
            st.success(f"Result: {result}")
        except Exception as e:
            st.error(f"Error: {str(e)}")

else:
    num1 = st.number_input("Enter First Number:", format="%.6f", step=0.1)
    operation = st.selectbox("Choose Operation", [
        "Add", "Subtract", "Multiply", "Divide", "Power", "Square Root",
        "Sine", "Cosine", "Tangent", "Log10", "Natural Log (ln)", "Factorial"
    ])
    if operation not in ["Sine", "Cosine", "Tangent", "Square Root", "Log10", "Natural Log (ln)", "Factorial"]:
        num2 = st.number_input("Enter Second Number:", format="%.6f", step=0.1)

    if st.button("Calculate"):
        try:
            if operation == "Add":
                result = num1 + num2
            elif operation == "Subtract":
                result = num1 - num2
            elif operation == "Multiply":
                result = num1 * num2
            elif operation == "Divide":
                result = num1 / num2 if num2 != 0 else "Error: Division by zero"
            elif operation == "Power":
                result = math.pow(num1, num2)
            elif operation == "Square Root":
                result = math.sqrt(num1)
            elif operation == "Sine":
                result = np.sin(np.radians(num1))
            elif operation == "Cosine":
                result = np.cos(np.radians(num1))
            elif operation == "Tangent":
                result = np.tan(np.radians(num1))
            elif operation == "Log10":
                result = math.log10(num1)
            elif operation == "Natural Log (ln)":
                result = math.log(num1)
            elif operation == "Factorial":
                result = math.factorial(int(num1))
            else:
                result = "Invalid Operation"
            
            st.success(f"Result: {result}")
        except Exception as e:
            st.error(f"Error: {str(e)}")