Skip to content

Instantly share code, notes, and snippets.

@rudrathegreat
Last active November 24, 2018 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudrathegreat/de0f463c7304af152cccd34ce0cd5a61 to your computer and use it in GitHub Desktop.
Save rudrathegreat/de0f463c7304af152cccd34ce0cd5a61 to your computer and use it in GitHub Desktop.
A Simple Calculator Using Python
import math
import numpy as np
class Calculator(object):
def __init__(self):
pass
def add(self, num1, num2):
try:
answer = int(num1) + int(num2)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def subtract(self, num1, num2):
try:
answer = int(num1) - int(num2)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def multiply(self, num1, num2):
try:
answer = int(num1) * int(num2)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def divide(self, num1, num2):
try:
answer = int(num1) / int(num2)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def sqrt(self, num):
try:
answer = math.sqrt(int(num))
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def circum(self, radius):
answer = self.multiply(2, np.int(radius))
answer *= np.pi
return answer
def SOHCAHTOA(self, angle, t=''):
try:
if t == 'sin':
answer = math.sin(angle)
if t == 'cos':
answer = math.cos(angle)
if t == 'tan':
answer = math.tan(angle)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def square(self, num):
try:
answer = self.multiply(num, num)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def log(self, num, base):
try:
answer = math.log(num, base)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def factorial(self, num):
try:
answer = math.factorial(num)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def inverseSCT(self, angle, t=''):
try:
if t == 'sin' or 'asin':
answer = math.asin(angle)
if t == 'cos' or 'acos':
answer = math.acos(angle)
if t == 'tan' or 'atan':
answer = math.atan(angle)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def arc(self, angle, radii, circum == None):
try:
if circum == None:
circum = 2 * math.pi * radii
answer = self.multiply(circum * angle)
answer = self.divide(answer, 360)
else:
answer = self.multiply(circum * angle)
answer = self.divide(answer, 360)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def area_triangle(self, base, height):
try:
answer = self.multiply(base, height)
answer = self.divide(answer, 2)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def area_square(self, length):
try:
answer = self.square(length)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
def area_rect(self, length, width):
try:
answer = self.multiply(length, width)
return answer
except Exception as e:
print('Error Calculating: {}'.format{e})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment