MASS24 commited on
Commit
c85f41c
·
verified ·
1 Parent(s): 68db5bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -23
app.py CHANGED
@@ -1,25 +1,89 @@
1
  import streamlit as st
 
2
 
3
- st.title("Simple Calculator")
4
-
5
- num1 = st.number_input("Enter first number:", format="%f")
6
- num2 = st.number_input("Enter second number:", format="%f")
7
-
8
- operation = st.selectbox("Choose operation", ["Addition (+)", "Subtraction (-)", "Multiplication (*)", "Division (/)"])
9
-
10
- if st.button("Calculate"):
11
- if operation == "Addition (+)":
12
- result = num1 + num2
13
- st.success(f"The result is: {result}")
14
- elif operation == "Subtraction (-)":
15
- result = num1 - num2
16
- st.success(f"The result is: {result}")
17
- elif operation == "Multiplication (*)":
18
- result = num1 * num2
19
- st.success(f"The result is: {result}")
20
- elif operation == "Division (/)":
21
- if num2 != 0:
22
- result = num1 / num2
23
- st.success(f"The result is: {result}")
24
- else:
25
- st.error("Cannot divide by zero!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import math
3
 
4
+ st.title("🧮 Advanced Calculator")
5
+
6
+ tab1, tab2, tab3 = st.tabs(["Basic Operations", "Scientific Functions", "Trigonometry"])
7
+
8
+ with tab1:
9
+ st.header("Basic Operations")
10
+ num1 = st.number_input("Enter first number", key="basic_num1")
11
+ num2 = st.number_input("Enter second number", key="basic_num2")
12
+ operation = st.selectbox("Choose operation", ["Add (+)", "Subtract (-)", "Multiply (*)", "Divide (/)"], key="basic_op")
13
+
14
+ if st.button("Calculate", key="basic_calc"):
15
+ if operation == "Add (+)":
16
+ result = num1 + num2
17
+ elif operation == "Subtract (-)":
18
+ result = num1 - num2
19
+ elif operation == "Multiply (*)":
20
+ result = num1 * num2
21
+ elif operation == "Divide (/)":
22
+ if num2 != 0:
23
+ result = num1 / num2
24
+ else:
25
+ st.error("Cannot divide by zero.")
26
+ result = None
27
+ if result is not None:
28
+ st.success(f"Result: {result}")
29
+
30
+ with tab2:
31
+ st.header("Scientific Functions")
32
+ num = st.number_input("Enter a number", key="sci_num")
33
+ function = st.selectbox("Choose function", ["Power (x^y)", "Square Root", "Log10", "Natural Log", "Factorial"], key="sci_func")
34
+
35
+ if function == "Power (x^y)":
36
+ exponent = st.number_input("Enter exponent", key="sci_exponent")
37
+
38
+ if st.button("Calculate", key="sci_calc"):
39
+ try:
40
+ if function == "Power (x^y)":
41
+ result = math.pow(num, exponent)
42
+ elif function == "Square Root":
43
+ if num < 0:
44
+ st.error("Square root of negative number is not supported.")
45
+ result = None
46
+ else:
47
+ result = math.sqrt(num)
48
+ elif function == "Log10":
49
+ if num <= 0:
50
+ st.error("Logarithm undefined for zero or negative numbers.")
51
+ result = None
52
+ else:
53
+ result = math.log10(num)
54
+ elif function == "Natural Log":
55
+ if num <= 0:
56
+ st.error("Natural log undefined for zero or negative numbers.")
57
+ result = None
58
+ else:
59
+ result = math.log(num)
60
+ elif function == "Factorial":
61
+ if num < 0 or int(num) != num:
62
+ st.error("Factorial only defined for non-negative integers.")
63
+ result = None
64
+ else:
65
+ result = math.factorial(int(num))
66
+ if result is not None:
67
+ st.success(f"Result: {result}")
68
+ except Exception as e:
69
+ st.error(f"Error: {e}")
70
+
71
+ with tab3:
72
+ st.header("Trigonometric Functions")
73
+ angle = st.number_input("Enter angle in degrees", key="trig_angle")
74
+ trig_func = st.selectbox("Choose function", ["sin", "cos", "tan"], key="trig_func")
75
+
76
+ if st.button("Calculate", key="trig_calc"):
77
+ rad = math.radians(angle)
78
+ if trig_func == "sin":
79
+ result = math.sin(rad)
80
+ elif trig_func == "cos":
81
+ result = math.cos(rad)
82
+ elif trig_func == "tan":
83
+ if math.isclose(math.cos(rad), 0, abs_tol=1e-10):
84
+ st.error("tan is undefined for this angle.")
85
+ result = None
86
+ else:
87
+ result = math.tan(rad)
88
+ if result is not None:
89
+ st.success(f"{trig_func}({angle}°) = {result}")