注意事项:
减法运算中 ,为了避免分母前出现负号的情况,在处理时,将所有负数按其绝对值处理。最后再添负号
除法运算中,0 / 0输出结果是 Inf。逻辑判断时 0 / 0容易出现bug
在判断正负的时候,别用两个数乘积来判断。因为如果这两个数过大容易出现结果溢出情况
# coding:utf-8
def Num(s):
N, k, F = 0, 0, 0
if s[0] == '-':
F = 1
s[1:] = s[-1: 0: -1]
s = s[1:]
else:
s[0:] = s[-1:: -1]
for item in s:
N += int(item) * 10 ** k
k += 1
if F == 0:
return N
else:
return (N * -1)
def GCD(a, b):
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
def Rationalized(m_a, d_a): # 有理化
flag = 0 #flag 为0表示 分子,分母同号
if (m_a < 0 and d_a > 0) or (m_a > 0 and d_a < 0):
flag = 1
if m_a