Last active
November 25, 2018 00:21
-
-
Save rudrathegreat/9fa8578ae83664baef182948d4f73dac to your computer and use it in GitHub Desktop.
Simple Calculator with Functionality to Use Previous Answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This class is designed to be used for mathematical | |
calculations only. | |
This program has the functionality to remember the | |
previous answer which you needed to calculate. | |
This code was orginally proposed in this issue, | |
link below - | |
https://github.com/AceLewis/my_first_calculator.py/issues/24 | |
This program is deviated from Calculator.py. The link | |
for that is below - | |
https://gist.github.com/rudrathegreat/de0f463c7304af152cccd34ce0cd5a61 | |
NumPy is a Python module programmed in Python, C | |
and FORTAN designed to do fast calculations in | |
Python. This is because Python is a very slow | |
program and when using the default operations | |
in Python, it often doen't give you the coreect | |
answer and it takes a long time to do so. | |
The math module is really good for many operations | |
found in a scientific calculator. It includes log, | |
sqrt, powers, etc. | |
More functionality coming soon! | |
''' | |
import math | |
import numpy as np | |
class Calculator(object): | |
def __init__(self): | |
self.answer = 0 | |
def add(self, num1, num2): | |
try: | |
if str(num1) == 'Ans': | |
answer = np.int(self.answer) + num2 | |
else: | |
answer = np.int(num1) + np.int(num2) | |
self.answer = answer | |
return answer | |
except Exception as e: | |
print('Error Calculating: {}'.format{e}) | |
def subtract(self, num1, num2): | |
try: | |
if str(num1) == 'Ans': | |
answer = np.int(self.answer) - num2 | |
else: | |
answer = np.int(num1) - np.int(num2) | |
self.answer = answer | |
return answer | |
except Exception as e: | |
print('Error Calculating: {}'.format{e}) | |
def multiply(self, num1, num2): | |
try: | |
if str(num1) == 'Ans': | |
answer = np.int(self.answer) * num2 | |
else: | |
answer = np.int(num1) + np.int(num2) | |
self.answer = answer | |
return answer | |
except Exception as e: | |
print('Error Calculating: {}'.format{e}) | |
def divide(self, num1, num2): | |
try: | |
if str(num1) == 'Ans': | |
answer = np.int(self.answer) / num2 | |
else: | |
answer = np.int(num1) / np.int(num2) | |
self.answer = answer | |
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