first off, i'll forthright in stating homework question.
i have build expression parser using following code basis: edit:
public class interpreter { public static double parser(st<string, double> variables, string[] inputarray) { double valueholder; stack<string> ops = new stack<string>(); stack<double> vals = new stack<double>(); for(int = 0; < inputarray.length; i++) { string input = inputarray[i]; if (input.equals("(")) ; else if (input.equals("+")) ops.push(input); else if (input.equals("-")) ops.push(input); else if (input.equals("*")) ops.push(input); else if (input.equals("/")) ops.push(input); else if (input.equals("sqrt")) ops.push(input); else if (input.equals(")")) { string op = ops.pop(); double v = vals.pop(); if (op.equals("+")) v = vals.pop() + v; else if (op.equals("-")) v = vals.pop() - v; else if (op.equals("*")) v = vals.pop() * v; else if (op.equals("/")) v = vals.pop() / v; else if (op.equals("sqrt")) v = math.sqrt(v); vals.push(v); } else if (input.matches("\\d")) { valueholder = variables.get(inputarray[i]); vals.push(valueholder); } else vals.push(double.parsedouble(input)); } return vals.pop(); } public static string[] getvalue(st<string, double> variables, string[] inputarray) { double keyholder; double valueholder; for(int = 0; < inputarray.length; i++) { if(variables.contains(inputarray[i])) { keyholder = variables.get(inputarray[i]); inputarray[i] = keyholder.tostring(); } else if (!variables.contains(inputarray[i])) { if (inputarray[i].matches("\\d")) //if letter { if (!inputarray[i].equals("=")) //if not "=" {for (int j = + 1; j < inputarray.length; j++) //next element { if (inputarray[j].matches("\\d")) //if letter {if (!inputarray[j].matches("=")) //if not "=" { //get values , math } } else if (inputarray[j].matches("\\d")) // if digit { if (j + 1 >= inputarray.length) { valueholder = double.parsedouble(inputarray[j]); variables.put(inputarray[i], valueholder); } else parser(variables, inputarray); //if } } } } } } return inputarray; } public static void main(string[] args) { st<string, double> variables = new st<string, double>(); while(!stdin.isempty()) { string input = stdin.readline(); string[] inputarray = input.split("\\s+"); // read line , split whitespace inputarray = getvalue(variables, inputarray); // remove variables , replace associated values double y = parser(inputarray); system.out.println(y); } }
}
console input like:
a = 5 b = 10 c = + b d = c * c print(d)
in case, output console 225. problem cannot figure out how split input between symbol table's keys , values. api inputting keys , values void put(key key, value v)
if key set null, key removed. in advanced.
it not string split, need other checks too, like:
1) split string based on =
pattern
if(stringvalue.matches("=")) { string[] valarray = stringvalue.split("="); } else { // else }
this give array of string. loop through string array , check below conditions.
2) check if there numeric
value present
ie: valarray[].matches("\d");
3) if numeric
value present, check if there more 1 occurrences of alphabet numeric
values present (to see if there more 1 variables present)
this check if alphabet present in of split strings >> valarray[].matches("\d");
4) finally, if there 1 numeric value , 1 alphanumeric value present, store key , value.
5) if there more 1 occurrences of empty variables, need skip operation(plus, minus ...) until have variable value present in key-value array.
you can check checking key-value pair array. don't store key-value if value if empty.
6) if =
not present check print
in string , print operation.
ie:
if(stringvalue.matches("print")); { //do }
note: stringvalue
console input line.
Comments
Post a Comment