Skip to content
Merged
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
244 changes: 199 additions & 45 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,201 @@
class State:
"""
initiates a calculator object
"""

import math

class Calculator:

def __init__(self):
self.value = 0
self.command = None
self.display_mode = "decimal"
self.trig_mode = "rads"
self.display = "0"
self.user_entry = None

def get_current_value(self):
pass

def print(self):
if self.display_mode == "decimal":
self.display = str(self.value)
elif self.display_mode == "hex":
self.display = hex(self.value)
elif self.display_mode == "binary":
pass
elif self.display_mode == "octal":
pass
else:
self.error()
print("current display is:", self.display)

def error(self):
self.value = None
self.command = None
self.display_mode = "Err"

def add(self, n):
self.value = self.value + n
self.command = None

def help(self):
print(
"You may enter a number or a command. They command will act on the displayed number, or wait for a second number to take action")
print(
"the commands are +, -, *, /, Square, SquareRoot, ^, Invert, ClearError, SwitchSign, Decimal, Binary, Octal, Hex, ToggleDisplayStyle, M+, MC, MRC, Sine, Cosine, Tangent, ASine, ACosine, ATangent, SwitchTrigMode, Rads, Degrees, !, Log, 10^x, Ln, e^x")

def sub(self, n):
self.value = self.value - n
self.command = None
self.degrees = True


def add(self, a, b):
"""
ADDS TWO NUMBERS AND RETURNS RESULT
:param a:
:param b:
:return:
"""
print( a + b)
return a + b


def subtract(self, x, y):
"""
SUBTRACTS TWO NUMBERS AND RETURNS RESULT
:param x:
:param y:
:return:
"""
print( x - y)
return x - y


def multiply(self, x, y):
"""
MULTIPLIES TWO NUMBERS AND RETURNS RESULT
:param x:
:param y:
:return:
"""
print( x * y)
return y * x


def divide(self, x, y):
"""
DIVIDES TWO NUMBERS AND RETURNS RESULT
:param x:
:param y:
:return:
"""
print( x / y)
return x / y


def square(self, base):
"""
SQUARES A NUMBER AND RETURNS RESULT
:param base:
:return:
"""
print( base ** 2)
return base ** 2


def exp(self, x, y):
"""
EXPONENTIATES TWO NUMBERS AND RETURNS RESULT
:param x:
:param y:
:return:
"""
print( x ** y)
return x ** y


def square_root(self, x):
"""
FINDS THE SQUARE ROOT OF A NUMBER AND RETURNS RESULT
:param x:
:return:
"""
print( x ** (1 / 2))
return x ** (1 / 2)


def inverse(self, x):
"""
INVERSES A NUMBER AND RETURNS RESULT
:param x:
:return:
"""
print( 1 / x)
return 1 / x

def deg_rad_swap(self):
"""
SWAPS BETWEEN RADIANS AND DEGREES
:return:
"""
self.degrees = not self.degrees

def sin(self, x):
"""
FINDS SINE OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.sin(x)

def cosine(self, x):
"""
FINDS COSINE OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.cos(x)

def tangent(self, x):
"""
FINDS TANGENT OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.tan(x)

def inverse_sine(self, x):
"""
FINDS INVERSE SINE OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.asin(x)

def inverse_cosine(self, x):
"""
FINDS INVERSE COSINE OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.acos(x)

def inverse_tangent(self, x):
"""
FINDS INVERSE TANGENT OF NUMBER AND RETURNS RESULT
:param x:
:return:
"""
if self.degrees:
x = math.radians(x)
return math.atan(x)

##SECONDARY FORMULAS

def add2(self, x):
"""
ADDS A NUMBER WITH THE SAME NUMBER
:param x:
:return:
"""
print( returned_result + x)
return returned_result + x


def subtract2(self, x):
"""
SUBTRACTS A NUMBER WITH THE SAME NUMBER
:param x:
:return:
"""
print( returned_result - x)
return returned_result - x


def multiply2(self, x):
"""
MULTIPLIES A NUMBER WITH THE SAME NUMBER
:param x:
:return:
"""
print( returned_result * x)
return returned_result * x


def divide2(self, x):
"""
DIVIDES A NUMBER WITH THE SAME NUMBER
:param x:
:return:
"""
print( returned_result / x)
return returned_result / x

Loading