标签:char ret dom lis for tab pen 博客 ble
https://github.com/chenzquan/YuanSuanQi
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 10 | 8 |
· Estimate | · 估计这个任务需要多少时间 | 10 | 8 |
Development | 开发 | 655 | 800 |
· Analysis | · 需求分析 (包括学习新技术) | 30 | 35 |
· Design Spec | · 生成设计文档 | 30 | 40 |
· Design Review | · 设计复审 (和同事审核设计文档) | 10 | 15 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 5 | 5 |
· Design | · 具体设计 | 40 | 55 |
· Coding | · 具体编码 | 5h*60 | 7h*60 |
· Code Review | · 代码复审 | 1h*60 | 1.5h*66 |
· Test | · 测试(自我测试,修改代码,提交修改) | 3h*60 | 2h*45 |
Reporting | 报告 | 290 | 252 |
· Test Report | · 测试报告+博客 | 4h*60 | 4.5h*75 |
· Size Measurement | · 计算工作量 | 10 | 66 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 40 | 50 |
合计 | 955 | 1456 |
(1) 思考如何随机生成四则运算表达式
(2) 查阅相关博客,了解效能分析所用工具
(3) 学习所用到的类库,如random库
设计实现过程:
这个四则运算表达式由三个函数实现,分别为create()、mid2post()、compute_post(),第一个负责生成表达式,第二个负责生成后缀表达式,最后一个表达式负责计算结果。
代码说明:
class Stack(object):
def __init__(self):
self.items = []
def push(self, new_item):
self.items.append(new_item)
def top(self):
return self.items[-1]
def pop(self):
return self.items.pop()
def isEmpty(self):
return [] == self.items
def size(self):
return len(self.items)
# 将中缀表达式变换成后缀表达式
def mid2post(trans_string, op_rank):
post_list = []
stack = Stack()
for checking_char in trans_string:
if checking_char in ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘:
post_list.append(checking_char)
elif checking_char == ‘(‘:
stack.push(checking_char)
elif checking_char == ‘)‘:
top_char = stack.pop()
while top_char != ‘(‘:
post_list.append(top_char)
top_char = stack.pop()
else: # 符号为运算符
while (not stack.isEmpty()) and (op_rank[stack.top()] >= op_rank[checking_char]):
post_list.append(stack.pop())
stack.push(checking_char)
while not stack.isEmpty():
post_list.append(stack.pop())
return ‘‘.join(post_list)
# 将计算后缀表达式
def compute_post(post_string):
stack = Stack()
for computing_char in post_string:
if computing_char in ‘0123456789‘:
stack.push(computing_char)
else: # 如果为运算符
value_2 = float(stack.pop())
value_1 = float(stack.pop())
if computing_char == ‘+‘:
value_3 = value_1 + value_2
elif computing_char == ‘-‘:
value_3 = value_1 - value_2
elif computing_char == ‘*‘:
value_3 = value_1 * value_2
else:
value_3 = value_1 / value_2
stack.push(value_3)
return stack.pop()
def create(grade):
s = []
s1=random.randint(1,9)
s2=random.randint(1,9)
s3=random.randint(1,9)
s4=random.randint(1,9)
s.append(s1)
s.append(s2)
s.append(s3)
s.append(s4)
if grade<=5:
operator=random.choice([‘+‘,‘-‘,‘*‘,‘/‘])
if(operator==‘-‘):
s2=random.randint(s1,9)
trans_string = str(s2) + operator + str(s1)
else:
flag = False
s5 = random.randint(1,2)
trans_string = ‘‘
if s5==1:
trans_string = trans_string + ‘(‘
flag = True
trans_string = trans_string + str(s[0])
for i in range(1,4):
operator=random.choice([‘+‘,‘-‘,‘*‘,‘/‘])
trans_string = trans_string + operator
trans_string = trans_string + str(s[i])
if flag==True:
s5=random.randint(1,2)
if s5==2:
trans_string = trans_string + ‘)‘
flag=False
return trans_string
def main():
op_rank = {‘*‘: 2, ‘/‘: 2, ‘+‘: 1, ‘-‘: 1, ‘(‘: 0}
number = 0
while True:
trans_string = create(number)
result = float(compute_post(mid2post(trans_string,op_rank)))
if result<0:
continue
print("第%d题%s=" % (number+1, trans_string))
print("result:%f"%result)
N = float(input())
if N==result:
print("回答正确!")
number = number + 1
else:
print("回答错误!")
if number==10:
break
print("正确回答%d道题"%(number))
测试运行:
性能分析:
这个程序通过使用cProfile和time模块来测试程序的性能
标签:char ret dom lis for tab pen 博客 ble
原文地址:https://www.cnblogs.com/zquan/p/8878695.html