排列组合运算器
def check(n,m):if n<m:
print("上标不能大于下标!")
return 1
return 0
def combination(n, m):
"""计算简单组合问题的函数"""
if check(n, m):
return 0
a=1
b=1
if m>n/2:
m=n-m
for i in range(1,m+1):
a *= n
b *= i
n -= 1
return a/b
def permutation(n, m):
"""计算简单排列问题"""
if check(n, m):
return 0
a=1
for i in range(0, m):
a *= n
n -= 1
return a
def bind(n, m):
"""捆绑法计算简单的问题"""
if check(n, m):
return 0
a=n-m+1
a=permutation(a, a)
b=permutation(m, m)
return a*b
def interpolation(n, m):
"""插空法计算简单的问题"""
if check(n, m):
return 0
a=permutation(n, n)
b=permutation(n+1, m)
return a*b
def plate_insert(n, m, same=True):
"""插板法解决一般问题"""
if check(n, m):
return 0
if same:
return combination(n-1, m)
else :
a = permutation(n, n)
a *= permutation(n-1,m)
return a
def power(n, m):
"""乘方运算"""
a=1
for i in range(0, m):
a *= n
return a
import easygui as g
while 1:
choice = ("排列", "组合", "混合运算", "捆绑法", "插空法",\
"插板法")
yn=("继续", "退出")
msg = "请选择公式:"
title = "概率统计"
select = g.choicebox(msg, title, choice)
if select == "排列":
title += " —— " + select
while 1:
n=g.integerbox('请输入A的下标 n:',
title, upperbound=999999999)
if n==None:
break
m=g.integerbox('请输入A的上标 m:',
title, upperbound=n)
if m==None:
break
p=ps.permutation(n, m)
msg = "上标:m="+str(m)+\
"\n下标: n="+str(n)+\
"\n\n结果:"+str(p)
if g.ccbox(msg, title, yn) == False:
break
elif select == "组合":
title += " —— " + select
while 1:
n=g.integerbox('请输入C的下标 n:',
title, upperbound=999999999)
if n==None:
break
m=g.integerbox('请输入C的上标 m:',
title, upperbound=n)
if m==None:
break
p=ps.combination(n, m)
msg = "上标:m="+str(m)+\
"\n下标: n="+str(n)+\
"\n\n结果:"+str(p)
if g.ccbox(msg, title, yn) == False:
break
elif select == "捆绑法":
title += " —— " + select
while 1:
n=g.integerbox('请输入总数 n:',
title, upperbound=999999999)
if n==None:
break
m=g.integerbox('请输入捆绑个数 m:',
title, upperbound=n)
if m==None:
break
p=ps.bind(n, m)
msg = "捆绑数:m="+str(m)+\
"\n总数: n="+str(n)+\
"\n\n结果:"+str(p)
if g.ccbox(msg, title, yn) == False:
break
elif select == "插空法":
title += " —— " + select
while 1:
n=g.integerbox('请输入被插空者数 n:',
title, upperbound=999999999)
if n==None:
break
m=g.integerbox('请输入插空者数 m:',
title, upperbound=n)
if m==None:
break
p=ps.interpolation(n, m)
msg = "插空者:m="+str(m)+\
"\n被插空者: n="+str(n)+\
"\n\n结果:"+str(p)
if g.ccbox(msg, title, yn) == False:
break
elif select == "插板法":
title += " —— " + select
while 1:
n=g.integerbox('请输入被插板者数 n:',
title, upperbound=999999999)
if n==None:
break
m=g.integerbox('请输入插板数 m:',
title, upperbound=n)
if m==None:
break
p=ps.plate_insert(n, m)
msg = "插板数:m="+str(m)+\
"\n被插板者: n="+str(n)+\
"\n\n结果:"+str(p)
if g.ccbox(msg, title, yn) == False:
break
elif select == "混合运算":
title += " —— " + select
a=1
symbol=
number = []
method = []
index=0
while 1:
msg="请选择运算方法:"
choice=("排列", "组合", "数","乘方")
choice=g.buttonbox(msg, title, choice)
if choice == "排列":
n=g.integerbox('请输入A的下标 n:',
title, upperbound=999999999)
if n==None:
continue
m=g.integerbox('请输入A的上标 m:',
title, upperbound=n)
if m==None:
continue
b=ps.permutation(n, m)
number.append((n,m))
method.append(choice)
elif choice == "组合":
n=g.integerbox('请输入C的下标 n:',
title, upperbound=999999999)
if n==None:
continue
m=g.integerbox('请输入C的上标 m:',
title, upperbound=n)
if m==None:
continue
b=ps.combination(n, m)
number.append((n,m))
method.append(choice)
elif choice == "乘方":
n=float(g.enterbox('请输入底数n:',
title))
if n==None:
continue
m=int(g.enterbox('请输入指数 m:',
title))
if m==None:
continue
b=ps.power(n, m)
number.append((n,m))
method.append(choice)
else:
b=float(g.enterbox("请输入数字 b:",\
title, ))
if b==None:
continue
number.append(b)
method.append(choice)
msg = "请选择运算符号:"
choice=("+", "-", chr(0x00d7), chr(0xf7), "=")
choice=g.buttonbox(msg, title, choice)
symbol.append(choice)
if symbol=='+':
a += b
elif symbol=='-':
a -= b
elif symbol==chr(0x00d7):
a *= b
elif symbol==chr(0xf7):
a /= b
index += 1
if symbol=='=':
index=0
msg = ""
while 1:
if method=="组合":
msg += "C"
elif method=="排列":
msg += "A"
elif method=="乘方":
msg += "P"
msg += str(number)
index += 1
msg += symbol
ifsymbol=='=':
break
msg += '\n结果:' + str(a)
if g.ccbox(msg, title, yn) == False:
break
else:
break
页:
[1]