import java.util.Stack; import java.util.StringTokenizer; import java.net.*; public class Calculate { private double plusVal; private double minusVal; private double mulVal; private double divVal; public static void main(String []args){ Calculate calc = new Calculate(); calc.parseTokens(args[0]); } public void parseTokens(String uttrykk){ String delim = "()*/-+"; StringTokenizer str = new StringTokenizer(uttrykk, delim, true); Stack stack = new Stack(); while(str.hasMoreTokens()){ String token = str.nextToken(); stack.push(token); } try{ while (stack.contains("(")){ int index = stack.indexOf("("); stack.removeElementAt(index); //removing the ( operator while (stack.contains("*")){ int index2 = stack.indexOf("*"); stack.removeElementAt(index2); //removing the * operator operatorMul(Double.parseDouble(stack.elementAt(index2-1)), Double.parseDouble(stack.elementAt(index2))); stack.removeElementAt(index2); stack.insertElementAt(Double.toString(mulVal), index2); stack.removeElementAt(index2-1); } while (stack.contains("/")){ int index2 = stack.indexOf("/"); stack.removeElementAt(index2); operatorDiv(Double.parseDouble(stack.elementAt(index2-1)), Double.parseDouble(stack.elementAt(index2))); stack.removeElementAt(index2); stack.insertElementAt(Double.toString(divVal), index2); stack.removeElementAt(index2-1); } while (stack.contains("-")){ int index2 = stack.indexOf("-"); stack.removeElementAt(index2); operatorMinus(Double.parseDouble(stack.elementAt(index2-1)), Double.parseDouble(stack.elementAt(index2))); stack.removeElementAt(index2); stack.insertElementAt(Double.toString(minusVal), index2); stack.removeElementAt(index2-1); } while (stack.contains("+")){ int index2 = stack.indexOf("+"); stack.removeElementAt(index2); operatorPlus(Double.parseDouble(stack.elementAt(index2-1)), Double.parseDouble(stack.elementAt(index2))); stack.removeElementAt(index2); stack.insertElementAt(Double.toString(plusVal), index2); stack.removeElementAt(index2-1); } } while (stack.contains(")")){ int index = stack.indexOf(")"); stack.removeElementAt(index); //removing the ) operator } while (stack.contains("*")){ int index = stack.indexOf("*"); stack.removeElementAt(index); //removing the * operator operatorMul(Double.parseDouble(stack.elementAt(index-1)), Double.parseDouble(stack.elementAt(index))); stack.removeElementAt(index); stack.insertElementAt(Double.toString(mulVal), index); stack.removeElementAt(index-1); } while (stack.contains("/")){ int index = stack.indexOf("/"); stack.removeElementAt(index); operatorDiv(Double.parseDouble(stack.elementAt(index-1)), Double.parseDouble(stack.elementAt(index))); stack.removeElementAt(index); stack.insertElementAt(Double.toString(divVal), index); stack.removeElementAt(index-1); } while (stack.contains("-")){ int index = stack.indexOf("-"); stack.removeElementAt(index); operatorMinus(Double.parseDouble(stack.elementAt(index-1)), Double.parseDouble(stack.elementAt(index))); stack.removeElementAt(index); stack.insertElementAt(Double.toString(minusVal), index); stack.removeElementAt(index-1); } while (stack.contains("+")){ int index = stack.indexOf("+"); stack.removeElementAt(index); operatorPlus(Double.parseDouble(stack.elementAt(index-1)), Double.parseDouble(stack.elementAt(index))); stack.removeElementAt(index); stack.insertElementAt(Double.toString(plusVal), index); stack.removeElementAt(index-1); } if (stack.size() == 1){ System.out.println("svaret er: "+stack.pop()); } } catch(Exception e) { System.out.println("feil i uttrykk:"+e); } } public double operatorPlus(double first, double last){ return plusVal = first + last; } public double operatorMinus(double first, double last){ return minusVal = (first - last); } public double operatorMul(double first, double last){ return mulVal = (first * last); } public double operatorDiv(double first, double last){ return divVal = (first / last); } }