# 小学四则运算

from fractions import Fraction
import random

# 设置一系列列表变量
q =[]
ans = []
right = []
wrong = []
right_num = []
wrong_num = []
not_all_repeat = []
repeat1 = []
repeat2 = []
repeat_ans = []

#设置有一个运算符号的函数
def cal1():
hao = random.choice(['+', '-', '*', '/'])
n1 = random.randint(0,10)
n2 = random.randint(0,10)
n3 = random.randint(1,10)
if hao == '+':
a = str(n1) + '+' + str(n2) + '='
b = n1 + n2
return a, b

elif hao == "-":
a = str(n1) + '-' + str(n2) + '='
b = n1 - n2
return a, b

elif hao == "*":
a = str(n1) + '*' + str(n2) + '='
b = n1 * n2
return a, b

elif hao == "/":
a = str(n1) + '/' + str(n3) + '='
b = Fraction(n1, n3)
return a, b

#设置有两个运算符号的函数
def cal2():
hao = random.choice(['+' + '-', '+' + '*', '+' + "/", "-" + "*", '-' + "/", "*" + "/"])
n1 = random.randint(0,10)
n2 = random.randint(0,10)
n4 = random.randint(0,10)
n3 = random.randint(1,10)
if hao == '+' + '-':
a = str(n1) + '+' + str(n2) + "-" + str(n4) + '='
b = n1 + n2 - n4
return a, b

elif hao == "+" + "*":
a = str(n1) + '+' + str(n2) + "*" + str(n4) + '='
b = n1 + n2 * n4
return a, b

elif hao == "+" + "/":
a = str(n1) + '+' + str(n2) + "/" + str(n3) + '='
b = n1 + Fraction(n2 , n3)
return a, b

elif hao == "-" + "*":
a = str(n1) + '-' + str(n2) + "*" + str(n4) + '='
b = n1 - n2 * n4
return a, b

elif hao == "-" + "/":
a = str(n1) + '-' + str(n2) + "/" + str(n3) + '='
b = n1 - Fraction(n2 , n3)
return a, b

elif hao == "*" + "/":
a = str(n1) + '*' + str(n2) + "/" + str(n3) + '='
b = n1 * Fraction(n2 , n3)
return a, b

#输入你想要的题目数量
n = int(input('开始愉快地提升吧！请输入你想要的题目数目： '))
k =n
#开始运行函数
while k > 0:
c = [cal1(), cal2()]
a = c[random.randint(0, 1)]
b1 = list(a[0])
not_all_repeat.append(b1)
q.append(a[0])
ans.append(a[1])
k -= 1

#对于重复的题号，题目的处理
repeat_num = 0
for i1 in range(0, n):
for i2 in range(0, n):
if i1 != i2:
if not_all_repeat[i1] == not_all_repeat[i2]:
repeat_num += 1
repeat1.append(str(i1))
repeat2.append(str(i2))
repeat_ans.append(str(q[i1]))

#在Exercises.txt中写入题目
y = 1
for i in q:
text = open('Exercises.txt', 'a+')
text.write("题目{}: ".format(y))
text.write(i)
text.write('\n')
text.close()
y += 1

e = 1
for i in ans:
e += 1

#在终端中写入自己的答案
e1 = 0
e2 = 0
for o in range(0, len(q)):
print(q[o])
d = o + 1
a = int(input("这是第{}题，请输入你的答案： ".format(d)))
#区分正确与错误的答案
if a == ans[o]:
e1 += 1
right.append(a)
right_num.append(d)
else:
e2 += 1
wrong.append(a)
wrong_num.append(d)

for i in right:
c.write(str(i))
c.write("\n")

for i in wrong:
c.write(str(i))
c.write("\n")

c.write("正确的题目有 :  ")
c.write(str(e1))

for i in right_num:
c.write(str(i))
c.write("  ")

c.write("\n")

c.write("错误的题目有 :  ")
c.write(str(e2))

for i in wrong_num:
c.write(str(i))
c.write("  ")
c.write('\n')

c.write("重复的题目有： {}  道。".format(repeat_num))

c.write('\n')
c.write("重复的Detail: ")
c.write('\n')

for i in range (0, repeat_num):
print('第 {} 组： '.format(i + 1))
print("第 {} 题与第 {} 题重复， 题目为： {}".format(repeat1[i], repeat2[i], repeat_ans[i]))
c.write('\n')

#关闭文件