Case Study: Text-Based Scientific Calculator

This case study presents a text-based Scientific Calculator implemented in Python. The calculator offers a range of mathematical and scientific functions, making it a versatile tool for performing various calculations. It provides functions for basic arithmetic operations, power, square root, logarithm, and trigonometric operations such as sine, cosine, and tangent.

import math

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 "Error: Cannot divide by zero"
    return x / y

def power(x, y):
    return x ** y

def square_root(x):
    if x < 0:
        return "Error: Cannot calculate square root of a negative number"
    return math.sqrt(x)

def logarithm(x, base):
    if x <= 0 or base <= 0 or base == 1:
        return "Error: Invalid input for logarithm"
    return math.log(x, base)

def sin(x):
    return math.sin(math.radians(x))

def cos(x):
    return math.cos(math.radians(x))

def tan(x):
    return math.tan(math.radians(x))

def main():
    while True:
        print("\nScientific Calculator")
        print("1. Add")
        print("2. Subtract")
        print("3. Multiply")
        print("4. Divide")
        print("5. Power")
        print("6. Square Root")
        print("7. Logarithm")
        print("8. Sin")
        print("9. Cos")
        print("10. Tan")
        print("0. Exit")
        choice = input("Enter your choice: ")

        if choice == '0':
            print("Exiting the calculator...")
            break

        if choice in ('1', '2', '3', '4', '5'):
            num1 = float(input("Enter the first number: "))
            num2 = float(input("Enter the second number: "))

            if choice == '1':
                print("Result:", add(num1, num2))
            elif choice == '2':
                print("Result:", subtract(num1, num2))
            elif choice == '3':
                print("Result:", multiply(num1, num2))
            elif choice == '4':
                print("Result:", divide(num1, num2))
            elif choice == '5':
                print("Result:", power(num1, num2))
        elif choice in ('6', '7'):
            num = float(input("Enter the number: "))

            if choice == '6':
                print("Result:", square_root(num))
            else:
                base = float(input("Enter the base: "))
                print("Result:", logarithm(num, base))
        elif choice in ('8', '9', '10'):
            num = float(input("Enter the angle in degrees: "))
            if choice == '8':
                print("Result:", sin(num))
            elif choice == '9':
                print("Result:", cos(num))
            elif choice == '10':
                print("Result:", tan(num))
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

Output:

Calculator Functions

The Scientific Calculator includes the following functions:

  1. Addition: Adds two numbers.
  2. Subtraction: Subtracts one number from another.
  3. Multiplication: Multiplies two numbers.
  4. Division: Divides one number by another, handling division by zero.
  5. Exponentiation: Raises a number to a given power.
  6. Square Root: Calculates the square root of a non-negative number, handling negative inputs.
  7. Logarithm: Computes logarithms of a number with a specified base, handling invalid inputs.
  8. Sine: Calculates the sine of an angle given in degrees.
  9. Cosine: Calculates the cosine of an angle given in degrees.
  10. Tangent: Calculates the tangent of an angle given in degrees.

Usage

Users can select the desired operation by entering a corresponding number. The calculator then prompts users for the necessary inputs, performs the calculation, and displays the result. Users can continue performing calculations or exit the calculator when done.

Error Handling

The calculator incorporates error handling to address various scenarios, including division by zero, negative square roots, invalid logarithm inputs, and invalid trigonometric inputs.

Conclusion

The text-based Scientific Calculator offers a range of mathematical and scientific functions in a user-friendly format. It provides a versatile tool for performing calculations across various domains, including arithmetic, algebra, and trigonometry. This case study demonstrates the implementation of a basic calculator in Python, which can be further extended and integrated into other applications or converted into a graphical user interface for enhanced usability.

Leave a Reply

Your email address will not be published. Required fields are marked *