Der in Java implementierte Taschenrechner, das Prinzip wird als Code -Kommentare gelesen. Der spezifische Inhalt lautet wie folgt
public class mycalculator {public static void main (String [] args) {String obj = "6+ (8/2)+6/3+1*8+30"; ArrayList <string> ArrayList = Strformat (OBJ); System.out.println (obj + "=" + calculate (arrayList)); } / *** Verwenden Sie nachfolgende Ausdrücke, um das Ergebnis zu berechnen System.out.println (Ergebnis); Stack <GeNeger> Stack = New Stack <> (); für (int i = 0; i <result.size (); i ++) {String symbol = result.get (i); if (isDigital (symbol)) {// Die Zahl ist direkt stack.push (Integer.ParseInt (Symbol)); } else {// der Operator int num1, num2; num1 = stack.pop (); // zwei Zahlen herausnehmen num2 = stack.pop (); Switch (Symbol) {case " +": stack.push (num2 + num1); brechen; case " -": stack.push (num2 - num1); brechen; Fall " *": stack.push (num2 * num1); brechen; Fall " /": stack.push (num2 / num1); brechen; Standard: Break; }}} return stack.pop (); } / *** In-Ordnung-Traversal wird in nachfolgende Traversal geändert. ArrayList <string> result = new ArrayList <> (); für (int index = 0; index <arrayList.size (); index ++) {String symbol = arrayList.get (index); if (isDigital (symbol)) {// Wenn es sich um eine Zahl handelt, wird es direkt ausgegeben. } else if (symbol.equals (")")) {string tmp; while (! (tmp = stack.pop ()). Equals ("(") {// Stopp nach dem Spiel ist erfolgreiches Ergebnis. Priorität ist weniger als die Priorität im Stapel, und der Stapel wird immer veröffentlicht. result.add (stack.pop ()); ArrayList = New ArrayList <> (); . {ArrayList.Add (comchar.toString (). Trim ()); ! Symbol.equals ("/") &&! Outpriority (String Ch) {Switch (CH) {case "+": case "-": return 3;Alle oben genannten sind der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.