Habib U Rehman commited on
Commit
1ce565d
·
verified ·
1 Parent(s): 0ac3d90

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import math
3
+
4
+ st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
5
+
6
+ st.title("🧮 Advanced Calculator")
7
+ st.write("Built with Streamlit for Hugging Face Spaces 🚀")
8
+
9
+ # Initialize history
10
+ if "history" not in st.session_state:
11
+ st.session_state.history = []
12
+
13
+ operation = st.selectbox(
14
+ "Choose an operation",
15
+ [
16
+ "Addition",
17
+ "Subtraction",
18
+ "Multiplication",
19
+ "Division",
20
+ "Power",
21
+ "Modulus",
22
+ "Square Root",
23
+ "Sin",
24
+ "Cos",
25
+ "Tan",
26
+ ],
27
+ )
28
+
29
+ def calculate(op, a=None, b=None):
30
+ if op == "Addition":
31
+ return a + b
32
+ elif op == "Subtraction":
33
+ return a - b
34
+ elif op == "Multiplication":
35
+ return a * b
36
+ elif op == "Division":
37
+ if b == 0:
38
+ return "❌ Division by zero"
39
+ return a / b
40
+ elif op == "Power":
41
+ return a ** b
42
+ elif op == "Modulus":
43
+ return a % b
44
+ elif op == "Square Root":
45
+ if a < 0:
46
+ return "❌ Negative number"
47
+ return math.sqrt(a)
48
+ elif op == "Sin":
49
+ return math.sin(math.radians(a))
50
+ elif op == "Cos":
51
+ return math.cos(math.radians(a))
52
+ elif op == "Tan":
53
+ return math.tan(math.radians(a))
54
+
55
+ # Input fields
56
+ if operation in ["Square Root", "Sin", "Cos", "Tan"]:
57
+ num1 = st.number_input("Enter number / angle (degrees)", value=0.0)
58
+ if st.button("Calculate"):
59
+ result = calculate(operation, num1)
60
+ st.success(f"Result: {result}")
61
+ st.session_state.history.append(f"{operation}({num1}) = {result}")
62
+ else:
63
+ num1 = st.number_input("Enter first number", value=0.0)
64
+ num2 = st.number_input("Enter second number", value=0.0)
65
+ if st.button("Calculate"):
66
+ result = calculate(operation, num1, num2)
67
+ st.success(f"Result: {result}")
68
+ st.session_state.history.append(
69
+ f"{operation}({num1}, {num2}) = {result}"
70
+ )
71
+
72
+ st.markdown("---")
73
+ st.subheader("📜 Calculation History")
74
+
75
+ if st.session_state.history:
76
+ for item in reversed(st.session_state.history):
77
+ st.write(item)
78
+ else:
79
+ st.write("No calculations yet.")