Data structure review-arithmetic expression evaluate algorithm (recursive operation with parentheses)

Source: Internet
Author: User

First, we declare that this is just the prototype of an algorithm, and the actual usage is almost 0. I only want to talk about this idea. The code is written in Java and can be run, but the expressions and median values cannot be negative. [because I use a Java string to intercept it. If I use C, without parentheses, this problem does not exist. It can also be a negative number. Next I will write my thoughts and you will know why it cannot be a negative number.

First, judge whether there are any parentheses. If there is a right brace, extract the string between the left brace and the right brace, and add a = to the string. [Because there is no limit in the brackets]

Second, if only the addition, subtraction, multiplication, division, for example, 1 + 2*3 + 4 + 5-5 =, it will enter the loop.

Third: There are two stacks. When there is a symbol, first put the data in the digital stack, and then compare the top of the symbol stack with the symbol to be placed in the stack. If the priority of the coming stack is lower than that of the top stack

The number stack is two, and the symbol stack is one for calculation. The result is put into the number stack, and the symbols to be put into the stack are now put into the stack.

Fourth: if an equal sign is encountered, all data is output to the stack. If the stack is empty, the first value of the digital stack is returned. [Final result]

Package calculator; public class demo {public double number [] = new double [6]; // digital stack public char sign [] = new char [6]; // symbol stack, * // = Priority greater than +-priority public int topnum =-1; // header pointer public int topsign =-1; // header pointer // expression evaluate, parentheses algorithm, note: The expression must be correct and cannot contain negative numbers, because this algorithm uses a string to intercept public Double Evaluation (string) throws exception {// The parameter indicates the correct expression double result = 0; // if the expression contains parentheses, extract the string if (string. contains ("(") | string. contains (")")) {Int temp = 0; For (INT I = 0; I <string. length (); I ++) {If (string. charat (I) = '(') {// touch the left bracket and record temp = I; continue;} If (string. charat (I) = ') {// returns the right brace and recursion, which is equivalent to the string of the output stack. substring (0, temp) + evaluation (string. substring (temp + 1, I) + "=") // Add an equal sign to continue recursion + String. substring (I + 1); system. out. println (string); I =-1; Continue ;}}// stack operation for (INT I = 0; I <string. length (); I ++) {Switch (string. charat (I) {Case '+': Case '-': Case '*': Case '/': Case '=': Result = calculate (string, I, string. charat (I); string = string. substring (I + 1); I =-1; break; default: break;} return result;} private double calculate (string, int sub, char signal) {Double X = double. parsedouble (string. substring (0, sub); number [++ topnum] = x; // number to stack if (topsign =-1) {// If the symbol stack is empty, sign [++ topsign] = signa L; return number [topnum];} If (signal = ') {// calculates the value while (topsign! =-1) {If (sign [topsign] = '+') {number [topnum-1] + = number [topnum];} else if (sign [topsign] = '-') {number [topnum-1]-= number [topnum];} else if (sign [topsign] = '*') {number [topnum-1] * = number [topnum];} else if (sign [topsign] = '/') {number [topnum-1]/= number [topnum];} topsign --; topnum --; // stack rollback} return number [topnum];} If (comparepriority (signal, sign [topsign]) {// stack rollback, add the calculation result to the digital stack. If (sign [topsign] = '+') {number [topnum-1] + = number [topnum];} else if (sign [topsign] = '-') {number [topnum-1]-= number [topnum];} else if (sign [topsign] = '*') {number [topnum-1] * = number [topnum];} else if (sign [topsign] = '/') {number [topnum-1]/= number [topnum];} sign [topsign] = signal; // The symbol stack should be topsign -- [rollback], and then ++ [import Stack]. Here we directly change the symbol topnum --; // stack rollback} else {sign [++ topsign] = signal; // stack feed} return number [topnum];}/*** compare, if (A <= B) of B, true */Boolean comparepriority (char a, char top) {Boolean judge = false is returned; if (A = '+' | A = '-') & (Top = '+' | Top = '-' | Top = '*' | Top = '/')) {judge = true;} If (A = '*' | A = '/') & (Top = '*' | Top = '/') {judge = true;} return judge;} public static void main (string [] ARGs) {try {system. out. println (new demo (). evaluation ("1*(2 + 3/(5-2) + 4 + 5 =");} catch (exception e) {e. printstacktrace (); system. out. println ("sorry, the expression is not a standard positive number expression and the process value cannot be negative ");}}}

Here, you may think that my limitations are relatively large. It is true that this is indeed a relatively high level of water. I have to write out the idea of this algorithm because I have to consider many situations. It is not easy to evaluate the expression.

If anyone could give me a good source code, I would be grateful.

Another thing is, I heard people say that this brace, without recursion, can be operated using three stacks. Maybe I have limited intelligence, I cannot figure out how to operate the three stacks. I am very grateful to the eldest brother who gave me some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.