NYOJ305 expression evaluate (recursion or stack)

Source: Internet
Author: User

NYOJ305 expression evaluate (recursion or stack)

 
Dr. after the robot card designed by Kong has mastered the addition and subtraction operations, he recently learned some simple function evaluation values. For example, he knows that the value of the function min (20, 23) is 20, and add) the value is 108, and so on. After training, Dr. Kong's robot Cato can even compute a nested, more complex expression.

Suppose the expression can be simply defined as: 1. A positive decimal number x is an expression. 2. If x and y are expressions, the min (x, y) function is also an expression with the value of x and the minimum number of y. 3. If x and y are expressions, the max (x, y) function is also an expression with the value x and the maximum number in y. 4. If x and y are expressions, the add (x, y) function is also an expression, and its value is the sum of x and y. For example, the value of max (add (1, 2), 7) is 7. Please write a program to help Dr. Kong calculate the correct answer for a given set of expressions, so as to verify the correctness of the Multi-calculation.

Input
Line 1: N indicates the number of expressions to be calculated (1 ≤ N ≤ 10)
Next there are N rows, each row is a string, indicating the expression to be evaluated
(No extra space exists in the expression. Each line cannot exceed 300 characters, and the decimal number in the expression is not
More than 1000 .)
Output
There are N rows in the output, and each row corresponds to the value of an expression.
Sample Input

3add(1,2) max(1,999) add(min(1,1000),add(100,99)) 

Sample output

3999200

Question Analysis:
I used stack applications for similar topics. Here is the recursive Code provided by the code of the great god. it is very simple to traverse strings. If you encounter 'D ', it should be add (). If 'X' is encountered, it is max (). If 'n' is encountered, it is min (),
It should be noted that both key characters and satellite characters are subscripts auto-incrementing. The main technique here is recursion. I personally think this function is very clever, I also know that this function sscanf (str + first, "% d % n", & x, & n); has never been used this way and is taught...

AC code:

/*** @ Xiaoran * recursion */# include
  
   
# Include
   
    
# Include
    # Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              # Define LL long longusing namespace std; char str [1005]; int first; int Opera () {int n, x; switch (str [first]) {case 'D ': {++ first; if (str [first-2] = 'D') return Opera () + Opera (); else return Opera ();} case 'X ': ++ first; return max (Opera (), Opera (); case 'N': ++ first; return min (Opera (), Opera ()); case 'A': case 'M': case 'I': case' (': case')': case', ': ++ first; return Opera (); default: {// write the value to x and the number of digits to n sscanf (str + first, % d % n, & x, & n); first + = n; return x ;}}int main () {int n; scanf (% d, & n); while (n --) {scanf (% s, str ); first = 0; int res = Opera (); printf (% d, res);} return 0 ;}
            
           
          
         
        
       
      
     
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.