python实现四则运算和效能分析

代码github地址:https://github.com/yiduobaozhi/-1

PSP表格:

 

 

预测时间(分钟)

planning

计划

15

Estimate

估计这个任务需要多少时间

100

Development

开发

15

Analysis

需求分析

10

Design Spec

生成设计文档

10

Design Review

设计复审(和同事审核设计文档)

0

Coding Standerd

代码规范(为目前的开发制定合适的规范)

5

Design

具体设计

3

Coding

具体编码

120

Code Review

代码复审

3

Text

测试(自测,修改代码,提交修改)

10

Reporting

报告

7

Text Report

测试报告

7

Size Measurement

计算工作量

10

Postmortem & Process Improvement Plan

事后总结,并提出过程改进计划

12

解题思路:

(1)寻找一种可以用于真分数的函数

(2)随机生成几个数或者几个分子分母

(3)设计代码,实现加减乘除基本功能

(4)查询效能分析相关资料,尝试优化代码

设计过程:
分开为整数部分计算和分数部分计算,分数调用Fraction函数进行计算,使用random随机生成数字,用于计算四则计算

具体代码:

from fractions import Fraction #导入分数函数
import numpy as np
import cProfile
def size():
    print("你需要算的是分数还是整数?")
    a=input()
    if a=="分数":
        x = np.random.randint(1,100)
        y = np.random.randint(1,100)
        e = input("加减乘除?")
        b = np.random.randint(1,100)
        c = np.random.randint(1,100)
        print(x,y,b,c)
        if e=="+":
            print("答案为:%s+%s=%s"%(Fraction(x,y),Fraction(b,c),Fraction(x, y) + Fraction(b, c)))
        elif e=="-":
            print("答案为:%s-%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) - Fraction(b, c)))
        elif e=="*":
            print("答案为:%s*%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) * Fraction(b, c)))
        elif e=="/":
            print("答案为:%s/%s=%s" % (Fraction(x, y), Fraction(b, c), Fraction(x, y) / Fraction(b, c)))
    elif a=="整数":
        x1=np.random.randint(1,100)
        y1=np.random.randint(1,100)
        print(x1,y1)
        e1=input("加减乘除?")
        if e1 == "+":
            print("答案为:%s+%s=%s" %(x1, y1, x1+y1))
        elif e1== "-":
            print("答案为:%s-%s=%s" %(x1, y1, x1-y1))
        elif e1 == "*":
            print("答案为:%s*%s=%s" %(x1, y1, x1 * y1))
        elif e1=="/":
            print("答案为:%s/%s=%s" %(x1, y1, x1 / y1))
cProfile.run(size)

测试运行:

效能分析:

PSP表格:

 

 

实际时间(分钟)

nning

计划

40

Estimate

估计这个任务需要多少时间

100

Development

开发

9

Analysis

需求分析

5

Design Spec

生成设计文档

1

Design Review

设计复审(和同事审核设计文档)

1

Coding Standerd

代码规范(为目前的开发制定合适的规范)

1

Design

具体设计

21

Coding

具体编码

100

Code Review

代码复审

3

Text

测试(自测,修改代码,提交修改)

5

Reporting

报告

60

Text Report

测试报告

20

Size Measurement

计算工作量

2

Postmortem & Process Improvement Plan

事后总结,并提出过程改进计划

1

 

posted @ 2018-04-18 23:40  一朵包纸  阅读(710)  评论(1编辑  收藏  举报