是上一个http://kidscode.cn/archives/16988/islook/1的GUI版

import tkinter as tk
from tkinter import messagebox


def permutations(array):
    permutation = []
    for first_num in array:
        array2 = array[:]
        array2.remove(first_num)
        for second_num in array2:
            array3 = array2[:]
            array3.remove(second_num)
            for third_num in array3:
                array4 = array3[:]
                array4.remove(third_num)
                forth_num = array4[0]
                permutation.append([first_num, second_num, third_num, forth_num])
    return permutation


def calc(lst):
    result = permutations(lst)
    operators = ["+", "-", "*", "/"]
    list2 = []

    for list1 in result:
        number1 = list1[0]
        number2 = list1[1]
        number3 = list1[2]
        number4 = list1[3]
        for operator1 in operators:
            for operator2 in operators:
                for operator3 in operators:
                    expression = [
                        "(({0}{1}{2}){3}{4}){5}{6}".format(number1, operator1, number2, operator2, number3,
                                                           operator3, number4),
                        "({0}{1}{2}){3}({4}{5}{6})".format(number1, operator1, number2, operator2, number3,
                                                           operator3, number4),
                        "(({0}{1}({2}{3}{4})){5}{6})".format(number1, operator1, number2, operator2, number3,
                                                             operator3, number4),
                        "{0}{1}(({2}{3}{4}){5}{6})".format(number1, operator1, number2, operator2, number3,
                                                           operator3, number4),
                        "{0}{1}({2}{3}({4}{5}{6}))".format(number1, operator1, number2, operator2, number3,
                                                           operator3, number4),
                        "{0}{1}{2}{3}{4}{5}{6}".format(number1, operator1, number2, operator2, number3,
                                                       operator3, number4)
                    ]

                    for e in expression:
                        try:
                            if round(eval(e), 2) == 24:
                                list2.append(e)
                        except ZeroDivisionError:
                            pass

    list3 = list(set(list2))
    return list3


class App:
    def __init__(self, root):

        self.root = root
        self.root.geometry('600x400')
        self.root.title('24点-计算器')

        self.input_frame = tk.Frame(self.root)
        self.input_frame.place(relx=0.5, rely=0.2, relwidth=0.8, relheight=0.2, anchor='n')
        self.num_entry1 = tk.Entry(self.input_frame, width=5, font=('Arial', 16))
        self.num_entry2 = tk.Entry(self.input_frame, width=5, font=('Arial', 16))
        self.num_entry3 = tk.Entry(self.input_frame, width=5, font=('Arial', 16))
        self.num_entry4 = tk.Entry(self.input_frame, width=5, font=('Arial', 16))
        self.num_entry1.place(relx=0.1, rely=0.5, anchor='center')
        self.num_entry2.place(relx=0.3, rely=0.5, anchor='center')
        self.num_entry3.place(relx=0.5, rely=0.5, anchor='center')
        self.num_entry4.place(relx=0.7, rely=0.5, anchor='center')
        self.num_label1 = tk.Label(self.input_frame, text='第一个数字', font=('Arial', 12))
        self.num_label2 = tk.Label(self.input_frame, text='第二个数字', font=('Arial', 12))
        self.num_label3 = tk.Label(self.input_frame, text='第三个数字', font=('Arial', 12))
        self.num_label4 = tk.Label(self.input_frame, text='第四个数字', font=('Arial', 12))
        self.num_label1.place(relx=0.1, rely=0.2, anchor='center')
        self.num_label2.place(relx=0.3, rely=0.2, anchor='center')
        self.num_label3.place(relx=0.5, rely=0.2, anchor='center')
        self.num_label4.place(relx=0.7, rely=0.2, anchor='center')

        # 添加计算按钮、结果标签和滚动条
        self.compute_button = tk.Button(self.root, text='计算', font=('Arial', 12),command=self.compute)
        self.compute_button.place(relx=0.5, rely=0.45, anchor='n')

    def compute(self):
        # 获取输入框中的数字
        nums = []
        try:
            nums.append(int(self.num_entry1.get()))
            nums.append(int(self.num_entry2.get()))
            nums.append(int(self.num_entry3.get()))
            nums.append(int(self.num_entry4.get()))
        except ValueError:
            messagebox.showerror('Error', '请输入整数数字!')
            return

        answer = calc(nums)
        if len(answer) == 0:
            messagebox.showwarning('无解', '此问题无解!')
        else:
            ans = "\n".join(answer)
            messagebox.showinfo('求解成功!', f'该问题的解为:            \n{ans}')


root = tk.Tk()
app = App(root)
root.mainloop()

本站作者已申明原创,禁止转载!

文章内容属作者个人观点,不代表本站立场,如有侵权立删。

   口袋儿题库-青少儿编程自测题库