Leetcode OJ Basic Calculator II

Source: Internet
Author: User

Basic Calculator II Topics

Ideas

Same as this: Basic Calculator I

Code
classexpressiontransformation { Public:stringtrans_to_postfix_expression_to_s (string);//Convert the resulting expression to a suffix expression    Long Long intCalculate_from_postfix_expression ();//Calculate values based on suffix expressionPrivate: vector<string>Ans_vector_post;//store suffix expression    stringpost_string;//store suffix expression};inline intPriorCharOP) {//Calculate priority function    if(OP = =' + '|| OP = ='-') {return 1; }Else if(OP = =' * '|| OP = ='/'|| OP = ='% ') {return 2; }Else{return 0; }}Long Long intString_to_int (stringIn) {//Convert the input string to the corresponding number function    Chars[ -]; for(inti =0; I < -; i++) {S[i] =' + '; } for(inti =0; I < in.size ();    i++) {S[i] = In[i]; }Long Long intAnssscanf(S,"%lld", &ans);returnAns;}stringDeleteblank (stringIn) {stringAnsintSize = In.size (); for(inti =0; i < size; i++) {if(In[i]! ="') Ans.push_back (In[i]); }returnAns;}stringexpressiontransformation::trans_to_postfix_expression_to_s (stringIn) { Stack<char>Op//Operation Fu YiAns_vector_post.clear ();//suffix expression to hold an array     for(inti =0; I < in.size ();) {Charc = In[i];if(' 0 '<= C && C <=' 9 ') {//Is digital direct insertion            stringNumintJ for(j = i; J < In.size () &&' 0 '<= In[j] && In[j] <=' 9 ';            J + +) {Num.push_back (in[j]);            } ans_vector_post.push_back (num);        i = j; }Else{if(c = =' (') {//is open parenthesis inserted directlyOp.push (' ('); }Else{if(c = =' ) ') {//Is closing the parentheses to output the operators in the original stack until the opening brackets are encountered, note that the opening brackets are discarded                     while(Op.top ()! =' (') {stringTemp                        Temp.push_back (Op.top ());                        Ans_vector_post.push_back (temp);                    Op.pop ();                } op.pop (); }Else{//If it is subtraction to take surplus                    if(Op.empty ()) {//Operation Fu Yi is empty and is inserted directlyOp.push (c); }Else{if the scanned operator takes precedence over the top of the stack operator, the operator is pressed into the stack. Otherwise, the stack operator pops up to the end of the array ans until it encounters a lower priority than the scan-to operator or stack, and presses the scanned operator into the stack                        if(Prior (c) > Prior (Op.top ()))                        {Op.push (c); }Else{ while(!op.empty () && Prior (c) <= Prior (Op.top ())) {stringTemp                                Temp.push_back (Op.top ());                                Ans_vector_post.push_back (temp);                            Op.pop ();                        } op.push (c);        }}}} i++; }    } while(!op.empty ()) {//Note the output of the remaining operator Ching Operation        stringTemp        Temp.push_back (Op.top ());        Ans_vector_post.push_back (temp);    Op.pop (); } post_string.clear ();//Constructs a string and returns     for(inti =0; I < ans_vector_post.size ();    i++) {post_string + = Ans_vector_post[i]; }returnpost_string;}Long Long intExpressiontransformation::calculate_from_postfix_expression () {//using the stack on the suffix expression evaluation, directly from the suffix expression left to right scan, encountered the number in the stack, encountered the character on the top of the stack two numbers out, and then put into the stack     Stack<long long int>Ans_post; for(inti =0; I < ans_vector_post.size (); i++) {Long Long intx, y;if(' 0 '<= ans_vector_post[i][0] && ans_vector_post[i][0] <=' 9 ') {Ans_post.push (String_to_int (ans_vector_post[i)); }Else{y = Ans_post.top ();//Note the order, here is like xy+ is X+yAns_post.pop ();            x = Ans_post.top (); Ans_post.pop ();if(ans_vector_post[i][0] ==' + ') {Ans_post.push (x + y); }Else if(ans_vector_post[i][0] =='-') {Ans_post.push (x-y); }Else if(ans_vector_post[i][0] ==' * ') {Ans_post.push (x * y); }Else if(ans_vector_post[i][0] =='/') {Ans_post.push (x/y); }Else{ans_post.push (x% y); }        }    }returnAns_post.top ();}classSolution { Public:intCalculatestrings) {expressiontransformation E;        s = Deleteblank (s); e.trans_to_postfix_expression_to_s (s);intPostans = E.calculate_from_postfix_expression ();returnPostans; }};

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

Leetcode OJ Basic Calculator II

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.