|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def mous(maxn, minn):
maxn = max(abs(maxn),abs(minn))
minn = min(abs(maxn),abs(minn))
while True:
if maxn // minn == maxn /minn:
return minn
break
else:
minn = maxn % minn
class fenshu():
def __init__(self,nem , den):
self.nem = nem
self.den = den
if self.den < 0:
self.nem = -nem
self.den = -den
self.nem = self.nem / mous(self.nem,self.den)
self.den = self.den / mous(self.nem, self.den)
def __str__(self):
return str(self.nem) + "/"+ str(self.den)
def __add__(self, other):
result_den = self.den * other.den
result_nem = self.nem * other.den + other.nem * self.den
result_den = result_den / mous(max(result_den, result_nem), min(result_den, result_nem))
result_nem = result_nem / mous(max(result_den, result_nem), min(result_den, result_nem))
return str(result_nem) + "/" + str(result_den)
def __sub__(self, other):
result_den = self.den * other.den
result_nem = self.nem * other.den - other.nem * self.den
result_den = result_den / mous(max(result_den, result_nem), min(result_den, result_nem))
result_nem = result_nem / mous(max(result_den, result_nem), min(result_den, result_nem))
return str(result_nem) + "/" + str(result_den)
def __eq__(self, other):
self.result =self.nem * other.den
other.result = other.nem* self.den
if self.result == other.result:
print("good")
else:
print("wrong")
def get_den(self):
print(self.den)
def get_nem(self):
print(self.nem)
a = fenshu(-4,8)
print(a)
问一下为什么打印结果是-0.5/1.0
而不是-1/2.0 |
|