Southwest min da OJ 1762 my formula can't be that hard to write "Polish style"

Source: Internet
Author: User

Describe

la La. As a bitter procedural ape. Please look at ...

now Mister wants you to help his son write a simple calculator (his son elementary 3 grade, Shh!) ), can not write out on the deduction of bonuses. Just help him write it ...

to a correct expression that contains the +-*/(). Want you to calculate the result of it. The rule of division is similar to the C + + divide method. For example: 9/5=1 10/5=2 other rules refer to reality ...

Input

example of multi-group test sample

Line string (less than 200 length)

All numbers participating in the operation are less than 1000 positive integers.

There are spaces in an expression.

Data is guaranteed to be legal.

Output

One line The result of an expression

Sample input

1+2
(5-6/(1+2) +10) *8

Sample output

3
107

Tips

←_←

Say my example is very conscience.

Idea: A long time ago the southwest People's big game has done the problem, actually is to give you an expression to let you evaluate, we can first turn it into a Polish style, then the Polish-style evaluation is much simpler

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <string.h>5#include <stack>6 #defineMAXN 1000097 using namespacestd;8stack<Char>Q;9stack<Long Long>p;Ten Long Longpriorit[ +],ans[maxn],h; One BOOLOPP[MAXN]; A CharCH[MAXN]; - intMain () - { thepriorit[(int)'+']=priorit[(int)'-']=1; -priorit[(int)'*']=priorit[(int)'/']=2; -priorit[(int)'(']=0; -      while(Gets (ch+1)!=NULL) +     { -          while(!q.empty ()) Q.pop (); +          while(!p.empty ()) P.pop (); AH=0; atmemset (ans,0,sizeof(ans)); -memset (OPP,0,sizeof(OPP)); -         Long LongLen=strlen (ch+1), idx=1, j=1; -          for(intI=1; i<=len;i++) -         { -             if(ch[i]!=' ') ch[j++]=Ch[i]; in         } -len=j-1; to          while(idx<=len) +         { -             Long Longnum=0, flag=0; the              while(ch[idx]>='0'&&ch[idx]<='9'&&idx<=len) *             { $num=num*Ten+ch[idx++]-'0';Panax Notoginsengflag=1; -             } the             if(flag==0) +             { A                 if(ch[idx]=='(') Q.push ('('); the                 Else if(ch[idx]==')') +                 { -                      while(!q.empty () &&q.top ()! ='(') $                     { $ans[++h]=-(Long Long) Q.top (); -opp[h]=1; - Q.pop (); the                     } - Q.pop ();Wuyi                 } the                 Else -                 { Wu                      while(!q.empty () &&priorit[(Long Long) Q.top ()]>=priorit[(int) Ch[idx]) -                     { Aboutans[++h]=-(int) Q.top (); $opp[h]=1; - Q.pop (); -                     } - Q.push (Ch[idx]); A                 } +             } the             Else -             { $ans[++h]=num; the             } the             if(flag==0) idx++; the         } the          while(!q.empty ()) -         { inans[++h]=-(Long Long) Q.top (); theopp[h]=1; the Q.pop (); About         } the          for(intI=1; i<=h;i++) the         { the            //printf ("%i64d", Ans[i]); +             if(opp[i]==0) P.push (Ans[i]); -             Else the             {Bayi                 Long Longu=p.top (); the P.pop (); the                 Long Longv=p.top (); - P.pop (); -                 if(Ans[i]==-(int)'-') P.push (V-u); the                 if(Ans[i]==-(int)'+') P.push (u+v); the                 if(Ans[i]==-(int)'*') P.push (u*v); the                 if(Ans[i]==-(int)'/') P.push (v/u); the             } -         } the         if(!p.empty ()) printf ("%i64d\n", P.top ()); the         Elseprintf"0\n"); the     }94}

Southwest min da OJ 1762 my formula can't be that hard to write "Polish style"

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.