zoukankan      html  css  js  c++  java
  • 20180925-6 四则运算

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2148

    代码地址:https://git.coding.net/doubanjiang73/two.git

    1.(1) 给出每个功能的重点、难点、编程收获。

        1)功能1的重点在于所要生成的四则运算题目是随机的,于是要利用Python random包来生成随机数。在使用random()时,发现该函数不能直接访问只有导入random模块后才能访问。

        2)功能2的重点在于要生成的四则运算题目支持带括号的题目输出。在生成随机数时仍采用Python random包进行处理,其中关于题目中括号的位置的确定十分困难。

        3)功能3的重点在于需要生成带小数四则运算题目的同时也要支持“精美输出”。在开始编写时不会生成txt格式文档,但经过学习后学会了如何“精美输出”。

        4)功能4的重点在于要生成支持分数的四则运算题目,在最开始处理分数约分的问题时十分困难,后来发现利用fraction类处理分数十分简单并利用该方法可以实现字符串、浮点数的输入。

    (2)结对编程体会

           在结对编程的过程中,首先我要感谢孙韦男同学与我一同结对进行编程。在尚未与孙韦男同学结对前,我希望找一名擅长编程的同学与我一组,这样便可以减轻很大压力。但由于想到老师第一天上课时讲到的关于团队的问题时,我改变了我的观念,因为我的编程水平无法帮助到擅长编程的同学。于是在与孙韦男同学进行沟通后我们决定成为一组,一起从头学起、共同进步。结对编程的出现改变了以往我所熟悉的编程模式,让我在编程过程中可以与同学进行讨论。在最初阶段,两个人在一起编程时总会对视后腼腆一笑然后手足无策、不知如何是好。渐渐地我们可以彼此分享自己的编程思路一起学习其他人的代码,共同寻找编程思路并且在编程的过程中也会互相指出彼此的错误,一起完成老师布置的作业。

    (3)在编码、争论、复审等活动中花费时间较长,给你较大收获的事件

         (1)在开始编程时,我们对于使用何种语言进行了讨论。我们二人决定在c语言或者python中进行选择。因为咨询过其他同学,他们说这两种语言较为基础适合初学者学习。综合未来学习需要,我们决定采用python语言作为此次作业的编程语言。

        (2)在开始编程后,我们逐渐发现彼此在编程时都会有一些的小习惯。于是,针对这个问题我们进行了讨论,确定了代码规范。在这个过程中,我们学会了互相谦让并能从对方角度思考问题。

        (3)在初次尝试编程后,我们发现我们对于老师所布置作业的要求理解出现了很大偏差。在初次编程时,我们的理解是自己加入一些四则运算题目并给出答案。但查看往年作业时,我们发现往届同学所给出的四则运算题目是随机生成的。于是,我们只能重新开始编写此次作业,导致花费了许多时间。

        (4)在将任务一的源程序转换成exe格式后,在最初运行时出现了闪退的问题经过查询发现可能是运行速度过快的问题,于是我们们更换了电脑重新进行运行。但在第二台笔记本上每次运行程序的时候都会跳出“高级安全Windows防火墙”这一界面。首先,我们分别通过Windows防火墙页面、控制台、Windows defender三种方式进行尝试。其次,我们通过控制面板系统和安全Windows 防火墙允许的应用页面添加了该程序但也未能解决此问题。最后,我们是通过卸载电脑管家的方式解决了这个问题。

                   

      (5)在编程的过程中,对于python的掌握不够熟练导致在编程的过程中极易出现语法错误的问题。在此次作业的完成过程中,我们在查询函数的使用方法上花费了极大的精力。

    2.照片1张,包括结对的2位同学、工作地点、计算机,可选项包括其他能表达结对编程工作经历的物品或场景。

    结对对象:孙韦男、祝玮琦

    工作地点:宿舍

    运行环境:Pycharm

     

    3.四则运算代码

    功能一:

    import random 
    ops = ['+','-','*','/']
    com = input('>') #用户输入
    cot = 0 #答对的题
    x = 0
    while x < 20 :
        s1 = random.randint(1,10)
        s2 = random.randint(1,10)
        s3 = random.randint(1,10)
        s4 = random.randint(1,10)
        op1 = random.choice(ops) #随机运算符
        op2 = random.choice(ops)
        op3 = random.choice(ops)
        while op1 == op2 == op3:
            op1 = random.choice(ops) #随机运算符
            op2 = random.choice(ops)
            op3 = random.choice(ops)
        eq = (str(s1)+op1+str(s2)+op2+str(s3)+op3+str(s4))
        res = eval(eq) 
        if len(str(res) ) > 5:
            continue
        x += 1
        print(eq)
        in_res =eval( input('?'))
        if in_res == res:
            print('算对啦,你真是个天才!')
            cot += 1
        else:
            print('再想想吧,答案似乎是%s喔!'%res)
    
    print('你一共答对%s道题,共20道题'%cot)

    功能二:

    from random import randint
    from random import choice
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    cot = 0 #答对的题
    x = 0
    while x < 20 :
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = eval(eq) 
        if len(str(res) ) > 5:
            continue
        x += 1
        print(eq)
        in_res =eval( input('?'))
        if in_res == res:
            print('算对啦,你真是个天才!')
            cot += 1
        else:
            print('再想想吧,答案似乎是%s喔!'%res)
    
    print('你一共答对%s道题,共20道题'%cot)

    功能三:

    from random import randint
    from random import choice
    import os 
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    com_list = com.split()
    while com_list[2].isdigit() == False:
        print('题目数量必须是正整数')
        com = input('>') #用户输入
        com_list = com.split()
    
    def xx():
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = eval(eq) 
        return [eq,res]
    
    eq = [];  res = []
    while len(res) < int(com_list[2]):
        a = xx()
        if a[1] in res or len((str(a[1])) ) >6:
            continue
        eq.append(a[0])
        res.append(a[1])
    
    f= open('题目.txt','w')
    for i in range(len(eq)):
        print('{0:15}'.format(eq[i]),end = '')
        print(res[i])
        xxx = 17 - len(eq[i])
        f.write(str(eq[i]+' '*xxx))
        f.write(str(res[i])+'
    ')
    f.close()
    os.system('题目.txt')  #决定是否打开txt

    功能四:

    import os
    from random import randint
    from random import choice
    from fractions import Fraction
    ops = ['+','-','*','/']
    bra = ['(', '', ')']
    com = input('>') #用户输入
    com_list = com.split()
    while com_list[2].isdigit() == False:
        print('题目数量必须是正整数')
        com = input('>') #用户输入
        com_list = com.split()
    
    def xx():
        s1 = randint(1,10)
        s2 = randint(1,10)
        s3 = randint(1,10)
        s4 = randint(1,10)
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)
        """括号"""
        bra_1 = ['' , '' ,'']
        bra_2 = ['' , '' , '']
        i = ii =0
        while (i ==0 and ii ==2) or abs(i-ii)==1 
            or ii < i  :
            i = randint(0,2)
            ii = randint(0,2)
    
        bra_1[i] = '(';   bra_2[ii]=')'
    
        while op1 == op2 == op3 :
            op1 = choice(ops) #随机运算符
            op2 = choice(ops)
            op3 = choice(ops)
    
        eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
        bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
        + str(s4) + bra_2[2]
        res = Fraction(eval(eq)) 
        return [eq,res]
    
    eq = [];  res = []
    while len(res) < int(com_list[2]):
        a = xx()
        if a[1] in res or len((str(a[1])) ) >6:
            continue
        if int(a[1]) == a[1]: #保证输入全为分数
            continue
        eq.append(a[0])
        res.append(a[1])
    
    f= open('题目.txt','w')
    for i in range(len(eq)):
        print('{0:15}'.format(eq[i]),end = '')
        print(res[i])
        xxx = 17 - len(eq[i])
        f.write(str(eq[i]+' '*xxx))
        f.write(str(res[i])+'
    ')
    f.close()
    os.system('题目.txt')  #决定是否打开txt

    4.运行结果截图

    功能一:

     

    功能二:

     功能三:

    功能四:

  • 相关阅读:
    STM32 ~ J-LINK V8 修复
    转移文件
    linux
    STM32 ~ MDK环境下调试程序 HardFault_Handler 相关
    HR_ROS 节点信息
    STM32 ~ 串口DMA通道查找
    CodeBackUP_node_find_serial
    Java问题排查工具箱[转载]
    JDK1.7 ConcurrentHashMap 源码浅析
    JDK1.7 HashMap 源码分析
  • 原文地址:https://www.cnblogs.com/zwqhh/p/9740672.html
Copyright © 2011-2022 走看看