init()
eq()
pressAdd()
pressSub()
getCurrentVariable()
resetCurrentVariable()
rollbackCurrentVariable()
class MyInteger:
def __init__(self, value=0):
self.currentValue = []
if(type(value) in [int, float, str, MyInteger]):
self.currentValue.append(int(value))
else:
self.currentValue.append(0)
def __eq__(self, other):
if(len(self.currentValue) == len(other.currentValue)):
for i in range(0, len(self.currentValue)):
if int(self.currentValue[i]) != int(other.currentValue[i]):
return False
else:
return False
def pressAdd(self, addvalue):
if (type(addvalue) in [int, float, str, MyInteger]):
self.currentValue.insert(0, self.currentValue[0] + int(addvalue))
return self.currentValue[0]
def pressSub(self, subvalue):
if (type(subvalue) in [int, float, str, MyInteger]):
self.currentValue.insert(0, self.currentValue[0] - int(subvalue))
return self.currentValue[0]
def getCurrentVariable(self, motion=None):
if (motion == None):
return self.currentValue[0]
elif (motion == "hex"):
hexnum = hex(int(self.currentValue[0]))
return hexnum
elif (motion == "oct"):
octnum = oct(int(self.currentValue[0]))
return octnum
elif (motion == "bin"):
binnum = bin(int(self.currentValue[0]))
return binnum
def resetCurrentVariable(self):
for i in range(0, len(self.currentValue)):
self.currentValue[i] = 0
return self.currentValue[0]
def rollbackCurrentVariable(self):
if(len(self.currentValue) > 0):
del self.currentValue[0]
return self.currentValue[0]
else:
self.resetCurrentVariable()
return self.currentValue[0]
from MyInteger import *
if __name__ == '__main__':
### MyInteger Class ###
print("MyInteger Class")
print()
# __init__ method
print("Concstructor method")
myint1 = MyInteger()
myint2 = MyInteger(3)
print("myint1:", myint1.currentValue)
print("myint2:", myint2.currentValue)
print()
# __eq__ method
print("Equally Compare method")
print(myint1 == myint2)
print()
# pressAdd method
print("pressAdd method")
print("After adding:", myint1.pressAdd(77))
print(myint1.currentValue)
print()
# pressSub method
print("pressSub method")
print("After subtract:", myint1.pressSub(2))
print(myint1.currentValue)
print()
# getCurrentVariable method
print("getCurrentVariable method")
print("Option is None:", myint1.getCurrentVariable())
print("Option is hex:", myint1.getCurrentVariable("hex"))
print("Option is oct:", myint1.getCurrentVariable("oct"))
print("Option is bin:", myint1.getCurrentVariable("bin"))
print()
# resetCurrentVariable method
print("resetCurrentVariable method")
print(myint1.resetCurrentVariable())
print(myint1.currentValue)
print()
# rollbackCurrentVariable method
print("rollbackCurrentVariable method")
myint1.currentValue[0] = 75
myint1.currentValue[1] = 77
print(myint1.rollbackCurrentVariable())
print(myint1.currentValue)
print()