-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.py
105 lines (91 loc) · 2.9 KB
/
arithmetic.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
#-*- coding: utf-8 -*-
import random
from fractions import Fraction
import sys
import getopt
typeString = sys.getfilesystemencoding()#获取本地编码格式(因为考虑到要在cmd运行)
def ran_num():#生成随机数
return random.randint(1, 100)
def ran_op():#随机选择运算符
return random.randint(0, 3)
def judgeLevel(op1, op2):#判断运算符优先级
if op1 == 'x' and op2 == '+':
return True
if op1 == 'x' and op2 == '-':
return True
if op1 == '/' and op2 == '+':
return True
if op1 == '/' and op2 == '-':
return True
return False
def backExp(exp1):#计算后缀表达式
stack = []
bck = []
for i in range(len(exp1)):
if type(exp1[i]) == int:
bck.append(exp1[i])
if type(exp1[i]) == str:
if stack:
if judgeLevel(exp1[i], stack[-1]):
stack.append(exp1[i])
else:
bck.append(stack[-1])
stack.pop()
if stack and not judgeLevel(exp1[i], stack[-1]):
bck.append(stack[-1])
stack.pop()
stack.append(exp1[i])
else:
stack.append(exp1[i])
if stack:
stack = stack[::-1]
bck.extend(stack)
return bck
def calculate(op, num1, num2):#计算
if op == '+':
res = Fraction(num1, 1) + Fraction(num2, 1)
if op == '-':
res = Fraction(num1, 1) - Fraction(num2, 1)
if op == 'x':
res = Fraction(num1, 1) * Fraction(num2, 1)
if op == '/':
res = Fraction(num1, 1) / Fraction(num2, 1)
return res
global operators
operators = ('+', '-', 'x', '/')
n = 0
ratio = 0
try:
opts, args = getopt.getopt(sys.argv[1:], 'n:')
except getopt.GetoptError:
print "please input arg"
for opt, value in opts:
if opt == '-n':
n = int(value)
for i in range(n):
nums = (str(ran_num()), str(ran_num()), str(ran_num()), str(ran_num()))
ops = (operators[ran_op()], operators[ran_op()], operators[ran_op()])
exp = nums[0] + ops[0] + nums[1] + ops[1] + nums[2] + ops[2] + nums[3]
exp1 = [int(nums[0]), ops[0], int(nums[1]), ops[1],
int(nums[2]), ops[2], int(nums[3])]
print exp + '=',
ures = raw_input()
bac = backExp(exp1)
print bac
new_stack = []
for i in range(len(bac)):
if type(bac[i]) == int:
new_stack.append(bac[i])
if type(bac[i]) == str:
tmp = calculate(bac[i], new_stack[-2], new_stack[-1])
new_stack.pop()
new_stack.pop()
new_stack.append(tmp)
print new_stack
# print new_stack[0]
if(ures == str(new_stack[0])):
print u'正确'.encode(typeString)
ratio += 1
else:
print u'错误,正确答案='.encode(typeString) + str(new_stack[0])
print u'本次得分:' + str(100.0 / n * ratio)