######################################## Just use for practice #################################### ####! /Usr/bin/perl # use warning # Use diagnostics; # Use diagnositc # Use warnings; # use warning # use strict; # use strict # This means you must always use # ------------- my --------------- # Before you new any variants # use strict; ######################################## ####################### four arithmetic operations implemented using Perl, brackets can be processed. However, too many exception lines are not considered. # For example, the number of parentheses does not match, or non-Parentheses are entered. # The basic idea is to use parentheses to perform recursion. Find the first smallest parentheses and recursion. # After calculating the result, replace the entire brackets. For example, replace (1 + 3) with the number 4. # Since each recursion ends to the end, there will be only left and right brackets, and the middle is a formula that does not contain # parentheses. In this case, perform common operations. # Match from the beginning. If a number is found, it is pushed into the digital stack. If the symbol is found, press the # symbol stack. When finding the */number, immediately extract the previous number, calculate it with the current number, and then press # into the digital stack. After the first image is taken, only digits and minus signs are left in the stack. # Extract a symbol in sequence and calculate two numbers. It is known that the stack is empty. # The final result is the final result. ######################################## ######################## $ In_expression = "(1 + 1) * (3*(2 + 2) "; # Write your expression here. You can also manually enter print "your expression is: \ N $ in_expression \ n"; $ ans = & Cal ($ in_expression); # Call the function, the parameter is the second number sub CAL () {####################################### ######################################## ######################## my $ ret = 0; my $ para =$ _ [0]; # obtain the passed parameter value while ($ para = ~ /\(.*? \)/) # As long as there are Parentheses () in the parameter, it is processed. Either the parentheses are removed or recursive {if ($ para = ~ /^ \ ([^ \ (] *? \) $/) # When the brackets ("beginning with, ending with parentheses") "and the center is all non-Parentheses, headers and tails {$ para = ~ S/^ \ (//; # remove the brackets ($ para = ~ S/\) $ //; # Remove the parentheses)} If ($ para = ~ /\ ([^ \ (] *? \)/) # Recursion of the first string with parentheses {$ ret = & Cal ($ &); $ para = ~ S/\ ([^ \ (] +? \)/$ RET/; # calculates the value in the first bracket and replaces the overall content (including the brackets) of the first bracket) # use non-greedy to limit to the first bracket $ CNT ++; print "$ CNT. $ Para \ n ";}# the preceding operations are parentheses, remove parentheses or recursion ################################### ######################################## ############################ if the following conditions are not in parentheses, the four arithmetic operations my @ my_num = (); # used to store numbers my @ my_ope = (); # used to store the operator while ($ para ne '') # While not complete {if ($ para = ~ S/^ (\ D +) //) # Find the numbers {push @ my_num, $1; # store the numbers} if ($ para = ~ S/(\ D + ?) //) # Find the operators {# operator # the multiplication and division operation with a higher priority below. Immediately retrieve the previous number and calculate it with the current number if ($1 EQ '*') {if ($ para = ~ S/^ (\ D +) //) # if there is a number after '*' {$ tmp_l = pop @ my_num; $ tmp_r = $1; $ TMP = $ tmp_l * $ tmp_r; push @ my_num, $ TMP; # push the caled number to @ my_num} elsif ($1 EQ '/') {if ($ para = ~ S/^ (\ D +) //) # if there is a number after '/' {$ tmp_l = pop @ my_num; $ tmp_r = $1; $ TMP = $ tmp_l/$ tmp_r; push @ my_num, $ TMP; # push the caled number to @ my_num} # multiplication and division operations with higher priority, immediately extract the previous number and calculate it with the current number # ------------------------------------------------------------------ else # add and subtract symbols, first store the {push @ my_ope, $1; # store the operators }}}################################# ######################################## ################## ############# The multiplication and division symbols have been processed. At this time, only the addition and subtraction symbols are left. It is also possible to process from the back to the front. (Because of the addition combination Law) while (@ my_ope! = 0) {If (POP @ my_ope EQ '+') # I don't think you need to comment out {$ tmp_r = pop @ my_num; $ tmp_l = pop @ my_num; $ TMP = $ tmp_l + $ tmp_r; push @ my_num, $ TMP;} elsif (POP @ my_ope EQ '-') {$ tmp_r = pop @ my_num; $ tmp_l = pop @ my_num; $ TMP = $ tmp_l-$ tmp_r; push @ my_num, $ TMP ;}} return pop @ my_num;} print "\ nthe result is: $ ans \ n "; ######################################## ######################################## ######################################## ######################################## ###################### print "\ n "; print "press any key to exit... "; <>;# stop to show the result