Пример #1
0
 def is_repeat(self, express_set, expression):
     """
     判断重复方法
     :param
         express_set: 表达式集合
         expression: 生成的表达式
     :return: True or False
     """
     target_exp_suffix = to_suffix(expression)
     target_exp_binary_tree = generate_binary_tree(target_exp_suffix)
     for item in express_set:
         source_exp_suffix = to_suffix(item)
         source_exp_binary_tree = generate_binary_tree(source_exp_suffix)
         if tree_is_same(target_exp_binary_tree) == tree_is_same(
                 source_exp_binary_tree):
             return True
     return False
Пример #2
0
def check_answer(exercisefile, answerfile):
    """
    校对答案
    :param 
        exercisefile: 练习题文件
        answerfile: 答案文件
    :return: None
    """
    wrong_num = 0
    correct_num = 0
    exercise_answer = []
    correct_list = []  # 正确题目序号
    wrong_list = []  # 错误题目序号

    try:
        with open(exercisefile, 'r', encoding='utf-8') as f:
            for line in f:
                #匹配出正则表达式
                exp_str = re.findall(r'\d+: (.*) = \n', line)
                if exp_str:
                    exp = exp_str[0]
                else:
                    continue
                temp = str(suffix_to_value(to_suffix(exp)))
                exp_value = change_fraction(temp)
                exercise_answer.append(exp_value)
    except IOError:
        print('please check if the path is correct')

    #判断表达式列表是否为空
    try:
        with open(answerfile, 'r', encoding='utf-8') as f:
            for i, line in enumerate(f):
                ans_str = re.findall(r'\d+: (.*)\n', line)
                #容错
                if ans_str:
                    ans = ans_str[0]
                else:
                    continue
                #判断是否正确
                if ans == exercise_answer[i]:
                    correct_num += 1
                    correct_list.append(i + 1)
                else:
                    wrong_num += 1
                    wrong_list.append(i + 1)
        with open('Grade.txt', 'w+', encoding='utf-8') as f:
            correct_str = 'Correct: ' + str(correct_num) + ' ' + str(
                correct_list) + '\n'
            wrong_str = 'Wrong: ' + str(wrong_num) + ' ' + str(wrong_list)
            f.write(correct_str)
            f.write(wrong_str)
    except IOError:
        print('please check if the path is correct')
Пример #3
0
def expression_result(exp_list):
    """
    求表达式的结果
    :param exp_list: 表达式列表
    :return: None
    """
    for i, exp in enumerate(exp_list):
        order_str = str(i + 1)
        exp_value = str(suffix_to_value(to_suffix(exp))) + '\n'
        result = order_str + ': ' + exp_value
        with open('Answer.txt', 'a+', encoding='utf-8') as f:
            f.write(result)
Пример #4
0
    def generate(self, config):
        """
        表达式生成主函数
        :param
            config: config类
        :return: 
        """
        exp_num = config.exp_num
        exp_list = []
        i = 0
        while i < exp_num:
            random_num_operation = randint(1, config.max_num_of_oper)
            is_need_parentheses = randint(0, 1)
            number_of_oprand = random_num_operation + 1  #操作数比操作符的数目多1
            exp = []
            for j in range(random_num_operation + number_of_oprand):
                if j % 2 == 0:
                    #随机生成操作数
                    exp.append(
                        self.generate_operand(randint(0, 3), config.num_range))
                    if j > 1 and exp[j - 1] == '÷' and exp[j] == '0':
                        while True:
                            exp[j - 1] = self.generate_operation()
                            if exp[j - 1] == '÷':
                                continue
                            else:
                                break
                else:
                    #生成运算符
                    exp.append(self.generate_operation())

            #判断是否要括号
            if is_need_parentheses and number_of_oprand != 2:
                expression = " ".join(
                    self.generate_parentheses(exp, number_of_oprand))
            else:
                expression = " ".join(exp)

            #判断是否有重复
            if self.is_repeat(exp_list, expression) or suffix_to_value(
                    to_suffix(expression)) == False:
                continue
            else:
                exp_list.append(expression)
            print('第 %d 道题' % int(i + 1))
            i = i + 1

        return exp_list
    """
    if not root.left:
        if not root.right:
            return root.value
    elif root.value == '+' or root.value == 'x':
        left = tree_is_same(root.left)
        right = tree_is_same(root.right)
        if operator.le(left, right):
            #print(root.value + left + right)
            return root.value + left + right
        else:
            return root.value + right + left
    else:
        return root.value + tree_is_same(root.left) + tree_is_same(root.right)


if __name__ == '__main__':
    exp1 = '3 + ( 2 ÷ 1 )'
    exp2 = '1 + 2 + 3'
    re1 = suffix_expression.to_suffix(exp1)
    re2 = suffix_expression.to_suffix(exp2)
    print(re1)
    print(re2)
    res1 = generate_binary_tree(re1)
    res2 = generate_binary_tree(re2)
    r1 = tree_is_same(res1)
    r2 = tree_is_same(res2)
    print(r1)
    print(r2)
    if r1 == r2:
        print("12")