Implementation of the calculator data structure (JAVA) (iv)

Source: Internet
Author: User

principle:

1. Change the middle order expression to follow-up expression

2. The current character is a number, put that number in the stack

3. The current character is the operator, take out two trees from the stack, according to operator operation, put the result of the operation into the stack

4. Repeat until the character is finished, and there is only one element left in the stack, that is, the result of the operation

PS: I do not handle, can only run 10 of the operation, if necessary can be extended

Package Com.lip.datastructure.tree;import Java.util.iterator;import Java.util.stack;public class Calculator{public static void Main (string[] args) {String obj = "A * (b+c) +c/d"; String obj1 = "1+3 +9/3"; System.out.println (obj1+ "=" +calculator (obj1));} Use post-order expressions to calculate the///principle: 1. When the period character is a letter or a number, it is directly into the stack//2. The current character is an operator that takes out two numbers from the stack//3. Put the results of the calculation into the stack, the last remaining element of the stack is the result public static T Calculator (String obj) {string postobj=tranform (obj); System.out.println (); Stack<integer>stack=new stack<integer> (); for (int i=0;i<postobj.length (); i++) {char ch= Postobj.charat (i); if (Character.isletterordigit (CH))//character or number {Stack.push (Integer.parseint (ch+ ""));} else//operator {//Remove two number int op1,op2;op1=stack.pop (); Op2=stack.pop (); switch (CH) {case ' + ': Stack.push (OP2+OP1); Break;case ' -': Stack.push (OP2-OP1); Break;case ' * ': Stack.push (OP2*OP1); Break;case '/': Stack.push (OP2/OP1); break;default:break ;}}} return Stack.pop ();} The middle sequence traversal changes to subsequent traversal of public static string Tranform (String obj) {stack<character> Stack = new Stack<characteR> (); String obj2 = ""; for (int i = 0; I < obj.length (); i++) {Char ch = obj.charat (i), if (Character.isletterordigit (CH))//letter or digital Direct output {obj2 + = ch; System.out.print (CH);} else if (ch = = ') ') operations characters stop out of stack {char temp;while (temp = Stack.pop ())! = ' (') {obj2 + = temp; System.out.print (temp);}} else//the stack priority of the comparison operator {if (Stack.isempty ()) {Stack.push (ch); continue;} Char temp = Stack.peek (), while (ICP (CH) <= ISP (temp))//stack priority is less than the priority in the stack, the stack {System.out.print (temp); Obj2 + = temp; Stack.pop (); if (Stack.isempty ()) break;temp = Stack.peek ();} Stack.push (ch);}} Bounce the remaining elements out of the stack while (!stack.isempty ()) {Char temp = Stack.pop (); Obj2 + = temp; System.out.print (temp);} return obj2;} Operator precedence within stack private static int ISP (char ch) {switch (ch) {case ' + ': Case '-': return 2;case ' * ': Case '/': Return 4;case ') ': RE Turn 7;case ' (': Return 1;default:break;} return 0;} Precedence priority of the operator into the stack private static int ICP (char ch) {switch (ch) {case ' + ': Case '-': return 3;case ' * ': Case '/': Return 5;case ') ': Return 1;case ' (': return 7;default:break;}return 0;}} 

Implementation of the calculator data structure (JAVA) (iv)

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.