The ACM team wants to create a calculator for mdd. However, it does not only need to calculate a + B calculator, but also wants to input an expression to obtain its value, please help him implement this calculator.
For example, if the input is "1 + 2/4 =", the program outputs 1.50 (the result is retained with two decimal places)
-
Input
-
Enter an integer N in the first line. There are N groups of test data (n <10 ).
Each group of test data has only one row, which is a string of no more than 1000 characters. This formula ends with "=. This expression only contains the +-*/and parentheses. Parentheses can be nested. Data ensures that no negative numbers are displayed in the input operations.
Data guarantee that the divisor is not 0
-
Output
-
Each group outputs the operation results of this group, and the output results retain two decimal places.
-
Sample Input
-
21.000+2/4=((1+2)*5+1)/4=
-
Sample output
-
1.50
-
4.00
-
I have done this once before. It may take some time to write the code directly this time. Just comment the code.
-
// Nyoj 35 # include <iostream> # include <cstring> # include <sstream> # include <string> # include <cstring> # include <iomanip> # include <stack> using namespace STD; char mid [1008], post [1008]; // prefix, suffix expression char compare (char C, char d) // comparison priority {Switch (c) {Case '+': Case '-': If (D = '+' | D = '-' | D = ') '| D =') return '>'; else return '<'; break; Case '*': Case '/': if (D = '+' | D = '-' | D = '*' | D = '/' | D = ') '| D =') return '>'; else return '<'; break; Case '(': If (D = ')') return '='; If (D = ') return'> '; else return' <'; break; Case ')': if (D = '(') return '='; return '>'; break; Case '=': If (D = ') return' = '; else return '<'; break ;}// the string is converted to double to (char a []) // The function {stringstream OSS; OSS <; double result; OSS> result; return result;} bool ischar (char C) // determine whether the operator is {If (C = '+' | C = '-' | C = '*' | C = '/' | | C = ') return true; return false;} void change (char mid [], char post []) // prefix expression variable suffix expression {stack <char> S1; // store char a [1000] in the stack; s1.push ('='); // The push operator = int I = 0, M =-1, L = strlen (MID ); while (I <L) {char ch; If (mid [I]> = '0' & Mid [I] <= '9 ') | (mid [I] = '. ') // push the number and decimal point together into the suffix expression {post [++ m] = mid [I]; I ++ ;} else {post [++ m] = ''; // Add a space to convert data if (compare (s1.top (), mid [I]) = '<') {s1.push (mid [I]); I ++;} // the priority of the out-of-stack symbol is greater than that in the stack. It is pushed into else if (compare (s1.top (), mid [I]). = '>') {CH = s1.top (); s1.pop (); Post [++ m] = CH ;}// stack rollback, put the extension expression in else if (compare (s1.top (), mid [I]) = ') {s1.pop (); ++ I ;} // stack rollback }}double operator (double A, char B, double C) // operation {If (B = '+') Return A + C; if (B = '-') return a-c; If (B = '*') return a * C; If (B = '/') return A/C;} double run (char post []) // returns the {int L = strlen (post); int I = 0, flag = 0; stack <double> STR; // There is a stack to store the result char Q [20]; // used to convert the data memset (Q,); Int J =-1; double result, x, Y; while (I <L) {If (post [I]> = '0' & post [I] <= '9' | post [I] = '. ') {q [++ J] = post [I]; flag = 1; // if it is a data with decimals, It is not converted until it is a space or operator ++ I; continue;} else {If (flag = 1) {result = to (Q); flag = 0; j =-1; Str. push (result); // The converted data is pushed to the memset (Q,) in the stack;} If (post [I] = '') I ++; // The space is invalid data, but it is convenient to convert if (ischar (post [I]) // operator to perform operations, and press the result into the stack {x = Str. top (); Str. pop (); y = Str. top (); Str. pop (); Str. push (operator (Y, post [I], x); ++ I ;}} return Str. top (); // returned result} int main () {int N; CIN> N; while (n --) {CIN> mid; change (MID, post ); cout <setiosflags (IOs: fixed) <setprecision (2) <run (post) <Endl; memset (post, 0,1008 );} // system ("pause"); Return 0 ;}