Algorithm title: Arithmetic (infix to postfix conversion, the inverse of the Polish style worth thinking about)

Source: Internet
Author: User

/ * Arithmetic of the string. Give a string that contains the 0~9 number and the +-*\/() operator,-only the minus sign does not represent a negative. Examples are as follows: Input: 1 + 2 * (3-4) *///Haha, see this problem, in fact, it is not difficult, the problem is not to think,//Of course you understand the algorithm, the algorithm used here is inverse Polish style. //If you do not understand the place, you can search the Internet to reverse the Polish style. /* My summary: The computer can not understand the human positive thinking, so in order to meet the computer's thinking, we will do the opposite, the operation symbol is placed behind the operand, to form a suffix expression, but if you can according to the Encyclopedia of the thinking to do, then easy to get the answer. *//*1. Constructs an inverse Polish expression based on an expression. 2. Calculation according to the inverse Polish expression. 3. The result can be easily obtained in the end, I hope you can learn, mutual encouragement. *///start:#include <iostream>#include <string.h>#include <stack>using namespace STD;BOOLIs_op (CharCH) {return(ch = =' + '|| ch = ='-'|| ch = =' * '|| ch = ='/'|| ch = =' ('|| ch = =' ) '|| ch = =' # ');}BOOLIs_com (CharCH1,CharCH2) {//Compare symbol expression is priority.     inti =0;intj =0; for(;"#+-*/()"[i]! = CH1; i++) {} for(;"#+-*/()"[j]! = CH2; J + +) {}BOOLstr[][7] = {1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1};returnSTR[I][J];}intGrial (Char*&AMP;STR) {//Check if the brace match is ok first.     intFlags =0;Char*FP = str; while(*FP! =' + ')    {if(*FP = =' (') flags++;if(*FP = =' ) ') flags--;    fp++; }if(Flags! =0)return-1;//Start constructing Polish expressions.     strcat(STR,"#");Char*p = str;Char*q = str; Stack<char>OP; Op.push (' # '); Stack<string>NM;intCount =0;stringS while(*p! =' + ')    {if(Is_op (*P)) {if(*p = =' ) ')            { while(Op.top ()! =' (') {s = Op.top ();                    Nm.push (s);                Op.pop (); }if(Op.top () = =' (') {Op.pop (); p++;Continue; }            }if(Is_com (Op.top (), *p) && op.top ()! =' (')            { while(Is_com (Op.top (), *p))                    {s = op.top ();                    Nm.push (s); Op.pop ();if(*p = =' # '&& op.top () = =' # ') Break;            } op.push (*p); }Else{Op.push (*p);        } p++; }Else{ while(! Is_op (*p)) {count = Count *Ten+ *p-' 0 '; p++;if(*p = =' + '|| *p = =' # ') Break; }Charbuff[255];memset(Buff,0,sizeof(buff)); Itoa (count, Buff,Ten);            s = buff;            Nm.push (s); Count =0; }    } Stack<string>Numst; while(Nm.empty () = =false) {Numst.push (Nm.top ());//reverse order. Nm.pop (); }//So far the inverse Polish style has been constructed, let's calculate it.      Stack<int>Temp while(Numst.empty () = =false)    {if(Numst.top () = ="+"|| Numst.top () = ="-"|| Numst.top () = ="*"|| Numst.top () = ="/")        {intA = Temp.top (); Temp.pop ();intb = Temp.top (); Temp.pop ();if(Numst.top () = ="+") {Temp.push (b + a); }Else if(Numst.top () = ="-") {Temp.push (b-a); }Else if(Numst.top () = ="*") {Temp.push (b*a); }Else if(Numst.top () = ="/") {Temp.push (b/a);        } numst.pop (); }Else{Temp.push (Atoi (Numst.top (). C_STR ()));        Numst.pop (); }    }returnTemp.top ();}intMain () {;Char*s =New Char[ -];memset(S,0,sizeof(s));strcpy(S,"(1+2) *3-(1+2)/4+10");cout<< grial (s) << Endl;strcpy(S,"1+2+4+ (1*2)/3+4");cout<< grial (s) << Endl;strcpy(S,"+1+2-1*2" (4/2));cout<< grial (s) << Endl;strcpy(S,(1+ (2+3) +2-0));//I am also concerned that my double brackets are not taken into account,    //Didn't think of a success.     cout<< grial (s) << Endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Algorithm title: Arithmetic (infix to postfix conversion, the inverse of the Polish style worth thinking about)

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.