nosheen80 commited on
Commit
094ab23
·
verified ·
1 Parent(s): 2c40fb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -21
app.py CHANGED
@@ -1,26 +1,68 @@
1
  import streamlit as st
 
 
2
 
3
- st.title("🧮 Simple Calculator")
 
4
 
5
- # User inputs
6
- num1 = st.number_input("Enter first number:", format="%.2f")
7
- operation = st.selectbox("Choose operation:", ["Add", "Subtract", "Multiply", "Divide"])
8
- num2 = st.number_input("Enter second number:", format="%.2f")
9
 
10
- # Calculate based on operation
11
- if st.button("Calculate"):
12
- if operation == "Add":
13
- result = num1 + num2
14
- st.success(f"Result: {result}")
15
- elif operation == "Subtract":
16
- result = num1 - num2
17
- st.success(f"Result: {result}")
18
- elif operation == "Multiply":
19
- result = num1 * num2
20
- st.success(f"Result: {result}")
21
- elif operation == "Divide":
22
- if num2 != 0:
23
- result = num1 / num2
24
  st.success(f"Result: {result}")
25
- else:
26
- st.error("Division by zero is not allowed.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import math
4
 
5
+ st.set_page_config(page_title="Scientific Calculator", layout="centered")
6
+ st.title("🔬 Advanced Scientific Calculator")
7
 
8
+ st.markdown("Use the fields below to calculate using scientific functions or direct expressions.")
 
 
 
9
 
10
+ calc_mode = st.radio("Choose Mode", ["Button-Based Calculator", "Expression Evaluator"])
11
+
12
+ if calc_mode == "Expression Evaluator":
13
+ expr = st.text_input("Enter expression (e.g. 2 * np.sin(np.pi/4) + np.log10(100))")
14
+ if st.button("Evaluate Expression"):
15
+ try:
16
+ result = eval(expr, {"__builtins__": None}, {
17
+ "np": np, "math": math,
18
+ "sin": np.sin, "cos": np.cos, "tan": np.tan,
19
+ "log": np.log, "log10": np.log10,
20
+ "sqrt": np.sqrt, "pi": np.pi, "e": np.e,
21
+ "pow": np.power, "factorial": math.factorial
22
+ })
 
23
  st.success(f"Result: {result}")
24
+ except Exception as e:
25
+ st.error(f"Error: {str(e)}")
26
+
27
+ else:
28
+ num1 = st.number_input("Enter First Number:", format="%.6f", step=0.1)
29
+ operation = st.selectbox("Choose Operation", [
30
+ "Add", "Subtract", "Multiply", "Divide", "Power", "Square Root",
31
+ "Sine", "Cosine", "Tangent", "Log10", "Natural Log (ln)", "Factorial"
32
+ ])
33
+ if operation not in ["Sine", "Cosine", "Tangent", "Square Root", "Log10", "Natural Log (ln)", "Factorial"]:
34
+ num2 = st.number_input("Enter Second Number:", format="%.6f", step=0.1)
35
+
36
+ if st.button("Calculate"):
37
+ try:
38
+ if operation == "Add":
39
+ result = num1 + num2
40
+ elif operation == "Subtract":
41
+ result = num1 - num2
42
+ elif operation == "Multiply":
43
+ result = num1 * num2
44
+ elif operation == "Divide":
45
+ result = num1 / num2 if num2 != 0 else "Error: Division by zero"
46
+ elif operation == "Power":
47
+ result = math.pow(num1, num2)
48
+ elif operation == "Square Root":
49
+ result = math.sqrt(num1)
50
+ elif operation == "Sine":
51
+ result = np.sin(np.radians(num1))
52
+ elif operation == "Cosine":
53
+ result = np.cos(np.radians(num1))
54
+ elif operation == "Tangent":
55
+ result = np.tan(np.radians(num1))
56
+ elif operation == "Log10":
57
+ result = math.log10(num1)
58
+ elif operation == "Natural Log (ln)":
59
+ result = math.log(num1)
60
+ elif operation == "Factorial":
61
+ result = math.factorial(int(num1))
62
+ else:
63
+ result = "Invalid Operation"
64
+
65
+ st.success(f"Result: {result}")
66
+ except Exception as e:
67
+ st.error(f"Error: {str(e)}")
68
+