|
327-evaluating simple C expressions |
3763 |
30.56% |
1145 |
70.66% |
Question link:Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 104 & page = show_problem & problem = 263
Question type: data structure, binary tree
Sample input:
a + bb - za+b--+c++c+f--+--a f-- + c-- + d-++e
Sample output:
Expression: a + b value = 3 a = 1 b = 2Expression: b - z
Question:For an expression, the variable of the expression is composed of 26 lower-case letters. The initial values of these 26 letters are divided into 1, 2, 3 ,...... 26, and a variable in the expression will not appear again. Operators include +,-, ++, and -- (auto-increment and auto-increment have prefixes and suffixes ). Then output the value of this expression, and the calculated value for each variable that appears.
Solution:Because it was a Data Structure topic, we naturally came up with methods for building. After thinking about the method, start typing the code. After you have completed the build code and prepared the computing results, you will find that this question is completely feasible and easier. No matter what the method is, the basic idea of solving the problem is to first remove the prefix and suffix ++ and -- of the expression, and then calculate the result from left to right.
The following code is a non-build version:
# Include <iostream> # include <cstdio> # include <cctype> # include <cstring> # include <deque> # include <vector> # include <algorithm> using namespace STD; vector <char> var; deque <int> que; const int maxn = 120; char STR [maxn]; int Val [26]; // used to save a, B ,...... The initial value of Z int increment; // filter the input string and remove the space void filter () {int Pos = 0; For (INT I = 0; I <strlen (STR); ++ I) {If (STR [I]! = '') {STR [POS ++] = STR [I] ;}} STR [POS] = 0; // string end flag '\ 0'} // whether the prefix inline bool haveprefix (int I) exists) {If (STR [I-1] = '+' & STR [I-2] = '+' | STR [I-1] = '-' & STR [I-2] = '-') return true; return false;} // whether the suffix inline bool havesuffix (int I) exists) {If (STR [I + 1] = '+' & STR [I + 2] = '+' | STR [I + 1] = '-' & STR [I + 2] = '-') return true; return false;} void preprosess () {increment = 0; while (! Que. empty () que. pop_back (); var. clear (); For (INT I = 0; I <strlen (STR); ++ I) {If (STR [I]> = 'A' & STR [I] <= 'Z') {// VaR WITH A prefix. push_back (STR [I]); // Save the letter to if (I> = 2 & haveprefix (I) {If (STR [I-1] = '+ ') + val [STR [I]-'a']; else -- Val [STR [I]-'a']; int n = Val [STR [I]-'a']; que. push_back (n); STR [I-1] = STR [I-2] = '';} // There Is A suffix else if (I <= strlen (STR) -3 & havesuffix (I) {int n = Val [STR [I]-'a']; que. push_back (n); I F (STR [I + 1] = '+') {++ Val [STR [I]-'a']; -- increment ;} else {-- Val [STR [I]-'a']; ++ increment;} STR [I + 1] = STR [I + 2] = '';} else {int n = Val [STR [I]-'a']; que. push_back (n) ;}}} int getsum () {for (INT I = 0; I <strlen (STR); ++ I) {If (STR [I] = '+' | STR [I] = '-') {int A = que. front (); que. pop_front (); int B = que. front (); que. pop_front (); If (STR [I] = '+') que. push_front (a + B); else que. push_front (a-B) ;}} retu Rn que. Front ();} void solve () {// to A, B, C ...... Initial Z value for (INT I = 0; I <26; ++ I) {Val [I] = I + 1;} filter (); preprosess (); int sum = getsum (); // puts (STR); printf ("value = % d \ n", sum); sort (var. begin (), var. begin () + var. size (); For (INT I = 0; I <var. size (); ++ I) {printf ("% C = % d \ n", VAR [I], Val [Var [I]-'a']);} // printf ("\ n");} int main () {freopen ("input.txt", "r", stdin); freopen ("output.txt", "W ", stdout); While (gets (STR) {printf ("expression: % s \ n", STR); solve ();} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double