Go language stack implementation arithmetic expression

Source: Internet
Author: User

The middle sequence expression is converted into a post-order expression, and the value is computed by the post -ordering expression .

Package main//expression//author:xiong Chuan liang//date:2015-2-2import ("FMT" "Github.com/xcltapestry/xclpkg/algorithm" " StrConv "" Errors ") func main () {//Ordinal expression post-order expression//A+b = ab+//(a+b)/c = ab+c///a+b* (c+d) = abcd+ *+//a*b+c* (D-E)/f = ab*cde-*f/+//str: = "a*b+c* (D-E)/F"//str: = "1*2+3* (4-5)/6" str: = "1*2+3* (5-1)/2" Exp,err: = Expconve RT (STR) If err! = Nil {fmt. Println ("The middle order expression fails!", err)}else{exp (EXP)}//v: = 1*2+3* (4-5)/6v: = 1*2+3* (5-1)/2fmt. PRINTLN ("Standard result:", v)}func Expconvert (str string) (String,error) {var result stringstack: = algorithm. Newstack () for _,s: = range str {ch: = string (s) if Isoperator (CH) {//Is the operator if stack. Empty () | | ch = = "(" {stack. Push (ch)}else{if ch = = ")" {//processing brackets for{if stack. Empty () {return "", errors. New ("There is a problem with the expression!" The corresponding \ "(\" number ")}if stack was not found. Top (). (string) = = "(" {break}result + = stack. Top (). (string) stack. Pop ()}//pops Up "(" stack. Top () stack. Pop ()}else{//non-bracket//Comparison priority for{if stack. Empty () {break} m: = Stack. Top (). (string) if priority (CH) > PrioriTy (m) {break} result + = M stack. Pop ()} stack. Push (CH)}}}else{//non-operator result + = ch}//end isoperator ()}//end for range strfor {if stack. Empty () {break}result + = stack. Top (). (string) stack. Pop ()}fmt. Println ("Expconvert () str =", str) FMT. Println ("expconvert () result =", result) return Result,nil}func Exp (str string) {FMT. Println ("\ncalc \nexp ():", str) stack: = algorithm. Newstack () for _,s: = range str {ch: = string (s) if Isoperator (CH) {//Is the operator if stack. Empty () {break}b: = stack. Top (). (string) stack. Pop () A: = Stack. Top (). (string) stack. Pop () Ia,ib: = ConvToInt32 (A, b) SV: = FMT. Sprintf ("%d", Calc (Ch,ia,ib)) stack. Push (SV) fmt. Println ("Exp ()", A, "", CH, "", B, "=", SV)}else{stack. Push (CH) fmt. Println ("Exp () ch:", ch)}//end isoperator}//stack. Print () if!stack. Empty () {fmt. Println ("Expression operation result:", stack.) Top ()) stack. Pop ()}}func ConvToInt32 (A, B string) (Int32,int32) {ia, Erra: = StrConv. Parseint (A, ten, +) if Erra! = nil {panic (Erra)}ib, ERRB: = StrConv. parseint (b, ten, +) if ERRB! = nil {Panic (ERRB)}return Int32 (IA), Int32 (IB)}func isoperator (OP string) (bool) {switch (OP) {case "(", ")", "+", "-", "*", "/": Return Truedefault:return False}}func Priority (OP string) (int) {switch (OP) {case "*", "/": Return 3case "+", "-": Return 2case "(": return 1default:return 0}}func Calc (Op string,a,b Int32) (Int32) {switch (OP) {case ' * ': return (A * b) case "/": Return (A/b) c ASE "+": return (A + B) case "-": Return (a) Default:return 0}}

Result of Operation:

Expconvert () str    =  1*2+3* (5-1)/2expconvert () result =  12*351-*2/+calcexp (): 12*351-*2/+exp () Ch:  1EXP () Ch:  2Exp ()  1  *  2 = 2EXP () Ch:  3Exp () Ch:  5Exp () Ch:  1Exp ()  5  -  1 = 4EXP ()  3  *  4 = 12EXP () Ch:  2Exp ()  /  2 = 6EXP ()  2  +  6 = 8 expression operation Result:  8 Standard results:  8



MAIL: [Email protected]

blog:http://blog.csdn/net/xcl168






Go language stack implementation arithmetic expression

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.