panheng
发表于 2019-8-21 16:47:03
本帖最后由 panheng 于 2019-8-21 16:55 编辑
符合规则的好繁琐啊。。。。
def multiple(x2,y2): #求最小公倍数
if x2 == 0 or y2 == 0:
print("分母不能为“0”!")
else:
if x2 % y2 == 0:
z2 = x2
elif y2 % x2 == 0:
z2 = y2
else:
z2 = x2 * y2
return z2
def reduction(z1,z2): #约分并返回结果
for i in range(z1, 1, -1):
if z2 % i == 0 and z1 % i == 0:
z2, z1 = int(z2 / i), int(z1 / i)
if z2 == 1:
result = z1
elif z1 ==0:
result = 0
else:
result = str(z1) + '/' + str(z2)
return result
def get_num(str1): #获取分子分母
if '/' in str1:
x1,x2 = int(str1.split('/')), int(str1.split('/'))
return x1,x2
else:
x1,x2 = int(str1),1
return x1,x2
def add_num(num1,num2): #加法
x1, x2 = get_num(num1)
y1, y2 = get_num(num2)
z2 = multiple(x2,y2)
z1 = int(x1 * z2 / x2 + y1 * z2 / y2)
return reduction(z1,z2)
def sub_num(num1,num2): # 减法
x1, x2 = get_num(num1)
y1, y2 = get_num(num2)
z2 = multiple(x2,y2)
z1 = int(x1 * z2 / x2 - y1 * z2 / y2)
return reduction(z1,z2)
if __name__ == '__main__':
num1 = input("请输入第一个数,分数请使用“a/b”:")
num2 = input("请输入第一个数,分数请使用“a/b”:")
calc = input("请输入运算方法“+”或“-”")
if calc == '+':
result = add_num(num1,num2)
elif calc == '-':
result = sub_num(num1,num2)
print("{} {} {}的运算结果为:{}".format(num1, calc, num2, result))
Jung
发表于 2019-12-3 14:47:43
def add(a,b,c,d):
Divisor = a +c
if b==d:
print("%d/%d + %d/%d = %d/%d" % (a,b,c,d,Divisor,b))
else:
Denorminator = b*d
Divisor = a*d + c*b
print("%d/%d + %d/%d = %d/%d" % (a,b,c,d,Divisor,Denorminator))
闲愚
发表于 2019-12-3 19:34:51
def find(num1,num2):
while num2 > 0:
num3 = num1 % num2
num1 = num2
num2 = num3
return num1
def add(a,b,c,d):
fenzi = a * d + b * c
fenmu = b * d
if fenzi > fenmu:
both = find(fenzi,fenmu)
fenzi /= both
fenmu /= both
elif fenzi < fenmu:
both = find(fenmu,fenzi)
fenzi /= both
fenmu /= both
return fenzi,fenmu
def sub(a,b,c,d):
fenzi = a * d - b * c
fenmu = b * d
if fenzi > fenmu:
both = find(fenzi,fenmu)
fenzi /= both
fenmu /= both
elif fenzi < fenmu:
both = find(fenmu,fenzi)
fenzi /= both
fenmu /= both
return fenzi,fenmu
a = int(input('请输入第一个数的分子:'))
b = int(input('请输入第一个数的分母:'))
c = int(input('请输入第二个数的分子:'))
d = int(input('请输入第二个数的分母:'))
e = input('请输入所做的运算(+ or -):')
if e == '+':
(fenzi, fenmu) = add(a, b, c, d)
else:
(fenzi, fenmu) = sub(a, b, c, d)
if fenzi == fenmu:
print('结果是:1')
else:
print('结果是:%d/%d' % (fenzi,fenmu))
华一仙
发表于 2020-2-28 11:30:12
def getCommonDivisor(m, d):
while d:
m, d = d, m%d
return m
def calc(m, d):
if d == 0:
return "X -- ZeroDivisionError"
elif m == 0:
return '0'
elif m == d:
return '1'
com_div = getCommonDivisor(m, d)
return str(m//com_div) + '/' + str(d//com_div)
def myAdd(a, b, c, d):
""" a/b + c/d """
molecule = a*d + b*c
denominator = b*d
return calc(molecule, denominator)
def myMinus(a, b, c, d):
""" a/b - c/d """
molecule = a*d - b*c
denominator = b*d
return calc(molecule, denominator)
杜若左
发表于 2020-3-25 15:33:25
def simplify(a,b):
for i in range(2,a):
if a%i==0 and b%i ==0:
a = a/i
b = b/i
print(a,'/',b)
def add (a,b,c,d):
t = a*d
i = c*b
simplify(t+i,b*d)
def sub (a,b,c,d):
t = a*d
i = c*b
simplify(t-i,b*d)
if __name__ == '__main__':
h,j,k,l = map(int,input().split())
print("加法还是减法呢?(减法输入1 减法输入0)")
p = int(input())
if p == 1:
add(h,j,k,l)
else:
sub(h,j,k,l)
holiday_python
发表于 2020-4-18 12:57:01
def add(a,b,c,d):
sum_result = a/b + c/d
return sum_result
if __name__ == '__main__':
a = int(input('请输入第1个分数的分子:'))
b = int(input('请输入第2个分数的分母:'))
c = int(input('请输入第1个分数的分子:'))
d = int(input('请输入第1个分数的分母:'))
print(add(a,b,c,d))
19971023
发表于 2020-6-2 18:24:38
1
谢啾啾
发表于 2020-11-9 21:59:08
# 分数加减法
def add(a,b,c,d):
# 分母相同,直接分子运算
if b == d:
temp = a+c
# 若分子大于分母且为倍数关系
if temp > b and temp%b == 0:
temp /= b
print(temp)
# 若分母大于分子且为倍数关系
elif temp < b and b%temp == 0:
b /= temp
print(temp,'/',b)
# 一般情况
else:
print(temp,'/',b)
else:
a = a*d
c = c*b
b = d = b*d
temp = a+c
# 若分子大于分母且为倍数关系
if temp > b and temp%b == 0:
temp /= b
print(temp)
# 若分母大于分子且为倍数关系
elif temp < b and b%temp == 0:
b /= temp
print(temp,'/',b)
# 一般情况
else:
print(temp,'/',b)
def sub(a,b,c,d):
# 分母相同,直接分子运算
if b == d:
temp = a-c
# 若分子大于分母且为倍数关系
if temp > b and temp%b == 0:
temp /= b
print(temp)
# 若分母大于分子且为倍数关系
elif temp < b and b%temp == 0:
b /= temp
print(temp,'/',b)
# 一般情况
else:
print(temp,'/',b)
else:
a = a * d
c = c * b
b = d = b * d
temp = a - c
if temp > b and temp % b == 0:
temp /= b
print(temp)
# 若分母大于分子且为倍数关系
elif temp < b and b % temp == 0:
b /= temp
print(temp, '/', b)
# 一般情况
else:
print(temp, '/', b)
add(1,4,2,3)
sub(1,1,1,8)
结果
11 / 12
7 / 8
aironeng
发表于 2020-11-10 08:49:31
学习一下
sanchuan555
发表于 2021-1-20 12:07:42
class Fractioncount:
def __init__(self,a,b,c,d):
self.a=a
self.b=b
self.c=c
self.d=d
def add(self):
A = int(self.a)
B = int(self.b)
C = int(self.c)
D = int(self.d)
x = A * D + C * B
y = B * D
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller + 1):
if ((x % i == 0) and (y % i == 0)):
hcf = i
fz = x / hcf
fm = y / hcf
return '%d/%d' % (fz, fm)
def sub(self):
A = int(self.a)
B = int(self.b)
C = int(self.c)
D = int(self.d)
x = A * D - C * B
y = B * D
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller + 1):
if ((x % i == 0) and (y % i == 0)):
hcf = i
fz = x / hcf
fm = y / hcf
return '%d/%d' % (fz, fm)
def mul(self):
A = int(self.a)
B = int(self.b)
C = int(self.c)
D = int(self.d)
x = A * B
y = B * D
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller + 1):
if ((x % i == 0) and (y % i == 0)):
hcf = i
fz = x / hcf
fm = y / hcf
return '%d/%d' % (fz, fm)
def div(self):
A = int(self.a)
B = int(self.b)
C = int(self.c)
D = int(self.d)
x = A * D
y = B * C
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller + 1):
if ((x % i == 0) and (y % i == 0)):
hcf = i
fz = x / hcf
fm = y / hcf
return '%d/%d' % (fz, fm)
w=Fractioncount(3,2,3,4)
print(w.div(),w.mul(),w.sub(),w.add())
qq1151985918
发表于 2021-1-20 12:17:56
本帖最后由 qq1151985918 于 2021-1-20 13:24 编辑
def getDicisor(x ,y):
if x % y == 0:
return y
else:
return getDicisor(y ,x%y)
def add(a ,b ,c ,d):
if b * d == 0:
print("输入错误!")
return
up = a * d + b * c
down = b * d
dicisor = getDicisor(up,down)
up = up / dicisor
down = down / dicisor
text = "%d/%d + %d/%d = %d/%d"%(a,b,c,d,up,down)
print(text)
def minus(a ,b ,c ,d):
if b * d == 0:
print("输入错误!")
return
up = a * d - b * c
down = b * d
dicisor = getDicisor(up,down)
up = up / dicisor
down = down / dicisor
text = "%d/%d - %d/%d = %d/%d"%(a,b,c,d,up,down)
print(text)
wwwwwise
发表于 2021-2-20 09:30:41
1
王重玖
发表于 2021-2-20 10:09:09
def addx(a,b,c,d):
print(str(a+b)+"/"+str(c+d))
addx(1,2,3,4)