java - recursively finding value of a String from a map -
I have a hashp in which the key and value & lt; String, string & gt; .
i.e. MapValue:
mapValue.put ("A", "B-7"); MapValue.put ("b", "start + 18"); MapValue.put ("C", "A + 25"); Now I want to evaluate the expression for 'C' . Then for C , the expression will be replaced by ((START + 18) -7) +25 .
So if any method passes through I string C , string it "((start + 18) -7) +25)" Should return and I want to evaluate it according to priority.
Thanks
Generally the logic of such a function (assuming, Know and syntax is strictly as follows:
public string eval (hashmap map value, string variable) {string / tmp = mapValue. Get (variable); // Strong expression for every naw operation {// array split expression in operators in string [] Wars = TMP. Split ("\\" + op); For each element of splitted expr // for array (for int i = 0; i & lt; vars.length; i ++) {// check that the element is a valid key in a hashmap (if the map value canteen (Wars [ii])) {// If it is the substance of the element with it the result of repetition wars [i] = eval (map, wars [i]); // DO ENTERTAINATION} // if the element is not a valid key}} // Join the split string with appropriate operator tmp = join (wars, op); } // Return to bracket return "(" + + + ")"; } 'result will be the result of eval (mapValue,' C '):
(((+ START + 18) -7) +25) Some small insertion function can be implemented as follows: included in public string (string [] arr, string D) {string result = arr [0]; Int i = 1; While (i & lt; arr.length) {Results + = D + Ar [i]; I ++; } Return results; }
All code above is more to describe the argument because better operation should be used with some exception handling, string etc.
Hope it helps
Cheers!
Comments
Post a Comment