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

xiaoxiao2021-03-06  60

The transformation of the prefix expression to the suffix expression to convert the expression from the infix expression in the form of a suffix representation to the equivalent expression representation

C # cote:

// 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 [ T OP] = 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 time {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 <= '9') || c == '.') {OPERAND = C;

IF ((c == '|| i == s.length - 1) && operand! = "" "// update {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 sequence of sequential impact X = OPERANDS [TOP ---]; // POP double-purpose operator's first operand Switch (C) {case ' ': v = x y; Break; Case '-': V = x - y; breaf; case '*': v = x * y; break; case '/': v = x / y; // First operand / second operand pay attention to operational number sequence on division Break; default: v = 0; Break;} OPERANDS [ TOP] = V; // push intermediate results 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 time {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 time {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 time {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 <= '9') || c == '.') {OPERAND = C;

IF ((c == '|| i == s.Length () - 1) && operand! = "") // update {operands [ TOP] = java.lang.double.Parsedouble (OPERAND); // push operands Operand = "";

IF (c == ' ' || c == '-' || c == '*' || c == '/') {IF ((Operand! = ")) {OPERANDS [ TOP ] = java.lang.double.parsedouble (Operand); // push operands Operand = ";} y = OPERANDS [TOP -]; // POP double-purpose operator's second operand (then first out) Operating sequence affects the impact of the division x = OPERANDS [TOP ---]; // POP double-purpose operator's first operand Switch (c) {case ' ': v = x y; Break; Case '-' : V = x - y; Break; case '*': v = x * y; breaf; case '/': v = x / y; // First operand / second operand Note Operation number sequence Influence Break; default: v = 0; Break;} OPERANDS [ TOP] = V; // Push intermediate result is again incoming stack}} v = Operands [TOP--]; // POP final result system.out. Println (V);}}

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

New Post(0)