"The highest end of the entire network?" infix expression to suffix expression and evaluation (can be used for negative numbers, factorial)

Source: Internet
Author: User

There are comments in the code ... Directly on the code ...

#include <bits/stdc++.h> #define REP (I,k,n) for (int. i=k;i<=n;i++) #define PER (i,n,k) for (int i=n;i>=k;i--) # Define PII pair<int,int> #define PB push_back#define MP make_pair#define re return#define se second#define fi firstus ing namespace std;//--------------------------------------------------------------- Head----------------------------------------------------------------------struct BigInteger {typedef unsigned long    Long LL;    static const int BASE = 100000000;    static const int WIDTH = 8;    Vector<int> s;    biginteger& Clean () {while (!s.back () &&s.size () >1) S.pop_back (); return *this;}    BigInteger (LL num = 0) {*this = num;}    BigInteger (string s) {*this = s;}        biginteger& operator = (long long num) {s.clear ();            do {s.push_back (num% BASE);        Num/= BASE;        } while (num > 0);    return *this;        } biginteger& operator = (const string& str) {s.clear (); int x, Len= (Str.length ()-1)/WIDTH + 1;            for (int i = 0; i < len; i++) {int end = Str.length ()-i*width;            int start = max (0, End-width);            SSCANF (Str.substr (Start,end-start). C_STR (), "%d", &x);        S.push_back (x);    } return (*this). Clean ();        } BigInteger operator + (const biginteger& b) const {BigInteger C; c.s.clear ();            for (int i = 0, g = 0;; i++) {if (g = = 0 && i >= s.size () && i >= b.s.size ()) break;            int x = g;            if (I < s.size ()) x + = S[i];            if (I < b.s.size ()) x + = B.s[i];            C.s.push_back (x% BASE);        g = x/base;    } return C; } BigInteger Operator-(const biginteger& B) Const {ASSERT (b <= *this);//meiosis cannot be greater than the meiosis BigInteger C        C.s.clear ();       for (int i = 0, g = 0;; i++) {if (g = = 0 && i >= s.size () && i >= b.s.size ()) break;     int x = s[i] + G;            if (I < b.s.size ()) x-= B.s[i];            if (x < 0) {g =-1; x + = BASE;} else g = 0;        C.s.push_back (x);    } return C.clean (); } BigInteger operator * (const biginteger& b) const {int I, J;        LL G;        Vector<ll> V (s.size () +b.s.size (), 0); BigInteger C;        C.s.clear ();        For (I=0;i<s.size (), i++) for (J=0;j<b.s.size (); j + +) V[i+j]+=ll (S[i]) *b.s[j];            for (i = 0, g = 0;; i++) {if (G-==0 && i >= v.size ()) break;            LL x = v[i] + G;            C.s.push_back (x% BASE);        g = x/base;    } return C.clean ();  } BigInteger Operator/(const biginteger& B) Const {ASSERT (b > 0);       Divisor must be greater than 0 BigInteger C = *this;               Business: Mainly to let C.s and (*this). s vector as large as BigInteger m;            Remainder: initialized to 0 for (int i = S.size ()-1; I >= 0; i--) {m = m*base + s[i];    C.s[i] = bsearch (b, M);        M-= b*c.s[i];    } return C.clean ();        } BigInteger operator% (const biginteger& b) Const {//method same as Division BigInteger c = *this;        BigInteger m;            for (int i = S.size ()-1; I >= 0; i--) {m = m*base + s[i];            C.s[i] = bsearch (b, M);        M-= b*c.s[i];    } return m;        } int bsearch (const biginteger& B, const biginteger& m) const{int L = 0, R = BASE-1, x;            while (1) {x = (l+r) >>1;            if (b*x<=m) {if (b* (x+1) >m) return x; else L = x;}        else R = x;    }} biginteger& operator + = (Const biginteger& b) {*this = *this + b; return *this;}    biginteger& operator-= (const biginteger& b) {*this = *this-b; return *this;}    biginteger& operator *= (const biginteger& b) {*this = *this * b; return *this;}    biginteger& operator/= (const biginteger& b) {*this = *this/b; return *this;} biginteger&Operator%= (const biginteger& b) {*this = *this% B; return *this;}        BOOL operator < (const biginteger& b) Const {if (s.size ()! = B.s.size ()) return s.size () < B.s.size ();        for (int i = S.size ()-1; I >= 0; i--) if (s[i]! = B.s[i]) return s[i] < b.s[i];    return false;    } BOOL operator > (const biginteger& B) Const{return B < *this;} BOOL Operator<= (const biginteger& b) Const{return! (    b < *this);} BOOL Operator>= (const biginteger& b) Const{return! (    *this < b);}    BOOL Operator!= (const biginteger& b) Const{return b < *this | | *this < b;} BOOL operator== (const biginteger& b) Const{return! ( b < *this) &&! (b > *this);}};o    stream& operator << (ostream& out, const biginteger& x) {out << x.s.back ();        for (int i = X.s.size ()-2; I >= 0; i--) {char buf[20];        sprintf (buf, "%08d", X.s[i]); for (int j = 0; J < strlen (buf);    J + +) out << buf[j]; } return out;}    istream& operator >> (istream& in, biginteger& x) {string S; if (! (    In >> s)) return in;    x = S; return in;} stack<biginteger> si;stack<char> sc;vector<biginteger> vc;bool isnum[1005];bool flag=0; BigInteger Pow (BigInteger A,biginteger b) {BigInteger base=a,ans=1;while (b!=0) {if (b%2==1) ans*=base;base*=base;b/=2;} return ans;}    BigInteger Clas (char c)//symbol Priority {if (c== ' (' | | c== ') ') re 0;    if (c== ' + ' | | c== '-') re 1;    if (c== ' * ' | | c== '/') re 2;    if (c== ' ^ ') re 3; Re 4;}    BigInteger cal (BigInteger a,biginteger b,biginteger C)//symbol evaluation {BigInteger Ans=1;    if (c== ' + ') re a+b;    if (c== '-') re b-a;    if (c== ' * ') re a*b;    if (c== '/') re b/a; Re pow (b,a);}    int main () {string s;cin>>s;    S= "(" +s+ ")";//can be used to prevent the stack is empty to find top when the overflow condition int n= (int) s.size ();            Rep (i,0,n-1) {if (s[i]== ' (')//left parenthesis directly pressed into {Sc.push (s[i]);        Continue } if (s[i]== ') ')//Find the right parenthesis to empty the stack to the opening parenthesis {char c=sc.top (); Sc.pop ();                while (c!= ' (') {VC.PB (c);            C=sc.top (); Sc.pop ();        } continue;            } if (s[i]>= ' 0 ' && s[i]<= ' 9 ')//number {BigInteger sum=0;            while (s[i]>= ' 0 ' && s[i]<= ' 9 ') sum=sum*10+s[i++]-' 0 ';            if (flag) Sum=biginteger ( -1) *sum,flag=0;            VC.PB (sum);            Isnum[vc.size () -1]=1;        I--;continue;            } if (s[i]== '-' && (Clas (s[i-1])!=4 && s[i-1]!= ') ') {flag=1;        Continue         } while (!sc.empty () && clas (S[i]) <=clas (Sc.top ())//If the symbol is found the first priority is smaller than his VC.PB (Sc.top ()), Sc.pop ();    Sc.push (S[i]);    } while (!sc.empty ())//emptying stack VC.PB (Sc.top ()), Sc.pop ();           Rep (I,0,vc.size ()-1)//evaluated by suffix expression {if (!isnum[i]) {BigInteger a=si.top (); Si.pop (); BigInteger b=si.top (); Si.pop ();            BigInteger ans=cal (A,b,vc[i]);        Si.push (ANS);    } else Si.push (Vc[i]);    } cout<<si.top (); return 0;} ----------------------------------------------------------------End---------------------------------------------------- ------------------

"The highest end of the entire network?" Infix expression to postfix expression and evaluation (available for negative numbers, factorial)

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.