python四则运算 - Go语言中文社区

python四则运算


github项目地址 : https://github.com/kongkalong/python

PSP表格 :

PSP2.1

预估耗时(小时)

实际耗时(小时)

Planning

 

 

.Estimate

72

48

Development

 

 

.Analysis

0.5

0.1

.Design Spec

0

0

.Coding Standard

0

0

.Design Review

0

0

.Design

24

8

.Coding

48

36

.Code Review

0

4

.Test

0

6

Reporting

 

 

.Test Report

0

0

.Size Measurement

0

0

.Postmortem&Process Inprovement Plan

0

0

合计

 

 

设计过程 :

首先,定义生成随机整数的函数printInteger()和生成随机真分数的函数printProperFraction(),然后分别定义输出加法运算的函数addition()、输出减法运算的函数subtraction()、输出乘法运算的函数multiplication()和输出除法运算的函数division(),最后定义一个数值在0-3的随机数,0-3按顺序分别代表addition() subtraction() multiplication() division(),在while循环内用if语句随机输出四则运算。

代码 :

import random

 

#生成0到100的随机整数

def printInteger():

    #返回包括整数的字符串和值的数组

    a=[]

    m=random.randint(0,100)

    n=str(m)

    a.append(m)

    a.append(n)

    return a

 

#输出真分数,真分数表示为m/n

def printProperFraction():

    while True:

        m=random.randint(3,100)

        n=random.randint(2,100)

        if m>n and m%n!=0:

            break

    #返回包括真分数的字符串和值的数组

    a=[]

    a.append(m/n)

    a.append(str(m)+"/"+str(n))

    return a

 

#输出加法运算

def addition():

    m=random.randint(0,1)

    n=random.randint(0,1)

    #运算符左右均为真分数

    if m==0 and n==0:

        a=printProperFraction()

        b=printProperFraction()

        print(a[1]+"+"+b[1])

    #运算符左边为真分数,右边为整数

    if m==0 and n==1:

        a=printProperFraction()

        b=printInteger()

        print(a[1]+"+"+b[1])

    #运算符左边为整数,右边为真分数

    if m==1 and n==0:

        a=printInteger()

        b=printProperFraction()

        print(a[1]+"+"+b[1])

    #运算符左右均为整数

    if m==1 and n==1:

        a=printInteger()

        b=printInteger()

        print(a[1]+"+"+b[1])

 

#输出减法运算

def subtraction():

    m=random.randint(0,1)

    n=random.randint(0,1)

    if m==0 and n==0:

        a=printProperFraction()

        b=printProperFraction()

        #如果运算符左边的数小于右边的数则交换位置

        if a[0]<b[0]:

            a,b=b,a

        print(a[1]+"-"+b[1])

    if m==0 and n==1:

        a=printProperFraction()

        b=printInteger()

        if a[0]<b[0]:

            a,b=b,a

        print(a[1]+"-"+b[1])

    if m==1 and n==0:

        a=printInteger()

        b=printProperFraction()

        if a[0]<b[0]:

            a,b=b,a

        print(a[1]+"-"+b[1])

    if m==1 and n==1:

        a=printInteger()

        b=printInteger()

        if a[0]<b[0]:

            a,b=b,a

        print(a[1]+"-"+b[1])

       

#输出乘法运算

def multiplication():

    m=random.randint(0,1)

    n=random.randint(0,1)

    if m==0 and n==0:

        a=printProperFraction()

        b=printProperFraction()

        print(a[1]+"×"+b[1])

    if m==0 and n==1:

        a=printProperFraction()

        b=printInteger()

        print(a[1]+"×"+b[1])

    if m==1 and n==0:

        a=printInteger()

        b=printProperFraction()

        print(a[1]+"×"+b[1])

    if m==1 and n==1:

        #避免出现0×0

        while True:

            a=printInteger()

            b=printInteger()

            if a[0]!=0 or b[0]!=0:

                break

        print(a[1]+"×"+b[1])

 

#输出除法运算

def division():

    m=random.randint(0,1)

    n=random.randint(0,1)

    if m==0 and n==0:

        a=printProperFraction()

        b=printProperFraction()

        print(a[1]+"÷"+b[1])

    if m==0 and n==1:

        a=printProperFraction()

        #避免除数为0

        while True:

            b=printInteger()

            if b[0]!=0:

                break

        print(a[1]+"÷"+b[1])

    if m==1 and n==0:

        a=printInteger()

        b=printProperFraction()

        print(a[1]+"÷"+b[1])

    if m==1 and n==1:

        a=printInteger()

        while True:

            b=printInteger()

            if b[0]!=0:

                break

        print(a[1]+"÷"+b[1])

       

count=0

while count<300:

    type=random.randint(0,3)

    if type==0:

        addition()

    if type==1:

        subtraction()

    if type==2:

        multiplication()

    if type==3:

        division()

    count+=1

 

测试运行 :

 

 

 

 

PSP表格 :

PSP2.1

预估耗时(小时)

实际耗时(小时)

Planning

 

 

.Estimate

 

48

Development

 

 

.Analysis

 

0.1

.Design Spec

 

0

.Coding Standard

 

0

.Design Review

 

0

.Design

 

8

.Coding

 

36

.Code Review

 

4

.Test

 

6

Reporting

 

 

.Test Report

 

0

.Size Measurement

 

0

.Postmortem&Process Inprovement Plan

 

0

合计

 

 

转载于:https://www.cnblogs.com/kongweijian/p/10574285.html

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_30735745/article/details/94808674
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 23:15:36
  • 阅读 ( 837 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢