Expression evaluation time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
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
-
-
Sample output
-
3999200
- Code
-
1#include <iostream>2#include <string.h>3#include <stdio.h>4#include <stdlib.h>5#include <stack>6 using namespacestd;7 8stack<int>s;9 Ten intMinintPintq) { One //if (p > Q) return q; A //else return p; - returnP >= q?q:p; - } the - intMaxintPintq) { - //if (p > Q) return p; - //else return q; + returnP >= q?p:q; - } + A intAddintPintq) { at returnp+Q; - } - - voidReverseChara[]) { - Charb[405]; -memset (b,0,sizeof(b)); in strcpy (b,a); - for(inti =0; I < strlen (a); i++) toA[i] = B[strlen (b)-i-1]; + } - intMain () { the intN; * Chara[305],str[305]; $scanf"%d",&n);Panax Notoginseng GetChar (); - while(n--){ the intK =0, ex =0; + gets (a); Amemset (str,0,sizeof(str)); the if(A[strlen (a)-1] !=')'){ +printf"%s\n", a); - Continue; $ } $ for(inti = strlen (a)-1; I >=3; i--){ - if(A[i] = =')')Continue; - if(A[i]! =','&& A[i]! ='(') str[k++] =A[i]; the if(a[i-1] ==','|| a[i-1] =='('){ - reverse (str);Wuyiex =atof (str); theK =0; -memset (str,0,sizeof(str)); Wu S.push (ex); - } About if(A[i] = ='('){ $ intP,q;//getting data from the stack -p =s.top (); S.pop (); -Q =s.top (); S.pop (); - Switch(a[i-1]){ A Case 'D': S.push (Add (p,q)); i-=3;Continue; + Case 'N': S.push (min (p,q)); i-=3;Continue; the Case 'x': S.push (Max (p,q)); i-=3;Continue; - } $ } the } theprintf"%d\n", S.top ()); the } the return 0; -}
Summary
The string is converted to a double-precision value:
1 // Syntax: 2 #include <stdlib.h>3 doubleconstchar *str); 4 // function: Converts the string str to a double-precision value and returns the result. The parameter str must start with a valid number, but it is allowed to end with any non-numeric character except "E" or "E". For example:5//x = atof ("42.0is_the_answer"); 6 // the value of x is 42.0.
Const
The keyword const is used to tell the compiler that a variable that has been initialized can no longer be modified.
const int a=5; const a=5 with int; Equivalent
Class name const object name equals Const class name Object name
"NYOJ-35" expression evaluation--simple stack exercise