本文主要是介绍生成100以内的,3个数的加减法混合技巧运算,带括号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
生成100以内的,3个数的加减法混合技巧运算,带括号。直接生成word格式,可直接打印
共有3种模式,分别如下
AB-CD+ED 59-43+33 59-(43-33)=59-10=49
AB-(CD+EB) 77-(59+17) 77-59-17=77-17-59=60-59=1
AB+(CD-EF) 27+(73 - 29) 27+73-29=100-59=41
import os
import sys
import random
import subprocess
import importlib.util
# 全局变量,用于存储导入的模块
global Document, Pt, WD_PARAGRAPH_ALIGNMENT, WD_SECTION_START, WD_ORIENTATIONg_filename = "100以内的技巧运算"
g_num_questions_min = 100 #word格式时,会强制塞满最后一页,所以会等于或大于这个数
g_is_include_result = False #如果改为True,会生成算式和答案
g_blank_lines_num = 4 #行与行之间的空行数,方便在空白处做答
g_one_page_question_num = 3 * 6 #根据这个数来进行换行,每而3列,6行。g_questions = []
def chgWorkDir():curFullPath = os.path.realpath(__file__)curWorkPath, file_name = os.path.split(curFullPath)print(curWorkPath)os.chdir(curWorkPath) print('chgWorkDir to :' + curFullPath)def install_package(package):try:if importlib.util.find_spec(package) == None:print(f"\n\n库 {package} 未安装,正在尝试安装...\n\n")subprocess.check_call([sys.executable, "-m", "pip", "install", package])else:print(f"库 {package} 已安装")except ImportError:print(f"\n\n库 {package} 未安装,正在尝试安装...\n\n")subprocess.check_call([sys.executable, "-m", "pip", "install", package])print(f"库 {package} 安装完成")def init_environment():global Document, Pt, WD_PARAGRAPH_ALIGNMENT, WD_SECTION_START, WD_ORIENTATION# install_package("python-docx")from docx import Documentfrom docx.shared import Ptfrom docx.enum.text import WD_PARAGRAPH_ALIGNMENTfrom docx.enum.section import WD_SECTION_STARTfrom docx.enum.section import WD_ORIENTATION# AB-CD+ED 59-43+33 59-(43-33)=59-10=49
def generate_math_questions_1(num_questions):global g_questionsquestions_count = 0while(1):try:A = random.randint(4, 9)B = random.randint(2, 9)C = random.randint(2, 9)D = random.randint(4, C-1) # CD > ED, 保证CD大于EDE = random.randint(2, D)X = A*10+BY = C*10+DZ = E*10+Dexpression = f"{X} - {Y} + {Z}"result = X - Y + Z# 检查结果是否小于100if result < 0 or result > 100:continueif B >= D:continueif A == C:continueg_questions.append((expression, result))questions_count += 1if questions_count == num_questions: break;except:continue;return
#AB-(CD+EB) 77-(59+17) 77-59-17=77-17-59=60-59=1
def generate_math_questions_2(num_questions):global g_questionsquestions_count = 0while(1):try:A = random.randint(5, 9)B = random.randint(1, 9)C = random.randint(1, 9)D = random.randint(1, 9)E = random.randint(1, 9)X = A*10+BY = C*10+DZ = E*10+Bexpression = f"{X} - ({Y} + {Z})"result = X - (Y + Z)# 检查结果是否小于100if result <= 10 or result > 100:continueif B + D <= 10:continueg_questions.append((expression, result))questions_count += 1if questions_count == num_questions: break;except:continue;return#AB+(CD-EF) 27+(73 - 29) 27+73-29=100-59=41
def generate_math_questions_3(num_questions):global g_questionsquestions_count = 0while(1):try:A = random.randint(1, 9)B = random.randint(1, 9)C = random.randint(1, 9)D = random.randint(1, 9)E = random.randint(1, 9)F = random.randint(1, 9)X = A*10+BY = C*10+DZ = E*10+Fexpression = f"{X} + ({Y} - {Z})"result = X + (Y - Z)# 检查结果是否小于100if result <= 10 or result > 100:continueif B + D != 10:continueif D == F:continueif Y - 10 <= Z:continueg_questions.append((expression, result))questions_count += 1if questions_count == num_questions: break;except:continue;return
def save_questions_to_txt(questions, filename, question_num, is_include_result = False):filename += ".txt";with open(filename, 'w') as file:for i, q in enumerate(questions, 1):expression, result = qif is_include_result:express = str(i).rjust(2) + f" 、{expression} = {result}\n"else:express = str(i).rjust(2) + f" 、{expression}\n"file.write(express)if (i > question_num - 1):break;def is_near_bottom_margin(doc):for index, para in enumerate(doc.paragraphs):# 获取段落的对齐方式alignment = para.alignment# 检查段落格式是否存在if para.paragraph_format.space_after is not None:# 获取段落的下边距space_after = para.paragraph_format.space_after.pt# 判断对齐方式和下边距位置if alignment == WD_PARAGRAPH_ALIGNMENT.CENTER and space_after > Pt(70):print(f"第{index+1}段落:居中对齐且下边距大于12pt")return Falseelse:print(f"第{index+1}段落:左对齐且下边距小于12pt")return True#如果没有到达页尾,并且超过question_num时,添加空页,则直接返回,表示生成题目数量完成
#如果只是到达页尾,并没有超过question_num,则添加空页
#如果只是到达页中,则添加空行
def add_blank_lines(doc, i, question_num):if i % g_one_page_question_num == 0 and i > 0:if (i >= question_num):return Truedoc.add_page_break()else:for _ in range(g_blank_lines_num - 1):doc.add_paragraph("")return Falsedef save_questions_to_word(questions, filename, question_num, is_include_result = False):filename += ".docx";# 创建一个新的Word文档doc = Document()# 添加g_titile,并设置字体加粗、居中,2号字体content1 = doc.add_paragraph(g_filename)content1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTERcontent1.runs[0].bold = Truecontent1.runs[0].font.size = Pt(20)# 设置第一页的节属性section1 = doc.sections[-1]section1.start_type = WD_SECTION_START.CONTINUOUSsection1.orientation = WD_ORIENTATION.PORTRAITsection1.left_margin = Pt(60) section1.right_margin = section1.left_margin# 添加内容line_context = ""for i, q in enumerate(questions, 1):expression, result = qif is_include_result:express = str(i).rjust(2) + f" 、{expression} = {result}"else:express = str(i).rjust(2) + f" 、{expression}"# 在每个内容之间添加空行if i % 3 == 0:line_context += express;doc.add_paragraph(line_context)line_context = ""if add_blank_lines(doc, i, question_num):break;else:line_context += express + "\t\t\t";if line_context != "":doc.add_paragraph(line_context)# 保存文档doc.save(filename)if __name__ == '__main__':print('生成100以内的,3个数的加减法混合技巧运算,带括号 ...\n')chgWorkDir()init_environment()generate_math_questions_1(g_num_questions_min)generate_math_questions_2(g_num_questions_min)generate_math_questions_3(g_num_questions_min)random.shuffle(g_questions)random.shuffle(g_questions)save_questions_to_txt(g_questions, g_filename, g_num_questions_min, g_is_include_result)save_questions_to_word(g_questions, g_filename, g_num_questions_min, g_is_include_result)
这篇关于生成100以内的,3个数的加减法混合技巧运算,带括号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!