-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.py
executable file
·281 lines (235 loc) · 7.64 KB
/
cal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#! /usr/bin/env python3
"""
Author: neagle2009
Purpose: 生成计算训练算式
!!! 文件名不能与包名相同, 否则会把文件当成包引入, 出现各种属性找不到 !!!
"""
# from ctypes.wintypes import RGB
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.text import WD_LINE_SPACING
import argparse
import random
import datetime
level = 1
rangeType = 2
def getArgs():
""" 参数处理 """
parser = argparse.ArgumentParser(description='加减法算式生成')
parser.add_argument('-l',
'--level',
metavar='Level',
type=int,
default=1,
help='难度控制 1: 常规(退位进位都有); 2: 困难模式, 仅退位,进位')
parser.add_argument('-c',
'--count',
metavar='Count',
type=int,
help='生成算式数目')
parser.add_argument('-t',
'--type',
metavar='Type',
type=int,
default=3,
help='生成类型, 1: 加法;\
2: 减法; 3: 加减混合;4: 乘法; 5: 除法; 6: 乘除混合')
parser.add_argument('-r',
'--range',
help='生成算式范围1: 一位数(TODO); 2: 两位数; 3: 三位数;',
metavar='int',
type=int,
default=2)
return parser.parse_args()
def GetPlusCal(num):
"""获取加法算式"""
list = []
if rangeType == 3:
rangeMin = 100
rangeMax = 999
elif rangeType == 2:
rangeMin = 10
rangeMax = 99
else:
rangeMin = 10
rangeMax = 99
while len(list) < num:
a = random.randint(rangeMin, rangeMax)
b = random.randint(rangeMin, rangeMax)
if (a + b) > (rangeMax + 1):
continue
# 进位控制
p1 = a % 10
p2 = b % 10
if (level == 2) and ((p1 + p2) <= 10):
continue
result = str(a) + ' + ' + str(b) + ' = '
if result in list:
continue
list.append(result)
return list
def GetMultiCal(num):
"""获取乘法算式"""
list = []
while len(list) < num:
if level == 1:
a = random.randint(2, 9)
b = random.randint(2, 9)
result = str(a) + ' × ' + str(b) + ' = '
elif level == 2:
a = random.randint(2, 9)
max = 100 // a
b = random.randint(10, max)
result = str(a) + ' × ' + str(b) + ' = '
if result in list:
continue
list.append(result)
return list
def GetDivCal(num):
"""获取除法"""
list = []
result = ''
while len(list) < num:
# 整除, 口算, 乘法口诀逆运算
if level == 1:
a = random.randint(2, 9)
b = random.randint(2, 9)
r = a*b
if random.randint(0, 99) % 3 == 0:
r = r + random.randint(1, a-1)
result = str(r) + ' ÷ ' + str(a) + ' = '
# 乘法口诀加余数
elif level == 2:
a = random.randint(2, 9)
b = random.randint(10, 99)
result = str(b) + ' ÷ ' + str(a) + ' = '
if result in list:
continue
list.append(result)
return list
def GetMinusCal(num):
"""获取减法算式"""
list = []
if rangeType == 3:
rangeMin = 100
rangeMax = 999
elif rangeType == 2:
rangeMin = 10
rangeMax = 99
else:
rangeMin = 10
rangeMax = 99
while len(list) < num:
a = random.randint(rangeMin, rangeMax)
b = random.randint(a, rangeMax)
if (b - a) <= 10:
continue
# 退位控制
p1 = a % 10
p2 = b % 10
if (level == 2) and (p2 >= p1):
continue
result = str(b) + ' - ' + str(a) + ' = '
if result in list:
continue
list.append(result)
return list
def GetMixedCal(num):
"""获取混合算式算式"""
plus = num // 2
minus = num - plus
listP = GetPlusCal(plus)
listM = GetMinusCal(minus)
resultList = listP + listM
resultList.sort()
return resultList
def GetMixedCalMD(num):
"""获取混合算式算式"""
multi = num // 2
div = num - multi
listM = GetMultiCal(multi)
listD = GetDivCal(div)
resultList = listM + listD
resultList.sort()
return resultList
def printResult(list):
""" 结果打印(TODO: 直接生成打印格式) """
i = 0
print("Count: ", len(list))
for i in range(0, len(list)):
print(str(i + 1) + '.', list[i])
def saveDocx(args, list):
""" 结果保存到docx """
document = Document()
document.styles['Normal'].font.name = u'Dejavu Sans Mono for Powerline'
# 字号对照 link: https://www.jianshu.com/p/8f15e3f2f9e6
document.styles['Normal'].font.size = Pt(14)
document.styles['Normal']._element.rPr.rFonts.set(
qn('w:eastAsia'), u'Dejavu Sans Mono for Powerline')
title = ''
match args.type:
case 1:
title = '加法'
case 2:
title = '减法'
case 3:
title = '加减混合'
case 4:
title = '乘法'
case 5:
title = '除法'
case 6:
title = '乘除法'
header = document.sections[0].header.paragraphs[0]
header.alignment = WD_ALIGN_PARAGRAPH.LEFT
header.style.font.name = u'Dejavu Sans Mono for Powerline'
header.style.font.size = Pt(12)
header.style.font.color.rgb = RGBColor(165, 165, 165)
header.text = '____年__月__日' + ' 得分: ___' + ' 用时: _____' + ' 评语: _________'
footer = document.sections[0].footer.paragraphs[0]
footer.style.font.name = u'Dejavu Sans Mono for Powerline'
footer.style.font.size = Pt(12)
# 灰色!打印机打印时选择灰度打印,否则偏蓝
footer.style.font.color.rgb = RGBColor(165, 165, 165)
footer.text = title + '算式(共' + str(len(list)) + '题) '\
+ datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 分栏
document.sections[0]._sectPr.xpath('./w:cols')[0].set(qn('w:num'), '2')
for i in range(0, len(list)):
paragraph = document.add_paragraph(list[i], style='List Number')
paragraph_format = paragraph.paragraph_format
paragraph_format.space_before = None # 段前距
paragraph_format.space_after = None # 段后距
paragraph_format.line_spacing_rule = WD_ALIGN_PARAGRAPH.JUSTIFY # 行距规则
paragraph.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
x = datetime.datetime.now()
filename = title + x.strftime("%Y%m%d%H%M%S") + '.docx'
print('Save to: ' + filename)
document.save(filename)
def main():
''' main function '''
args = getArgs()
global level, rangeType
level = args.level
match args.type:
case 1:
list = GetPlusCal(args.count)
case 2:
list = GetMinusCal(args.count)
case 3:
list = GetMixedCal(args.count)
case 4:
list = GetMultiCal(args.count)
case 5:
list = GetDivCal(args.count)
case 6:
list = GetMixedCalMD(args.count)
random.shuffle(list)
printResult(list)
saveDocx(args, list)
x = datetime.datetime.now()
print(x.strftime("%Y-%m-%d %H:%M:%S"))
if __name__ == '__main__':
main()