Expression evaluation (Recursive implementation)

Source: Internet
Author: User
Tags mathematical functions min
evaluation of an expression Title Description

Dr.kong designed the robot to master the addition and subtraction operations, and recently learned some simple function evaluation, for example, it knows that the value of the function min (20,23) is 108, the value of Add (10,98) and so on. After training, the Dr.kong design robot will even calculate a nested more complex expression.

Suppose an expression can be simply defined as:

1. A positive decimal number x is an expression.

2. If x and Y are expressions, the function min (x, y) is also an expression with a value of the smallest fraction in X, Y.

3. If x and Y are expressions, the function max (x, y) is also an expression with a value of maximum number in X, Y.

4. If x and Y are expressions, the function add (x, y) is also an expression whose value is the sum of x, Y.

For example, the expression max (Add, 7) has a value of 7.

Ask you to write a program that, for a given set of expressions, helps Dr.kong to figure out the correct answer so that it can proofread the correctness of the calculation. input

First line: N Indicates the number of expressions to evaluate (1≤N≤10)

Next there are n rows, each line is a string representing the expression to be evaluated

(There are no extra spaces in the expression, no more than 300 characters per line, and the decimal numbers that appear in the expression are not

More than 1000. ) Output

The output has n rows, and each row corresponds to the value of an expression. Sample Input

3

Add (+)

Max (1,999)

Add (min (1,1000), add (100,99)) sample Output

3

999

200
Test Instructions Summary:

A total of three mathematical functions, add and Min and Max, there will be two or more nested form of functions, the results of the given mathematical functions are calculated;

Problem Solving Ideas:

First to eat a string, called the FF function, FF This function is mainly to determine whether the character passed is a letter, if it is a letter, then it is a function, and then the function inside the comma and after the comma is separated, called to the corresponding function, if the FF is received by the number, The character is converted to a digital output, three mathematical functions, the decibel calculates two values of the function, and finally returns;
Code:

#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #define N-th int ff (
Char str[]);
    int Minn (char Str1[],char str2[]) {int A, B;
    Char Ch1[n],ch2[n];
    int i,j,l,k;
    A=FF (STR1);
    B=FF (STR2);
    if (a>b) return B;
else return A;
    } int addd (char Str1[],char str2[]) {int A, B;
    Char Ch1[n],ch2[n];
    int i,j,l,k;
    A=FF (STR1);
    B=FF (STR2);
return a+b;
    } int Maxx (char Str1[],char str2[]) {int A, B;
    Char Ch1[n],ch2[n];
    int i,j,l,k;
    A=FF (STR1);
   B=FF (STR2);
    if (a>b) return A;
else return B;
    } int ff (char str[]) {char ch1[n],ch2[n];
    int i,j,n,m,k;
    int Len=strlen (str);
        if (Isalpha (Str[0])) {m=0;
        n=0;
            for (i=0,j=0,k=0;i<len;i++) {if (str[i]== ') ') m--;
            if (m==1&&str[i]== ', ') {n=1; } if (m>0) {if (n==0) ch1[j++]=str[i];
                else if (n==1) {n++;
            }else Ch2[k++]=str[i];
            } if (str[i]== ' (') {m++;
        }} ch1[j]= ' + ';
 ch2[k]= ' + ';
Puts (CH1);
        Puts (CH2);
        if (str[0]== ' a ') {return addd (CH1,CH2);
        }else if (str[0]== ' m ' &&str[1]== ' a ') {return Maxx (CH1,CH2);
        }else {return Minn (CH1,CH2);
        }}else {m=0;
        for (i=0;i<len;i++) {m=m*10+str[i]-' 0 ';
    } return m;
    }} int main () {int t,i,j,n,m,k;
    Char str[2000];
    Char Ch1[n],ch2[n];
    scanf ("%d", &t);
        while (t--) {scanf ("%s", str);
    printf ("%d\n", FF (str));
} 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.