master
-WY5D.:aaJAcRBz~ 1 year ago
parent 66ab36afbf
commit 7f4c7bfac8

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1 @@
main.py

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.8" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ykaxem9u.iml" filepath="$PROJECT_DIR$/.idea/ykaxem9u.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,201 @@
# -*- encoding: utf-8 -*-
"""
@Author: packy945
@FileName: TreeNode.py
@DateTime: 2023/5/15 14:27
@SoftWare: PyCharm
"""
import re
import tkinter as tk
from data import *
import tkinter.messagebox
class Tree:
def __init__(self):
self.bc = 70
self.Node = []
self.rootID = None
self.deepth = []
self.place = []
self.mark = [0] * 110
self.ck = []
def check(self):
self.ck = [1] * len(self.Node)
for N in self.Node:
if N[3]:
self.ck[N[3]] = 0
if N[4]:
self.ck[N[4]] = 0
def remark(self):
self.mark = [0] * 110
def deep(self):
flag = 0
for i in range(len(self.Node)):
if self.Node[i][1] == 'Value':
self.deepth[i] = 0
for i in range(len(self.Node)):
if self.deepth[i] == -1:
flag = 1
if self.Node[i][3] != None and self.Node[i][4] != None:
# print(self.Node[i][3], self.Node[i][4])
self.deepth[i] = max(self.deepth[self.Node[i][3]], self.deepth[self.Node[i][4]]) + 1
# print(self.deepth)
if flag:
self.deep()
def calculate(self, node):
# print(node)
if node[1] == 'Value':
return node[5] * 1.0
elif node[1] == 'Operator':
a = self.calculate(self.Node[node[3]])
b = self.calculate(self.Node[node[4]])
if node[2] == '^':
return pow(int(a), int(b))
else:
# print(a,node[2],b)
return eval(str(a) + node[2] + str(b))
def Aexp(self, node):
if node[1] == 'Value':
return str(node[5])
elif node[1] == 'Operator':
a = self.Aexp(self.Node[node[3]])
b = self.Aexp(self.Node[node[4]])
# print(a,node[2],b)
ans = '(' + str(a) + node[2] + str(b) + ')'
# print(ans)
return ans
tree = Tree()
cur = 0
class TreeNode:
def __init__(self, Type, value, left=None, right=None, color='方块'):
global cur
self.NodeID = cur # 节点编号
cur += 1
self.NodeType = Type # 节点类型
self.Ops = None # 运算符类型
self.left = left
self.right = right
self.LeftNodeID = None
self.RightNodeID = None
if self.left:
self.LeftNodeID = left.NodeID # 左节点编号
if self.right:
self.RightNodeID = right.NodeID # 右节点编号
self.FaceValue = None # 节点数值
self.FaceColor = color # 节点花色
if Type == 'Value':
self.FaceValue = value
else:
self.Ops = value
tree.Node.append([self.NodeID, self.NodeType, self.Ops, self.LeftNodeID, self.RightNodeID, self.FaceValue, self.FaceColor])
def build_ast(formula):
'''
建立语法树
:param formula: 表达式表达式的花色
:return: 建立好的语法树
'''
# 去掉空格
global tree, cur
try:
expr, color = formula
except:
expr = formula
color = ['方块'] * 20
if len(expr) == 0:
tk.messagebox.showinfo('', '表达式不存在')
return
cur = 0
tree.__init__()
expr = expr.replace(' ', '')
# 将所有数字和符号分离出来
tokens = re.findall(r'\d+|[()+\-*/^.]', expr)
i = 0
for token in tokens:
if tokens[i] == '.':
tokens[i - 1] = tokens[i - 1] + tokens[i] + tokens[i + 1]
del tokens[i: i + 2]
i += 1
# 定义优先级
precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
# 创建一个操作符栈和一个节点栈
op_stack = []
node_stack = []
color_cur = 0
# 遍历所有 token
for token in tokens:
if token.isdigit():
# print(cur, color[cur])
node_stack.append(TreeNode('Value', int(token), color=color[color_cur]))
color_cur += 1
elif token.count('.') == 1:
node_stack.append(TreeNode('Value', float(token), color=color[color_cur]))
color_cur += 1
elif token in '+-*/^':
while op_stack and op_stack[-1] != '(' and precedence[token] <= precedence[op_stack[-1]]:
op = op_stack.pop()
right = node_stack.pop()
left = node_stack.pop()
node_stack.append(TreeNode('Operator', op, left, right))
op_stack.append(token)
elif token == '(':
op_stack.append(token)
elif token == ')':
while op_stack and op_stack[-1] != '(':
op = op_stack.pop()
right = node_stack.pop()
left = node_stack.pop()
node_stack.append(TreeNode('Operator', op, left, right))
op_stack.pop()
# 处理剩下的操作符
while op_stack:
op = op_stack.pop()
right = node_stack.pop()
left = node_stack.pop()
node_stack.append(TreeNode('Operator', op, left, right))
# 返回抽象语法树的根节点
tree.rootID = node_stack[0].NodeID
# print(tree.rootID)
# print(tree.Node)
return node_stack[0]
def print_ast(node:TreeNode, prefix='', is_left=True):
if node:
if node.NodeType == 'Value':
s = str(node.FaceValue)
else:
s = str(node.Ops)
print(prefix + ('├── ' if is_left else '└── ') + s)
print_ast(node.left, prefix + ('' if is_left else ' '), True)
print_ast(node.right, prefix + ('' if is_left else ' '), False)

@ -0,0 +1,14 @@
from PIL import Image, ImageDraw, ImageFont
# 创建一个图像
image = Image.new("RGB", (100, 100), "white")
draw = ImageDraw.Draw(image)
# 加载默认字体
font = ImageFont.load_default()
# 要获取文本的大小
text = "Hello, World!"
text_size = draw.textsize(text, font)
print("Text size:", text_size)

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

@ -0,0 +1,101 @@
# -*- encoding: utf-8 -*-
"""
@Author: packy945
@FileName: data.py
@DateTime: 2023/5/11 14:36
@SoftWare: PyCharm
"""
from itertools import permutations
import random
import tkinter as tk
MARK = '#ff0000'
NOMARK = '#cccccc'
class Aexp:
def __init__(self, a, b, c, d):
self.Aexp_cur = 0
self.num = 0
self.card = [a, b, c, d]
self.answer = []
self.Color = []
self.Aexp(a, b, c, d)
def refresh(self, a, b, c, d):
self.Aexp_cur = 0
self.num = 0
self.card = [a, b, c, d]
# print(self.card)
self.answer = []
self.Color = []
self.Aexp(a, b, c, d)
def Aexp(self, one, two, three, four):
'''
输入四个数求24点
:param one:
:param two:
:param three:
:param four:
:return: 求解24点结果若无法得出则返回无法求得结果
'''
self.Aexp_cur = 0
my_list = [one, two, three, four]
result = [c for c in permutations(my_list, 4)]
list2 = [] # 算出24的排列组合的列表
symbols = ["+", "-", "*", "/"]
flag = False
for one, two, three, four in result:
for s1 in symbols:
for s2 in symbols:
for s3 in symbols:
express = ["(({0}{1}{2}){3}{4}){5}{6}".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
"({0}{1}{2}){3}({4}{5}{6})".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
"({0}{1}({2}{3}{4})){5}{6}".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
"{0}{1}(({2}{3}{4}){5}{6})".format(one[1], s1, two[1], s2, three[1], s3, four[1]),
"{0}{1}({2}{3}({4}{5}{6}))".format(one[1], s1, two[1], s2, three[1], s3, four[1])]
# if str(one) + str(two) + str(three) + str(four) == "8383":
# print(express)
for e in express:
try:
if round(eval(e), 6) == 24:
e = del_parentheses(e)
list2.append(e)
self.Color.append([one[0], two[0], three[0], four[0]])
flag = True
except ZeroDivisionError:
pass
if flag:
self.answer = list2
self.num = len(list2)
# return list3
else:
self.answer = ["无法得出24点"]
self.Color.append(None)
self.num = 0
#删除最外层括号
def del_parentheses(e):
if e[0] == '(' and e[-1] == ')':
try:
eval(e[1:len(e)-1])
except:
pass
else:
if round(eval(e[1:len(e)-1]),6) == 24:
return e[1:len(e)-1]
return e
if __name__ == '__main__':
ans = Aexp(1, 2, 4, 6)
for i in ans.answer:
print(i)
# print(ans)
pass

@ -0,0 +1,20 @@
(8 + 3) * (6 - 2)
9 - (4 + 2) * (7 - 1)
(2 * 3) + (5 - 1) / 4
((7 + 2) - 1) * (8 / 4)
9 * (3 - 1) + (8 / 4)
((9 - 1) / (4 + 1)) + 7 * 3
(8 - 3) * ((6 + 2) / 4) + 9
((7 * 2) - 4) * ((9 + 3) / 5) + 1
(9 + 4) * ((7 - 2) / (8 + 1)) - 6
((5 - 3) * (9 + 7)) / (8 - 2) + 6
(6 + 2) * (9 - 3)
8 - (5 + 1) * (7 - 2)
(3 * 4) + (8 - 2) / 5
((6 + 3) - 2) * (7 / 4)
8 * (2 - 1) + (9 / 3)
((4 - 2) / (9 + 1)) + 5 * 7
(7 - 2) * ((5 + 3) / 6) + 4
((6 * 3) - 1) * ((8 + 2) / 5) + 9
(19 + 21) * ((14 - 11) / (7 + 3)) - 5
((81 - 45) * (36 + 21)) / (18 - 6) + 7

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 779 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 765 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 830 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 739 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 733 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 793 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save