The third week of operations 06

This job requires See: https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631

Code address https://cjw_123.coding.net/p/cptj/git

Twinning partner: Bit barracks

1. Project analysis and programming Harvest:

1.1 features a

Requirements: four operations: support the four arithmetic title number of the title 4

1.1.1 a function key and difficult

(1) Random numbers

(2) the operator list stored random outputs

(3) the string into eval () operator

1.1.2 Code:

Code:

import random
def Get_Problem():
    opratiorList = ['+','-','*','/']
    numList = [2,4,5,8]
    opration1 = random.choice(opratiorList)
    opration2 = random.choice(opratiorList)
    opration3 = random.choice(opratiorList)
    a = random.randint(1,9)
    if opration1 == '/':
        b = random.choice(numList)
    else:
        b = random.randint(1,9)
    if opration2 == '/':
        c = random.choice(numList)
    else:
        c = random.randint(1,9)
    if opration3 == '/':
        d = random.choice(numList)
    else:
        d = random.randint(1,9)
    
    ToString = str(a)+opration1+str(b)+opration2+str(c)+opration3+str(d)
    print(ToString+'=')
    result = eval('%d%s%d%s%d%s%d'%(a,opration1,b,opration2,c,opration3,d))

    return result

 

1.2 Function two

Requirements: Support brackets

1.2.1 a function key and difficult

(1) randomly processing the brackets

(2) Logical operation processing on the order of parenthesized

1.2.2 Code:

 DEF the Calculate (exp): 
        LOC = 0 
        COUNT = 0
         # reading a first digital / whole (to be enclosed in brackets) 
        firstNum = exp [LOC]
         IF firstNum =! " ( " : 
            Result = a float (firstNum) 
            LOC + . 1 =
         the else : 
            locStart = LOC. 1 + 
            COUNT + =. 1   # in order to (with) matching 
            the while COUNT =! 0: 
                LOC + =. 1
                 IF exp [LOC] ==" ( " : 
                    COUNT + =. 1
                 elif exp [LOC] == " ) " : 
                    COUNT - =. 1 
            locEnd = LOC 
            LOC +. 1 = 
            Result = the Calculate (exp [locStart: locEnd])
         the while LOC < len (exp): 
            operator = exp [LOC] 
            LOC +. 1 =
             # Perfection a whole, that is, if there is the calculation time coming back with a whole or * / while 
            the while operator == " * "  or== operator " / " : 
                SECNUM = exp [LOC]
                 IF SECNUM =! " ( " : 
                    LOC + =. 1
                 the else : 
                    locStart = LOC. 1 + 
                    COUNT + =. 1   # in order to (with) matching 
                    the while COUNT =! 0: 
                        LOC + =. 1
                         IF exp [LOC] == " ( " : 
                            COUNT +. 1 =
                        elif exp[loc] == ")":
                            count -= 1
                    locEnd = loc
                    loc += 1
                    secNum = calculate(exp[locStart:locEnd])
                if result == "worngproblem":
                        return "worngproblem"
                elif secNum == "worngproblem":
                        return "worngproblem"
                operator ==Elif"*":
                        result = result * float(secNum)
                elif float(secNum) == 0:
                        return "worngproblem"
                else:
                        result = result / float(secNum)
                if loc >= len(exp):
                    break
                operator = exp[loc]
                loc += 1
            # 加号或者减号
            operator1 = operator
            if loc >=len (exp):
                 BREAK 
            SECNUM = exp [LOC]
             IF SECNUM =! " ( " : 
                SECNUM = a float (SECNUM) 
                LOC + =. 1
             the else : 
                locStart = LOC +. 1 
                COUNT + =. 1   # in order to (matched with) 
                the while = COUNT! 0: 
                    LOC + =. 1
                     IF exp [LOC] == " ( " : 
                        COUNT=. 1 +
                     elif exp [LOC] == " ) " : 
                        COUNT - =. 1 
                locEnd = LOC 
                LOC + =. 1 
                SECNUM = the Calculate (exp [locStart: locEnd])
             # perfect second addend / subtrahend 
            IF LOC < len (exp): 
                operator = exp [LOC] 
                LOC + =. 1 
                In Flag = False
                 the while operator == " * "  or operator ==" / " : 
                    In Flag = True 
                    thirdNum = exp [LOC]
                     IF thirdNum =! " ( " : 
                        LOC + =. 1
                     the else : 
                        locStart = LOC +. 1 
                        COUNT + =. 1   # in order to (with) matching 
                        the while COUNT =! 0: 
                            LOC + =. 1
                             IF exp [LOC] == " ( " :
                                count += 1
                            elif exp[loc] == ")":
                                count -= 1
                        locEnd = loc
                        loc += 1
                        thirdNum = calculate(exp[locStart:locEnd])
                    if operator == "*":
                        secNum = secNum * float(thirdNum)
                    elif float(thirdNum) == 0:
                        return "worngproblem"
                    else:
                        secNum = secNum / float(thirdNum)
                    if loc >= len(exp):
                        break
                    operator = exp[loc]
                    loc += 1
                if not flag:
                    loc -= 1
            if result == "worngproblem":
                return "worngproblem"
            elif secNum == "worngproblem":
                return "worngproblem"
            elif operator1 == "+":
                result += float(secNum)
            else:
                #print(secNum)
                result -= float(secNum)
            if loc >= len(exp):
                break
        return result

1.3 Three Functions

Requirements: defining a number of topics, printouts, to avoid duplication

1.3.1 a function key and difficult

(1) The existing expression into the list to compare Ruoyi printed regenerate

(2) to a digital input only positive integer can be determined in

1.3.2 Code:

DEF main ():
     IF (the sys.argv [. 1] == ' -C ' ): 
        
        List1 = [] 
        strnumofproblem = the sys.argv [2 ]
         IF strnumofproblem.isdecimal () == False:
             Print ( " the number of items must be positive The integer " )
         the else : 
            intnumofproblem = int (strnumofproblem)
             for _ in (intnumofproblem) Range:        # avoid repetition equation generating 
                strnum = Get_Problem ()
                 IF strnum in list1:
                    intnumofproblem += 1
                elif Get_Result(strnum) == "worngproblem":
                    intnumofproblem += 1
                else:
                    list1.append(strnum)
                    print("%-30s %g"% (strnum+'=',Get_Result(strnum)))

Experience in the programming of junction 2

  Experience in the programming pair to the importance of cooperation, and the necessity of code specifications. In order not to produce the necessary specifications ambiguity can be a good exchange and communication, which allows us to appreciate the actual environment of the real working environment. Great help for our future work and study.

3 things to spend a longer time

1, the job of interpretation and design

2, modification of the bug

3, analysis of the details of

4 working Photo

5 code address https://cjw_123.coding.net/p/cptj/git

Guess you like

Origin www.cnblogs.com/chijw123/p/11585282.html