|
|
import math |
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
def add(x, y): |
|
|
return x + y |
|
|
|
|
|
def subtract(x, y): |
|
|
return x - y |
|
|
|
|
|
def multiply(x, y): |
|
|
return x * y |
|
|
|
|
|
def divide(x, y): |
|
|
if y != 0: |
|
|
return x / y |
|
|
else: |
|
|
return "Error! Division by zero." |
|
|
|
|
|
def square_root(x): |
|
|
if x >= 0: |
|
|
return math.sqrt(x) |
|
|
else: |
|
|
return "Error! Negative input." |
|
|
|
|
|
def power(x, y): |
|
|
return math.pow(x, y) |
|
|
|
|
|
def sine(x): |
|
|
return math.sin(math.radians(x)) |
|
|
|
|
|
def cosine(x): |
|
|
return math.cos(math.radians(x)) |
|
|
|
|
|
def tangent(x): |
|
|
return math.tan(math.radians(x)) |
|
|
|
|
|
|
|
|
st.title("Calculator App") |
|
|
st.sidebar.title("Operations Menu") |
|
|
|
|
|
|
|
|
operation = st.sidebar.selectbox( |
|
|
"Select operation:", |
|
|
["Add", "Subtract", "Multiply", "Divide", "Square Root", "Power", "Sine", "Cosine", "Tangent"] |
|
|
) |
|
|
|
|
|
|
|
|
if operation in ["Add", "Subtract", "Multiply", "Divide", "Power"]: |
|
|
x = st.number_input("Enter the first number:", value=0.0) |
|
|
y = st.number_input("Enter the second number:", value=0.0) |
|
|
if st.button("Calculate"): |
|
|
if operation == "Add": |
|
|
st.write(f"Result: {x} + {y} = {add(x, y)}") |
|
|
elif operation == "Subtract": |
|
|
st.write(f"Result: {x} - {y} = {subtract(x, y)}") |
|
|
elif operation == "Multiply": |
|
|
st.write(f"Result: {x} * {y} = {multiply(x, y)}") |
|
|
elif operation == "Divide": |
|
|
st.write(f"Result: {x} / {y} = {divide(x, y)}") |
|
|
elif operation == "Power": |
|
|
st.write(f"Result: {x} ^ {y} = {power(x, y)}") |
|
|
|
|
|
elif operation == "Square Root": |
|
|
x = st.number_input("Enter the number:", value=0.0) |
|
|
if st.button("Calculate"): |
|
|
st.write(f"Result: √{x} = {square_root(x)}") |
|
|
|
|
|
elif operation in ["Sine", "Cosine", "Tangent"]: |
|
|
x = st.number_input("Enter the angle (in degrees):", value=0.0) |
|
|
if st.button("Calculate"): |
|
|
if operation == "Sine": |
|
|
st.write(f"Result: sin({x}°) = {sine(x)}") |
|
|
elif operation == "Cosine": |
|
|
st.write(f"Result: cos({x}°) = {cosine(x)}") |
|
|
elif operation == "Tangent": |
|
|
st.write(f"Result: tan({x}°) = {tangent(x)}") |
|
|
|