运行效果
优点
- 集合内可添加字母作为元素
- 集合元素显示较为友好
- easygui图形界面注重用户体验
- 可多次计算,十分便捷
缺点
- 字号太小,字体太丑
- 源代码无注释且较乱
- 过多的元素无法在图形界面显示
代码
from easygui import *
def encode(A):
A = A.strip('{}')
A = A.replace(' ','')
A = A.split(',')
for i in A:
if i == '':
A.remove(i)
if len(A) == 1 and A[0] == '∅':
return set()
return set(A)
def ChooseElement(B, i, Len):
if i == 0:
return('∅')
code = '{0:>{1}}'.format(bin(i)[2:],Len)
X = list()
count = 0
for each in code:
if each == '1':
X.append(B[count])
count += 1
X.sort()
return '{' + str(X)[1:-1] + '}'
def PowerSet(A):
Len = len(A)
num = 2 ** Len
C = set()
B = list(A)
B.sort()
for i in range(num):
C.add(ChooseElement(B, i, Len))
return C
def output(C):
if len(C) == 0:
return '∅'
else:
C = list(C)
C.sort()
C = str(C)
C = C.replace('"','')
C = C.replace("'",'')
return '{' + C[1:-1] + '}'
mode = 1
A = {1,2,3}
B = {1,4}
while True:
if mode != 3:
tte = '集合计算器'
msg1 = '请输入要计算的两个集合:'
msg1 += '(请使用半角的逗号作为分隔)'
A, B = (multenterbox(msg = msg1, title = tte, fields = ['A', 'B'], values=[output(A), output(B)]))
A = encode(A)
B = encode(B)
Choices1 = ['A∩B','A∪B','A–B','B–A','A⊕B','P(A)','改变A或B的值']
msg2 = 'A = ' + output(A) + '\n'
msg2 += 'B = ' + output(B) + '\n'
msg2 += '请选择你要进行的操作:\n'
choice = buttonbox(msg = msg2, title = tte, choices = Choices1)
if choice == 'A∩B':
C = A & B
elif choice == 'A∪B':
C = A | B
elif choice == 'A–B':
C = A - B
elif choice == 'B–A':
C = B - A
elif choice == 'A⊕B':
C = A ^ B
elif choice == 'P(A)':
C = PowerSet(A)
if choice == '改变A或B的值':
mode = 2
else:
mode = 1
if mode != 2:
if len(C) < 32:
msg3 = choice + ' = ' + output(C)
else:
msg3 = '太长了,写不下!' + choice + '共有' + str(len(C)) + '项'
Choices2 = ['继续运算', '改变A或B的值', '退出']
Choice = buttonbox(msg = msg3, title = tte, choices = Choices2)
if Choice == '退出':
break
elif Choice == '继续运算':
mode = 3
elif Choice == '改变A或B的值':
mode = 1
else:
break