Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
139 changes: 53 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,60 @@
# Python.ScientificCalculator
# Scientific Calculator

## Description
* **Objective** - To implement an `ScientificCalculator` which displays output of basic and scientific computations.
* **Purpose** - To establish familiarity with:
* Object `state`
* Teamwork :+1:
An interactive, command-line calculator written in Python. The application starts in `main.py`, where you can choose an arithmetic calculator or a scientific calculator.

## Features

## Git Collaboration
* Click the `fork` button in the top right corner to create a copy of this repository on your github account.
* You can go through the [GitHub forking tutorial](https://help.github.com/articles/fork-a-repo/) if you need additional practice with this.
* You should work on this project in your own repository.
### Arithmetic calculator

- Addition, subtraction, multiplication, and division
- Division-by-zero handling
- Calculator display that updates after each successful operation
- Memory controls:
- `M+` adds the displayed value to memory
- `MC` clears memory
- `MRC` recalls memory to the display

### Scientific calculator

Includes every arithmetic and memory feature, plus:

- Powers, square roots, and reciprocals
- Trigonometric functions: sine, cosine, and tangent
- Inverse trigonometric functions: arcsine, arccosine, and arctangent
- Trigonometric input and inverse-trigonometric results in degrees
- Base-10 and natural logarithms
- Factorials of non-negative integers
- Symbolic differentiation and integration of expressions in `x`
- Random integer generation within a supplied range
- Mean, median, and mode for a comma-separated list of numbers
- Input validation for invalid domains, such as negative square roots, zero reciprocals, and invalid logarithms

## Requirements

### Testing

* All features must be tested.
* Tests must include normal behavior, and any possible error situations.
* Tests must have descriptive names and should be independent of each other (running or not running one test should not influence the behavior of any other test).

### Core Features
* All calculators should have the following features:
- A `state`, representing the value currently displayed on the calculator (default 0)
- Get the current number on the display
- Clear the display
- Add, subtract, multiply, and divide the value on the `display` by a given number
- Calculate the square (x<sup>2</sup>) and square root (√x) of the number on the display
- Calculate variable exponentiation (x<sup>y</sup>)
- Calculate the inverse of the number on the display (1/x)
- Invert the sign of the number on the display (switch between positive and negative)
- Update the display to `Err` if an error occurs (eg: Division by zero)
- Errors must be cleared before any other operation can take place

* Each operation should automatically update the display
* YOU MAY NEED to break your code into several .py files so that you can do your `git` stuff easier.


### Scientific Features

- Switch display mode (binary, octal, decimal, hexadecimal)
- `switchDisplayMode()` should rotate through the options
- `switchDisplayMode(String mode)` should set the display to the mode given
- Memory - Store up to one numeric value in memory for recall later (default to 0) *
- (`M+` key) Add the currently displayed value to the value in memory (store in memory and update display) *
- (`MC` key) Reset memory *
- (`MRC` key) Recall the current value from memory to the display *
- Trig functions
- Sine - Calculate the sine of the displayed value and display it
- Cosine - Calculate the cosine of the displayed value and display it
- Tangent - Calculate the tangent of the displayed value and display it
- Inverse Sine
- Inverse Cosine
- Inverse Tangent
- Switch trig units mode (Degrees, Radians)
- `switchUnitsMode()` should rotate through the options
- `switchUnitsMode(String mode)` should set the trig units to the type given

### Bonus
- Factorial function
- Logarithmic functions
- Log
- 10<sup>x</sup> (inverse logarithm)
- Ln (natural logarithm)
- e<sup>x</sup> (inverse natural logarithm)



### Custom Features

In addition to the Core and Scientific features, you are required to create at least two of your own features for the calculator. They can be any two features that are not already covered and that you can implement as you see fit. These features must be properly tested.

### Hints

The following functions should take the displayed value (x) and updated it according to the given formula: (this may not be an exhaustive list)

- `square()`: x<sup>2</sup>
- `squareRoot()`: √x
- `inverse()`: <sup>1</sup>/<sub>x</sub>
- `switchSign()`: -x
- `sine()`: sin(x)
- `cosine()`: cos(x)
- `tangent()`: tan(x)
- `inverseSine()`: sin<sup>-1</sup>(x)
- `inverseCosine()`: sin<sup>-1</sup>(x)
- `inverseTangent()`: tan<sup>-1</sup>(x)
- `factorial()`: x! (x factorial)

## Submission

Completed projects should be submitted by submitting a pull request against the [original repository](https://git.zipcode.rocks/Cohort4.2/ZCW-MacroLabs-OOP-ScientificCalculator). All work should be done in your own repository.
- Python 3
- [SymPy](https://www.sympy.org/) for symbolic differentiation and integration

## Run the application

If you are using the project virtual environment:

```bash
.venv/bin/python main.py
```

Otherwise, install the dependency in your active Python environment and run the program:

```bash
python3 -m pip install sympy
python3 main.py
```

Follow the numbered prompts to select a calculator operation. Choose **3** from the main menu to exit.

## Tests

Run the current unit-test file with:

```bash
python3 calctests.py
```
56 changes: 56 additions & 0 deletions arithmetic_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import numbers


class ArithmeticCalculator:
def __init__(self):
self.display = 0
self.memory = 0

def get_two_numbers(self):
a = float(input("First number: "))
b = float(input("Second number: "))
return a, b

def show_result(self, value):
self.display = value
print(f"\nResult = {self.display }\n")

def add(self):
a, b = self.get_two_numbers()
self.show_result(a + b)

def subtract(self):
a, b = self.get_two_numbers()
self.show_result(a - b)

def multiply(self):
a, b = self.get_two_numbers()
self.show_result(a * b)

def divide(self):
a, b = self.get_two_numbers()

if b == 0:
print("\nCannot divide by zero.\n")
return

self.show_result(a / b)

def memory_add(self):
if not isinstance(self.display, numbers.Number):
print("\nM+ requires a numeric value on the display.\n")
return

self.memory += self.display
self.display = self.memory

print(f"\nMemory = {self.memory}\n")

def memory_clear(self):
self.memory = 0
print("\nMemory cleared.\n")

def memory_recall(self):
self.display = self.memory
print(f"\nRecalled value = {self.display}\n")

25 changes: 0 additions & 25 deletions calctests.py

This file was deleted.

12 changes: 0 additions & 12 deletions calculator.py

This file was deleted.

34 changes: 0 additions & 34 deletions main-app.py

This file was deleted.

Loading