Four arithmetic operations 2, 3.2 arithmetic operations of the plural number
# Writher gu peng # edit date 20160312 from fractions import Fraction # score from random import randint # random number def layer (layer_accual, operat_number2, brackets2, layer_amount2 ): # recursive program if (layer_accual> 0): # When the first layer is computed, more than three numbers are generated. The number of layers is set to 3. # Select the number # print "layer_accual", layer_accual opreation_radom = randint (0, layer_accual2-1) # Add 1 to the first layer, extract the number, replace find_operat_number = operat_number [opreation_radom] # replace one of the two numbers with a simple four-character binary operation # print "operater_num ", operater_num # Start from the second layer of the selected number, replace the selected number with a simple binary formula, and insert an array. # when inserting an array, determine whether to add parentheses based on the number, determines whether brackets are added based on the * \ symbol # around the number. if (operator [opreation_radom] = "/") or (operator [opreation_radom] = "*") or (operator [opreation_radom + 1] = "/") or (operator [opreation_radom + 1] = "*")): # determine the symbol brackets [layer_accual] = 1 if (multiplication_and_division = 2): brackets [layer_accual] = 0 operater_num = randint (1, multiplication_and_division) # Add the operator to the array operator_one = "? "If (operater_num = 1): operator_one =" + "if (operater_num = 2): operator_one ="-"if (operater_num = 3 ): operator_one = "*" if (operater_num = 4): operator_one = "/" if (layer_accual = 0): operator [1] = operator_one else: mov_amount = layer_accual + 2-opreation_radom for I in range (0, mov_amount): operator [layer_accual + 2-i] = operator [layer_accual + 2-i-1] # print "I ", I operator [opreation_radom + 1] = operator_one zhen_zheng = Randint () # True score or integer, random if (fraction_exist = 0): zhen_zheng = 1 if (zhen_zheng = 1 ): # generate the first digit first_num = randint (0, number_range) first_num = str (first_num) else: first_num1 = 2 first_num2 = 1 while (first_num1> = first_num2 ): first_num1 = randint (1, number_range) first_num2 = randint (1, number_range) first_num = Fraction (first_num1, first_num2) if (first_num! = 0): first_num = "(" + str (first_num) + ")" first_num = str (first_num) zhen_zheng = randint (1, 2) # True score or integer, random if (fraction_exist = 0): zhen_zheng = 1 if (zhen_zheng = 1): # generate the second number second_num = randint () second_num = str (second_num) else: second_num1 = 2 second_num2 = 1 while (second_num1> = seconds): second_num1 = randint (1, number_range) second_num2 = randint (1, number_range) second_num = Fraction (second_num1, second_num2) If (second_num! = 0): second_num = "(" + str (second_num) + ")" if (layer_accual = 0): # Layer 0th, store the first two digits in the array operat_number [0] = first_num operat_number [1] = second_num if (negative_exit = 0): # (if no negative number exists) if (second_num> first_num and operator_one = 2): while (second_num> = first_num): second_num = randint (1, number_range) if (remainder = 0 ): # (if there is no remainder) if (operator_one = 4): while (second_num % first_num! = 0): print "remainder" second_num = randint (1, number_range) # store two numbers from the first layer if (layer_accual> 0 ): mov_amount = layer_accual + 2-opreation_radom for I in range (0, mov_amount ): operat_number [operator + 1-i] = operat_number [operator + 1-i-1] operat_number [opreation_radom] = first_num operat_number [opreation_radom + 1] = second_num # formula if (operator = 1 ): tempperate1 = str (operat_number [0]) tempperate2 = str (opera T_number [1]) expressions = operat_number [0] + operator [1] + operat_number [1] if (layer_accual> 1): # first locate the replacement number and then generate expression 2, replace expression 1 global expressions kk = str (operat_number [opreation_radom]) expressions2 = first_num + operator_one + second_num # with 2 to find different numbers and replace them? # While (same_amount> 0): expressions = expressions. replace (find_operat_number, expressions2) Placement = layer_accual + 1 if (layer_accual <layer_amount2 + 1): layer (layer_accual, operat_number2, brackets2, role) ############ program start expressions_amount = 5 # formula quantity layer_amount = 2 # layers, that is, the number of numbers number_range = 10 # the size range of the integer value fraction_exist = 1 # whether there is a score multiplication_and_division = 2 # whether there is a multiplication or division, and if yes, It is 4negative_exit = 0 # whether, 1. remainde exists R = 0 # indicates whether the remainder exists. 1 indicates pritenr = 1 # printer mode quit_num = 1 print "expressions_amount", expressions_amountfor counter1 in range (0, expressions_amount): # preparation, execution parameter, number of layers of operation, number of layers of Operation layer_accual = 0 operator = ['K'] * (layer_amount + 3) # record operator record operat_number = ["? "] * (Layer_amount + 2) # recorder brackets for record operations = [0] * (layer_amount + 1) # operator [0] = "? "Operator [2] = "? "Layer (layer_accual, operat_number, brackets, layer_amount) # expressions_mul [counter1] = expressions # Check function while (quit_num = 1): print mode, 1 is displayed on the screen, 2. Export the file "temp = input () while (temp! = 1 and temp! = 2): print "enter" temp = input () pritenr = temp print "again to check whether there are Parentheses. Ten parameters are supported for calculation. 1 is not in parentheses, 2 contains brackets "temp = input () while (temp! = 1 and temp! = 2): print "enter" temp = input () multiplication_and_division = 2 * temp print "value range" number_range = input () print "to add or remove negative numbers, 1 is negative, 0 is no negative "temp = input () while (temp! = 0 and temp! = 1): print "enter" temp = input () negative_exit = temp print "for division with or without remainder, 1 for remainder, 0 for remainder" temp = input () while (temp! = 1 and temp! = 2): print "enter" temp = input () remainder = temp print "Total" expressions_amount = input () # expressions_mul = "regular" * expressions_amount # print expressions_mul print "start" for counter1 in range (0, expressions_amount): # preparation part, execution parameters, Operation layers, layer layer_accual = 0 operator = ['K'] * (layer_amount + 3) # record operator record operat_number = ["? "] * (Layer_amount + 2) # recorder brackets for record operations = [0] * (layer_amount + 1) # operator [0] = "? "Operator [2] = "? "Layer (transport, operat_number, brackets, layer_amount) # expressions_mul [counter1] = expressions # re-query function if (pritenr = 1): print expressions else: f = file('output.txt ', 'A') f. write (expressions + "\ n") # f. write (expressions) print "exit? 0: exit, 1: continue "temp = input () while (temp! = 0 and temp! = 1): print "enter" temp = input () quit_num = temp