Big talk Data Structure-use a stack to implement simple four arithmetic operations and four arithmetic operations on the Data Structure

Source: Internet
Author: User

Big talk Data Structure-use a stack to implement simple four arithmetic operations and four arithmetic operations on the Data Structure

I recently read "big talk Data Structure". Here is an example of using the stack to implement four arithmetic operations. Now I want to use java to implement this function.

The Code is as follows:

Package com. datastruct; import java. util. arrayList; import java. util. stack; import java. util. regex. matcher; import java. util. regex. pattern; public class StackPractice {private static ArrayList <String> symbolList; static {symbolList = new ArrayList <> (); symbolList. add (SymbolEnum. ADD. getVal (); symbolList. add (SymbolEnum. SUBTRACT. getVal (); symbolList. add (SymbolEnum. MULTIPLY. getVal (); symbolList. add (SymbolEnum. DIVIDE. getVal (); symbolList. add (SymbolEnum. LEFT_BRACKET.getVal (); symbolList. add (SymbolEnum. RIGHT_BRACKET.getVal ();} public static void main (String [] args) {String exp = "9 + (3-1) * 3 + 10/2 "; try {String exp2 = convertExp (exp); System. out. println (cal (exp2);} catch (Exception e) {System. out. println (e. getMessage () ;}} private static boolean isNumeric (String str) {Pattern pattern = Pattern. compile ("[0-9] *" ); Matcher isNum = pattern. matcher (str); if (! IsNum. matches () {return false;} return true;}/*** calculates the suffix expression value * @ return */private static int cal (String exp) {String [] expArr = exp. split (""); Stack <String> calStack = new Stack <String> (); for (int I = 0; I <expArr. length; I ++) {String tmp = expArr [I]; if (isNumeric (tmp) {// number into the stack calStack. push (tmp);} else {int num1 = Integer. parseInt (calStack. pop (); // The 1int num2 = Integer is displayed on the top of the stack. parseInt (calStack. pop (); // 2 swit pops up on the top of the stack Ch (tmp) {case "-": calStack. push (num2-num1 + ""); break; case "+": calStack. push (num2 + num1 + ""); break; case "*": calStack. push (num2 * num1 + ""); break; case "/": calStack. push (num2/num1 + ""); break; default: break ;}} return Integer. parseInt (calStack. pop ();}/*** convert the infix expression to the suffix expression * @ return */private static String convertExp (String exp) throws Exception {// convert string to array char [] expArr = exp. toCharArray (); StringBuf Fer sb = new StringBuffer (); Stack <String> symbolStack = new Stack <> (); boolean isLastCharNumberFlag = true; for (int I = 0; I <expArr. length; I ++) {char curChar = expArr [I]; // if a number is added, if it is a symbol, it is placed into the stack for calculation if (Character. isDigit (curChar) {if (isLastCharNumberFlag) {sb. append (curChar);} else {sb. append (""). append (curChar);} isLastCharNumberFlag = true;} else if (symbolList. contains (String. valueOf (curChar) {// symbol // String symbol = String. valueOf (curChar); if (SymbolEnum. RIGHT_BRACKET.getVal (). equals (symbol) {// if it is a right brace, you must match the left brace String symbolStr = symbolStack. pop (); while (! SymbolEnum. LEFT_BRACKET.getVal (). equals (symbolStr) {sb. append (""). append (symbolStr); symbolStr = symbolStack. pop ();} isLastCharNumberFlag = false; continue;} else if (SymbolEnum. DIVIDE. getVal (). equals (symbol) | SymbolEnum. MULTIPLY. getVal (). equals (symbol) {} else {if (symbolStack. isEmpty () {symbolStack. push (symbol); continue;} String symbolStr = symbolStack. peek (); if (SymbolEnum. DIVIDE. getVal (). equals (symbolS Tr) | SymbolEnum. MULTIPLY. getVal (). equals (symbolStr) {while (! SymbolStack. isEmpty () {sb. append (""). append (symbolStack. pop ();} symbolStack. push (symbol); isLastCharNumberFlag = false; continue ;}} symbolStack. push (symbol); isLastCharNumberFlag = false ;}} while (! SymbolStack. isEmpty () {sb. append (""). append (symbolStack. pop ();} return sb. toString ();}/*** operator enumeration * @ author Administrator **/enum SymbolEnum {ADD ("+"), SUBTRACT ("-"), MULTIPLY ("*"), DIVIDE ("/"), LEFT_BRACKET ("("), RIGHT_BRACKET (")"); private String val; public String getVal () {return val;} private SymbolEnum (String val) {this. val = val ;}}}

The program has some bugs for the time being. For example, the expression validity is not verified and multiple parentheses cannot be entered.

Related Article

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.