Data Structure Application "Stack" Implementation: Analytical Arithmetic Expression and Calculation (C #Java) (Reserve

xiaoxiao2021-03-06  50

Author Blog: http://blog.9cbs.net/playyueer/ A transformation of the blot-in-antix to the suffix expression To convert the expression from the prefix expression in the form of an equivalent expression C # CODE: // Using system; class class1 {public static void main () {system.console.writeline ("Hello World!"); // Pixel => Suffix Expression String S = "(1.9 (20 41) / (25 * 11) - 3) * 2 "; // is infix; //foxed String s =" "; // suffix char [] Operators = new char [s.Length]; int top = -1; for (INT i = 0; i = 0) / / Stack does not empty {char c = Operators [top -]; // Pop Operator if (c ==') {Operators [ TOP] = C; // push Operator Break;} else {s = s c;}}} Operators [ TOP] = C; // Push Operator S = ""; Break; Case '*': // ignore spaces Case '/ ': While (top> = 0) / / Stack does not empty {char c = Operators [top -]; // Pop Operator if (c ==') {Operators [ TOP] = C; // Push Operator Break;} else {if (c == ' ' || c == '-') {Operators [ TOP] = C; // push operator break;} else {s = s c;}}} Operators [ TOP] = C; // Push Operator S = ""; Break; Case '(': Operators [ Top] = C; S = ""; Break; Case ')': While TOP> =

0) / / Stack does not empty {char c = Operators [top -]; // POP Operator if (c == ') {Break;} else {s = s c;}} s = ""; Break; default: s = s c; break;}} while (TOP> = 0) {s = s Operators [top -]; // pop operator} system.console.writeLine (s); // suffix // suffix expression calculates Double [] Operands = new double [s.Length]; Double X, Y, V; TOP = - 1; String Operand = ""; for (int i = 0; i = '0' && c <= ') || c =='. ') {OPERAND = C;} IF c == '' && operand! = "" || i == s.length - 1) {OPERANDS [ Top] = system.convert.todouble (Operand); // push operands Operand = "} IF (c == ' ' || c == '-' || c == '*' || c == '/') {IF ((Operand! = ")) {OPERANDS [ TOP ] = System.convert.todouble (operand); // push operands operand = "";} y = OPERANDS [TOP ---]; // POP binocular operator's second operand (then first out) pay attention to operand The influence of sequence on division X = OPERANDS [TOP -]; // POP double-purpose operator Operation Switch (C) {CASE ' ': V = X Y; Break; Case '-': V = x - Y; Break; Case '*': v = x * Y; Break; Case '/' : V = x / y; // First operand / second operand attention Operation number sequence on division Break; default: v = 0; Break;} OPERANDS [ TOP] = v; // push middle The result is again in the stack}} v =

Operands [TOP -]; // POP final result system.console.writeline (v); system.console.readline ();}} java code: class class1 {public static void main (string [] args) {system. Out.println ("Hello World!"); // Pixel => Suffix Expression String S = "(1.9 (20 41) / (25 * 11) - 3) * 2"; // Fox String S = ""; // Suffix char [] Operators = new char [s.Length ()]; int top = -1; for (int i = 0; i = 0) / / Stack does not empty {char C = Operators [TOP ---]; // Pop Operator if (C == '(') {Operators [ Top] = C; // Push Operator Break;} else {s = s c;}} Operators [ TOP] = C; // Push Operator S = ""; Break; Case '*': // Operator Case '/': While (TOP> = 0) / / Stack does not empty {char c = Operators [TOP -]; // Pop Operator if (C == '(') {Operators [ Top] = C; // Push Operator Break;} else {if (c == ' ' || c == '-') {Operators [ TOP] = C; // push operator break;} else {s = s c;}}} Operators [ Top] = C; // Push Operator S = ""; Break; Case '(': // Operator Operators [ Top] = C; S = "" "; Break; Case ' ': // Operator While (TOP> =

0) / / Stack does not empty {char c = Operators [top -]; // POP Operator if (c == ') {Break;} else {s = s c;}} s = ""; Break; default: // operand S = S C; Break;}} while (TOP> = 0) {s = s Operators [TOP ---]; // Pop Operator} system.out.println (S); // suffix // suffix expression calculates Double [] Operands = New Double [S.Length ()]; Double X, Y, V; TOP = - 1; String Operand = ""; for (INT I = 0; i = '0' && c <= ') || c =='. ') {OPERAND = C;} IF ((c == '' && operand! = ") || i == s.length () - 1) {OperAnds [ TOP] = java.lang.double.parsedouble (OPERAND); // push operands Operand = "";} if (c == ' ' || c == '-' || c == '*' || c == '/') {ix ( (Operand! = ")) {OPERANDS [ TOP] = java.lang.double.parsedouble (Operand); // push operands Operand =" ";} y = Operands [top--]; // POP double Second operand of the operand (afterward) pay attention to the operation of the operand to the division of division X = OPERANDS [TOP -]; // POP double-purpose operator's first operand switch (c) {case ' ': v = x y; Break; Case '-': v = x - y; break; cas' * ': V = x * y; Break; Case' / ': v = x / y; // First operand / second operand attention Operation number sequence on division affects Break; default: V = 0; Break; } OPERANDS [ TOP] = V;

转载请注明原文地址:https://www.9cbs.com/read-83738.html

New Post(0)