bc utility in java, fails for some cases -
i trying write simple bc utility in java. program works fine cases failing expression 4+4*3-3-2-1/3. consecutive - operators. not sure if covered edge cases. suggestions make better helpful. thanks
//enum operations allowed. private enum operators { add (1, "+"), subtract (1, "-"), multiply (2, "*"), divide (2, "/"); string operator; // operator priority int weight; operators(int w, string o) { this.operator = o; this.weight = w; } public string getoperator() { return operator; } public int getweight() { return weight; } } // stack , queue convert expression postfix linkedlist<string> resultqueue = new linkedlist<string>(); stack<operators> operatorstack = new stack<operators>(); //stack evaluate postfix expression stack<integer> evalstack = new stack<integer>(); public int eval(string exp) { char[] expchar = exp.tochararray(); int = 0; stringbuilder sb = new stringbuilder(); while (i < expchar.length){ if (expchar[i] >= '0' && expchar[i] <= '9') { sb.append(expchar[i]); if (i == expchar.length - 1) { resultqueue.add(sb.tostring()); while (!operatorstack.isempty()){ resultqueue.add(operatorstack.pop().getoperator()); } } } else if (expchar[i] == '+' || expchar[i] == '-' || expchar[i] == '/' || expchar[i] == '*'){ if (sb.length() > 0) { resultqueue.add(sb.tostring()); sb = new stringbuilder(); } pushoperator(expchar[i]); } else return 0; i++; } (string r : resultqueue){ if (r.charat(0) > '0' && r.charat(0) < '9'){ evalstack.push(integer.valueof(r)); }else { try { int op2 = evalstack.pop(); int op1 = evalstack.pop(); if (r.charat(0) == '+') evalstack.push(integer.valueof(op1+op2)); else if (r.charat(0) == '-') evalstack.push(integer.valueof(op1-op2)); else if (r.charat(0) == '/') evalstack.push(integer.valueof(op1/op2)); else if (r.charat(0) == '*') evalstack.push(integer.valueof(op1*op2)); } catch (exception e){ system.out.println("parse error"); return 0; } } } int result=0; try{ result = evalstack.pop(); }catch (exception e){ system.out.println("parse error"); } return result; } private void pushoperator(char c) { operators val = getoperator(c); if (operatorstack.isempty() || val.getweight() >= operatorstack.peek().getweight()){ operatorstack.push(val); } else { while (!operatorstack.isempty() && val.getweight() <= operatorstack.peek().getweight()){ resultqueue.add(operatorstack.pop().getoperator()); } operatorstack.push(val); } } private operators getoperator(char c) { (operators o: operators.values()){ if (o.getoperator().equals(string.valueof(c))) return o; } throw new illegalargumentexception("operator not supported"); } public static void main(string[] args) { bc bc = new bc(); system.out.println("please enter expression: "); scanner scn = new scanner(system.in); string exp = scn.nextline(); //remove white spaces exp = exp.replaceall(" ", ""); system.out.println("result: " + bc.eval(exp)); scn.close(); }
the problem consecutive minus operations compute in wrong direction. compute right left whereas need computer left right.
if have on stack operants 12 3 2 0 - - -
compute effectively
12 - (3 - (2 - 0)) = 11
wheras correct be
12 - (3 + 2 + 0) = 7
one possible soulution fix be. before start computation need change operants on stack 12 3 2 0 + + -
.
see snippet executed before computation loop.
// code ... } i++; } (i = 0; < resultqueue.size() - 1; i++) { if ("-".equals(resultqueue.get(i)) && "-".equals(resultqueue.get(i+1))) { resultqueue.set(i, "+"); } } // code (string r : resultqueue) { if (r.charat(0) > '0' && r.charat(0) < '9') { ...
Comments
Post a Comment