-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsmath.py
96 lines (88 loc) · 2.52 KB
/
jsmath.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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 20 17:37:19 2020
@author: m1353
"""
import random
# 用来计算最终结果
def Calculation_Formula(a,b,c,c1,c2):
num = 0
flag = 0
if c2 == '*':
if c1 != '*':
if c1 != '/':
if c1 == '-':
num = (-b)*c
else:
num = b*c
flag = 1
if c2 == '/':
if c1 != '*':
if c1 != '/':
if c1 == '-':
num = (-b)/c
else:
num = b/c
flag = 1
if c1 == '+':
if flag == 1:
num = num+a
else:
num = a+b
if c1 == '-':
if flag == 1:
num = num+a
else:
num = a-b
if c1 == '*':
num = a*b
if c1 == '/':
num = a/b
if c2 == '+':
num = num+c
if c2 == '-':
num = num-c
if c2 == '*':
if flag == 0:
num = num*c
if c2 == '/':
if flag == 0:
num = num/c
return num
def Random_Formula():
a = random.randint(0, 100)
b = random.randint(0, 100)
c = random.randint(0, 100)
MathematicalSymbols = ['+', '-','/','*']
MathematicalSymbols1 = random.choice(MathematicalSymbols)
MathematicalSymbols2 = random.choice(MathematicalSymbols)
# 生成式子
if MathematicalSymbols1 == '-' and MathematicalSymbols2 != '*' and MathematicalSymbols2 != '/':
if a-b < 0:
Random_Formula()
if MathematicalSymbols1 == '*' or MathematicalSymbols1 == '/':
a = random.randint(1,10)
b = random.randint(1,10)
if MathematicalSymbols2 == '*' or MathematicalSymbols2 == '/':
b = random.randint(1,10)
c = random.randint(1,10)
if a==b or b==c or a==c:
Random_Formula()
formula = str(a) + MathematicalSymbols1 + str(b) + MathematicalSymbols2 + str(c)
re = Calculation_Formula(a,b,c,MathematicalSymbols1,MathematicalSymbols2)
string = formula+'='+str(re)
# 防止出现负数
if re < 0 :
Random_Formula()
else:
# print(formula,'=',re)
# 将生成的算式写到文件中去
with open(r'D:\Py-test\02.txt','a',encoding='utf-8') as f:
f.write(string)
# 为了美观
f.write('\n')
pass
#生成25个小学加减乘除混合运算
for i in range(25):
Random_Formula()
pass