Leetcode#227.basic Calculator II

Source: Internet
Author: User
Tags integer division

Topic

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers,,, + - * , / operators and empty spaces . The integer division should truncate toward zero.

Assume that the given expression was always valid.

Some Examples:

"3+2*2" = 7 "3/2" = 1 "3+5/2" = 5

A calculator that contains no parentheses, with spaces

Ideas

Take a stack implementation. Scan strings from beginning to end:

    1. If it is a number, as the current members, the previous number left one as a high.
    2. If it is a symbol, if you add and subtract, multiply the number by the symbol into the stack.
    3. If it is the multiplication method, a number is ejected from the stack, multiplied by the current number, in addition to pressing back to the stack.

Note that using a suffix expression, read out the two numbers in the read-out symbol, in the order:

    1. The initial value of the previous symbol is + (because when the first symbol is read, the operation on the first number must be pressed into the stack, the same as the + case)
    2. Read a number and press it into the stack
    3. read out a symbol
    4. Reading out a number
    5. Determines the current operation based on the previous symbol
    6. When the operation is complete, the symbol you just read is recorded as the previous symbol.

This is equivalent to completing the conversion of the normal expression to the suffix expression.

Code
classSolution { Public:    intCalculatestrings) {stack<int>Mystack; CharSign ='+'; intnum =0, res=0;  for(inti =0; I < s.size (); i++){            if(IsDigit (s[i]) num= num *Ten+ S[i]-'0'; if(((!isdigit (s[i)) && (!isspace (s[i))) | | (I==s.size ()-1)){                if(Sign = ='+') Mystack.push (num); if(Sign = ='-') Mystack.push (Num*-1); if(Sign = ='*') {num= Mystack.top () *num;                    Mystack.pop ();                Mystack.push (num); }                if(Sign = ='/') {num= Mystack.top ()/num;                    Mystack.pop ();                Mystack.push (num); } Sign=S[i]; Num=0; }        }         while(!Mystack.empty ()) {Res+=Mystack.top ();        Mystack.pop (); }        returnRes; }};
Other

About "Ctype.h"

Images from Wikipedia

Leetcode#227.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.