Muhammadtaha12 commited on
Commit
f3245c5
·
verified ·
1 Parent(s): 1da5844

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -100
app.py CHANGED
@@ -1,141 +1,94 @@
 
1
  import math
2
 
3
- # Function for addition
4
  def add(x, y):
5
  return x + y
6
 
7
- # Function for subtraction
8
  def subtract(x, y):
9
  return x - y
10
 
11
- # Function for multiplication
12
  def multiply(x, y):
13
  return x * y
14
 
15
- # Function for division
16
  def divide(x, y):
17
  if y == 0:
18
  return "Cannot divide by zero"
19
  return x / y
20
 
21
- # Function for exponentiation
22
  def power(x, y):
23
- return x ** y
24
 
25
- # Function for square root
26
  def sqrt(x):
27
- if x < 0:
28
- return "Cannot compute the square root of a negative number"
29
  return math.sqrt(x)
30
 
31
- # Function for logarithm (base 10)
32
  def log10(x):
33
  if x <= 0:
34
- return "Cannot compute logarithm of a non-positive number"
35
  return math.log10(x)
36
 
37
- # Function for natural logarithm (base e)
38
  def loge(x):
39
  if x <= 0:
40
- return "Cannot compute natural logarithm of a non-positive number"
41
  return math.log(x)
42
 
43
- # Function for sine
44
  def sine(x):
45
- return math.sin(math.radians(x)) # Converting degrees to radians
46
 
47
- # Function for cosine
48
  def cosine(x):
49
- return math.cos(math.radians(x)) # Converting degrees to radians
50
 
51
- # Function for tangent
52
  def tangent(x):
53
- return math.tan(math.radians(x)) # Converting degrees to radians
54
 
55
- # Function for factorial
56
  def factorial(x):
57
- if x < 0 or not x.is_integer():
58
- return "Factorial is only defined for non-negative integers"
59
  return math.factorial(int(x))
60
 
61
- # Function for absolute value
62
  def absolute(x):
63
  return abs(x)
64
 
65
- # Display the menu
66
- def menu():
67
- print("\nSelect operation:")
68
- print("1. Add")
69
- print("2. Subtract")
70
- print("3. Multiply")
71
- print("4. Divide")
72
- print("5. Power (x^y)")
73
- print("6. Square Root (√x)")
74
- print("7. Logarithm (log10(x))")
75
- print("8. Natural Logarithm (loge(x))")
76
- print("9. Sine (sin(x))")
77
- print("10. Cosine (cos(x))")
78
- print("11. Tangent (tan(x))")
79
- print("12. Factorial")
80
- print("13. Absolute Value")
81
- print("14. Exit")
82
-
83
- # Main function to handle the calculator operations
84
- def calculator():
85
- while True:
86
- menu() # Display the operation menu
87
- choice = input("Enter choice (1/2/3/4/5/6/7/8/9/10/11/12/13/14): ")
88
-
89
- if choice == '14':
90
- print("Exiting the calculator. Goodbye!")
91
- break
92
-
93
- # Handle operations with one input
94
- if choice in ['6', '7', '8', '9', '10', '11', '12', '13']:
95
- try:
96
- num1 = float(input("Enter the number: "))
97
- except ValueError:
98
- print("Invalid input. Please enter a numeric value.")
99
- continue
100
-
101
- # Perform the calculation based on user choice
102
- if choice == '6':
103
- print(f"√{num1} = {sqrt(num1)}")
104
- elif choice == '7':
105
- print(f"log10({num1}) = {log10(num1)}")
106
- elif choice == '8':
107
- print(f"loge({num1}) = {loge(num1)}")
108
- elif choice == '9':
109
- print(f"sin({num1}) = {sine(num1)}")
110
- elif choice == '10':
111
- print(f"cos({num1}) = {cosine(num1)}")
112
- elif choice == '11':
113
- print(f"tan({num1}) = {tangent(num1)}")
114
- elif choice == '12':
115
- print(f"{num1}! = {factorial(num1)}")
116
- elif choice == '13':
117
- print(f"Absolute value of {num1} = {absolute(num1)}")
118
-
119
- # Handle operations with two inputs
120
- else:
121
- try:
122
- num1 = float(input("Enter first number: "))
123
- num2 = float(input("Enter second number: "))
124
- except ValueError:
125
- print("Invalid input. Please enter numeric values.")
126
- continue
127
-
128
- # Perform the calculation based on user choice
129
- if choice == '1':
130
- print(f"{num1} + {num2} = {add(num1, num2)}")
131
- elif choice == '2':
132
- print(f"{num1} - {num2} = {subtract(num1, num2)}")
133
- elif choice == '3':
134
- print(f"{num1} * {num2} = {multiply(num1, num2)}")
135
- elif choice == '4':
136
- print(f"{num1} / {num2} = {divide(num1, num2)}")
137
- elif choice == '5':
138
- print(f"{num1} ^ {num2} = {power(num1, num2)}")
139
-
140
- # Run the calculator
141
- calculator()
 
1
+ import streamlit as st
2
  import math
3
 
4
+ # Define functions for various scientific operations
5
  def add(x, y):
6
  return x + y
7
 
 
8
  def subtract(x, y):
9
  return x - y
10
 
 
11
  def multiply(x, y):
12
  return x * y
13
 
 
14
  def divide(x, y):
15
  if y == 0:
16
  return "Cannot divide by zero"
17
  return x / y
18
 
 
19
  def power(x, y):
20
+ return math.pow(x, y)
21
 
 
22
  def sqrt(x):
 
 
23
  return math.sqrt(x)
24
 
 
25
  def log10(x):
26
  if x <= 0:
27
+ return "Logarithm undefined for non-positive numbers"
28
  return math.log10(x)
29
 
 
30
  def loge(x):
31
  if x <= 0:
32
+ return "Natural log undefined for non-positive numbers"
33
  return math.log(x)
34
 
 
35
  def sine(x):
36
+ return math.sin(math.radians(x))
37
 
 
38
  def cosine(x):
39
+ return math.cos(math.radians(x))
40
 
 
41
  def tangent(x):
42
+ return math.tan(math.radians(x))
43
 
 
44
  def factorial(x):
45
+ if x < 0:
46
+ return "Factorial undefined for negative numbers"
47
  return math.factorial(int(x))
48
 
 
49
  def absolute(x):
50
  return abs(x)
51
 
52
+ # Streamlit UI setup
53
+ st.title("Scientific Calculator")
54
+
55
+ # Input fields for two numbers (for binary operations)
56
+ num1 = st.number_input("Enter the first number:", value=0.0)
57
+ num2 = st.number_input("Enter the second number:", value=0.0)
58
+
59
+ # Select box for selecting operations
60
+ operation = st.selectbox(
61
+ "Select an operation",
62
+ ["Add", "Subtract", "Multiply", "Divide", "Power", "Square Root", "log10", "ln", "Sine", "Cosine", "Tangent", "Factorial", "Absolute"]
63
+ )
64
+
65
+ # Perform the selected operation
66
+ if operation == "Add":
67
+ result = add(num1, num2)
68
+ elif operation == "Subtract":
69
+ result = subtract(num1, num2)
70
+ elif operation == "Multiply":
71
+ result = multiply(num1, num2)
72
+ elif operation == "Divide":
73
+ result = divide(num1, num2)
74
+ elif operation == "Power":
75
+ result = power(num1, num2)
76
+ elif operation == "Square Root":
77
+ result = sqrt(num1)
78
+ elif operation == "log10":
79
+ result = log10(num1)
80
+ elif operation == "ln":
81
+ result = loge(num1)
82
+ elif operation == "Sine":
83
+ result = sine(num1)
84
+ elif operation == "Cosine":
85
+ result = cosine(num1)
86
+ elif operation == "Tangent":
87
+ result = tangent(num1)
88
+ elif operation == "Factorial":
89
+ result = factorial(num1)
90
+ elif operation == "Absolute":
91
+ result = absolute(num1)
92
+
93
+ # Display the result
94
+ st.write(f"The result of {operation} is: {result}")