-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreatProblem.py
99 lines (93 loc) · 2.88 KB
/
CreatProblem.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
import random
import math
from fractions import Fraction
# import Fraction
class Create(object):
# 创建四则运算符号
def create_operator(self):
operator = ["+", "-", "×", "÷"]
return random.choice(operator)
# 生成四则运算表达式
def create_arith(self, r):
x = 0
list = []
operator_num = random.randint(1, 3)
e1 = Create()
e2 = Create()
if operator_num == 1:
list.append(e1.create_number(r))
list.append(e2.create_operator())
list.append(e1.create_number(r))
elif operator_num == 2:
start = random.randint(0, 2)
end = 0
if start > 0:
end = start + 1
for i in range(1, 4):
if i == start:
list.append("(")
list.append(e1.create_number(r))
if i == end:
list.append(")")
list.append(e2.create_operator())
list.pop()
elif operator_num == 3:
start = random.randint(0, 3)
end = 0
if start > 0:
end = start + 1 + random.randint(0, 1)
if end >= 4:
end = 4
for i in range(1, 5):
if i == start:
list.append("(")
list.append(e1.create_number(r))
if i == end:
list.append(")")
list.append(e2.create_operator())
list.pop()
else:
list.append(e1.create_number(r))
list.append(e2.create_operator())
list.append(e1.create_number(r))
return list
# 将表达式假分数转化为带分数
def proper_fraction(self, list):
num = 0
for fract in list:
if type(fract) == Fraction:
n1 = fract.numerator
n2 = fract.denominator
if n2 == 1:
num += 1
continue
elif n1 > n2:
sub = int(n1/n2)
n1 = n1 % n2
list[num] = '%d%s%d/%d' %(sub, '’', n1,n2)
num += 1
return list
# 将答案假分数转化为带分数
def pop_fracte(self, re):
n1 = re.numerator
n2 = re.denominator
if n2 == 1:
return n1
elif n1 < n2:
return re
else:
sub = int(n1/n2)
n1 = n1 % n2
return '%d%s%d/%d' % (sub, '’', n1, n2)
# 生成随机数
def create_number(self, r):
b = random.randint(1, r)
a = random.randint(1, b * r)
n = Fraction(a, b)
return n
# 将列表中的表达式转化成字符串
def to_string(self, list):
np = ""
for i in range(len(list)):
np = np + str(list[i])
return np