Improve project: code quality, docs, and structure

This commit is contained in:
MoCipher 2026-02-19 12:18:17 +01:00
parent dc68ffef0f
commit 93ace72fee
2 changed files with 116 additions and 0 deletions

View file

@ -1,3 +1,4 @@
<<<<<<< HEAD
# ScientificCalculator # ScientificCalculator
A feature-rich scientific calculator implemented in C++ that provides advanced mathematical operations and a user-friendly interface. A feature-rich scientific calculator implemented in C++ that provides advanced mathematical operations and a user-friendly interface.
@ -198,3 +199,25 @@ This project is open source and available under the MIT License.
## Contact ## Contact
For questions, suggestions, or contributions, please open an issue on the GitHub repository. For questions, suggestions, or contributions, please open an issue on the GitHub repository.
=======
# Scientific Calculator
A simple scientific calculator built in Python.
## Features
- Basic arithmetic operations
- Trigonometric functions
- Logarithms and exponentials
## How to Use
1. Clone the repo:
`git clone <repo-url>`
2. Run the calculator:
`python calculator.py`
## Credits
Inspired by Python calculator tutorials.
## Disclaimer
For learning purposes.
>>>>>>> e44406f (Improve project: code quality, docs, and structure)

93
calculator.py Normal file
View file

@ -0,0 +1,93 @@
"""
Scientific Calculator
A simple command-line calculator supporting arithmetic, trigonometric, and logarithmic operations.
"""
import math
def add(x, y):
"""Return the sum of x and y."""
return x + y
def subtract(x, y):
"""Return the difference of x and y."""
return x - y
def multiply(x, y):
"""Return the product of x and y."""
return x * y
def divide(x, y):
"""Return the quotient of x and y."""
if y == 0:
raise ValueError("Cannot divide by zero.")
return x / y
def sine(x):
"""Return the sine of x (in radians)."""
return math.sin(x)
def cosine(x):
"""Return the cosine of x (in radians)."""
return math.cos(x)
def tangent(x):
"""Return the tangent of x (in radians)."""
return math.tan(x)
def logarithm(x, base=math.e):
"""Return the logarithm of x to the given base."""
if x <= 0:
raise ValueError("Logarithm undefined for non-positive values.")
return math.log(x, base)
def exponent(x, y):
"""Return x raised to the power y."""
return math.pow(x, y)
def main():
print("Scientific Calculator")
print("Select operation:")
print("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Sine\n6. Cosine\n7. Tangent\n8. Logarithm\n9. Exponent\n0. Exit")
while True:
choice = input("Enter choice: ")
try:
if choice == '1':
x, y = float(input("x: ")), float(input("y: "))
print("Result:", add(x, y))
elif choice == '2':
x, y = float(input("x: ")), float(input("y: "))
print("Result:", subtract(x, y))
elif choice == '3':
x, y = float(input("x: ")), float(input("y: "))
print("Result:", multiply(x, y))
elif choice == '4':
x, y = float(input("x: ")), float(input("y: "))
print("Result:", divide(x, y))
elif choice == '5':
x = float(input("x (radians): "))
print("Result:", sine(x))
elif choice == '6':
x = float(input("x (radians): "))
print("Result:", cosine(x))
elif choice == '7':
x = float(input("x (radians): "))
print("Result:", tangent(x))
elif choice == '8':
x = float(input("x: "))
base = input("Base (default e): ")
base = float(base) if base else math.e
print("Result:", logarithm(x, base))
elif choice == '9':
x, y = float(input("x: ")), float(input("y: "))
print("Result:", exponent(x, y))
elif choice == '0':
print("Goodbye!")
break
else:
print("Invalid choice.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()