Create app.py
Browse files
app.py
CHANGED
|
@@ -1,141 +1,94 @@
|
|
|
|
|
| 1 |
import math
|
| 2 |
|
| 3 |
-
#
|
| 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
|
| 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 "
|
| 35 |
return math.log10(x)
|
| 36 |
|
| 37 |
-
# Function for natural logarithm (base e)
|
| 38 |
def loge(x):
|
| 39 |
if x <= 0:
|
| 40 |
-
return "
|
| 41 |
return math.log(x)
|
| 42 |
|
| 43 |
-
# Function for sine
|
| 44 |
def sine(x):
|
| 45 |
-
return math.sin(math.radians(x))
|
| 46 |
|
| 47 |
-
# Function for cosine
|
| 48 |
def cosine(x):
|
| 49 |
-
return math.cos(math.radians(x))
|
| 50 |
|
| 51 |
-
# Function for tangent
|
| 52 |
def tangent(x):
|
| 53 |
-
return math.tan(math.radians(x))
|
| 54 |
|
| 55 |
-
# Function for factorial
|
| 56 |
def factorial(x):
|
| 57 |
-
if x < 0
|
| 58 |
-
return "Factorial
|
| 59 |
return math.factorial(int(x))
|
| 60 |
|
| 61 |
-
# Function for absolute value
|
| 62 |
def absolute(x):
|
| 63 |
return abs(x)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|