• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            使用c++的traits技術實現的任意類型元素的表達式求值

            這里以整形和浮點型表達式為例。

            #include <iostream>
            #include <
            string>
            #include <cmath>
            #include <sstream>
            #include <stack>
            #include <queue>
            using namespace std;

            template <typename T>
            struct Type{static bool isPrefix(char ch){return 0;}};

            //如果chdouble類型的合法前綴返回true,否則返回false
            template <> struct Type<double>{
               
            static bool isPrefix(char ch){
                   
            return ch=='.' || isdigit(ch);
                }
            };

            template <>
            struct Type<int>{
               
            static bool isPrefix(char ch){
                   
            return isdigit(ch);
                }
            };

            template <
            class T>
            class Expression{
               
            string instr;
                queue<T> Q;
            public:
               
            //用字符串str初始化表達式
                Expression(string str){instr=str;}

               
            //返回表達式的值
                T value(){return eval(postfix(instr));}

               
            static int prcd(char c1,char c2)
                {
            //c1的運算先于c2時返回真,否則返回假
                    if(c1=='^' &&c2=='^')return 0;
                   
            if(c1=='(')return 0;
                   
            if(c2==')')return 1;
                   
            if(c2=='(')return 0;
                   
            static char *str[]={"+-","*/","^",NULL};
                   
            int i1=0,i2=0;
                   
            for(int i=0;str[i];i++){
                       
            for(int j=0;str[i][j];j++){
                           
            if(str[i][j]==c1)i1=i;
                           
            if(str[i][j]==c2)i2=i;
                        }
                    }
                   
            return (i1>=i2);
                }

               
            string postfix(string instr)
                {
            //返回中綴表達式instr的后綴表達式,其中的操作數用o代替,并存入隊列Q
                    istringstream iss(instr);//輸入流
                    ostringstream oss;
                    stack<
            char> S;
                   
            for (char ch;iss>>ch;){
                       
            if(Type<T>::isPrefix(ch)){
                            iss.unget();
                            T s;
                            iss>>s;
                            oss<<'o';
                            Q.push(s);
                        }
                       
            else {
                           
            while(!S.empty() && prcd(S.top(),ch)){
                                oss<<S.top();S.pop();
                            }
                           
            if(ch!=')') S.push(ch);
                           
            else S.pop();
                        }
                    }
                   
            while(!S.empty()){
                        oss<<S.top();S.pop();
                    }
                   
            return oss.str();
                }

               
            static T oper(char symb,T& ta,const T& tb)
                {
                   
            switch(symb){
                   
            case '+':return ta+=tb;
                   
            case '-':return ta-=tb;
                   
            case '*':return ta*=tb;
                   
            case '/':return ta/=tb;
                   
            case '^':return ta=(T)pow(ta,(double)tb);
                   
            default:
                        cerr<<"
            未定義的運算符:"<<symb<<endl;exit(1);
                    }
                }

                T eval(
            string poststr)
                {
            //返回后綴表達式poststr的值
                    istringstream iss(poststr);//輸入流
                    stack<T> S;
                   
            for (char ch;iss>>ch;){
                       
            if(ch=='o'){
                            T v=Q.front();Q.pop();
                            S.push(v);
                        }
                       
            else{
                            T tb=S.top();S.pop();
                            oper(ch,S.top(),tb);
                        }
                    }
                    T t=S.top();S.pop();
                   
            return t;
                }
            };

            int main()
            {
               
            string line;
               
            while(getline(cin, line)){
                    Expression<
            double> e(line);
                    cout<< e.value() <<endl;
                    Expression<
            int> e2(line);
                    cout<< e2.value() <<endl;
                }
            }


            不止如此,它還可以實現自定義類型的表達式的求值,下面是一個對自定義集合類型的交、并、補運算進行求值。
            問題的描述見(http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=958 title:friends

            #include <iostream>
            #include <algorithm>
            #include <vector>
            #include <
            string>
            #include <cmath>
            #include <sstream>
            #include <cstdlib>
            #include <stack>
            #include <queue>
            using namespace std;

            struct Set{
                unsigned
            int date;
                Set(){}
                Set(
            const Set& b){date=b.date;}

                Set&
            operator += (const Set& b){
                    date|=(b.date);
                   
            return *this;
                }

                Set&
            operator *= (const Set& b){
                    date&=(b.date);
                   
            return *this;
                }

                Set&
            operator -= (const Set& b){
                    date&=(~b.date);
                   
            return *this;
                }

                Set
            operator - (){
                    date=~date;
                    date&=((1<<26)-1);
                   
            return *this;
                }
                friend istringstream&
            operator >> (istringstream& ss,Set& obj);
                friend ostream&
            operator << (ostream& os,const Set& obj);
            };

            template <typename T>
            struct Type{static bool isPrefix(char ch){return 0;}};

            //如果chdouble類型的合法前綴返回true,否則返回false
            template <> struct Type<Set>{
               
            static bool isPrefix(char ch){
                   
            return ch=='{';
                }
            };

            istringstream&
            operator >> (istringstream& ss,Set& obj){
                obj.date=0;
               
            char ch;
                ss>>ch;
            //去掉{
                while(ss>>ch,ch!='}'){
                    obj.date|= (1<<(ch-'A'));
                }
               
            return ss;
            }

            ostream&
            operator << (ostream& os,const Set& obj){
                os<<'{';
               
            for(int i=0;i<26;i++){
                   
            if(obj.date&(1<<i)){
                        os<<(
            char)('A'+i);
                    }
                }
                os<<'}';
               
            return os;
            }

            template <
            class T>
            class Expression{
               
            string instr;
                queue<T> Q;
            public:
               
            //用字符串str初始化表達式
                Expression(string str){instr=str;}

               
            //返回表達式的值
                T value(){return eval(postfix(instr));}

               
            static int prcd(char c1,char c2)
                {
            //c1的運算先于c2時返回真,否則返回假
                    if(c1=='^' &&c2=='^')return 0;
                   
            if(c1=='(')return 0;
                   
            if(c2==')')return 1;
                   
            if(c2=='(')return 0;
                   
            static char *str[]={"+-","*/","^",NULL};
                   
            int i1=0,i2=0;
                   
            for(int i=0;str[i];i++){
                       
            for(int j=0;str[i][j];j++){
                           
            if(str[i][j]==c1)i1=i;
                           
            if(str[i][j]==c2)i2=i;
                        }
                    }
                   
            return (i1>=i2);
                }

               
            string postfix(string instr)
                {
            //返回中綴表達式instr的后綴表達式,其中的操作數用o代替,并存入隊列Q
                    istringstream iss(instr);//輸入流
                    ostringstream oss;
                    stack<
            char> S;
                   
            for (char ch;iss>>ch;){
                       
            if(Type<T>::isPrefix(ch)){
                            iss.unget();
                            T s;
                            iss>>s;
                            oss<<'o';
                            Q.push(s);
                        }
                       
            else {
                           
            while(!S.empty() && prcd(S.top(),ch)){
                                oss<<S.top();S.pop();
                            }
                           
            if(ch!=')') S.push(ch);
                           
            else S.pop();
                        }
                    }
                   
            while(!S.empty()){
                        oss<<S.top();S.pop();
                    }
                   
            return oss.str();
                }

               
            static T oper(char symb,T& ta,const T& tb)
                {
                   
            switch(symb){
                   
            case '+':return ta+=tb;
                   
            case '-':return ta-=tb;
                   
            case '*':return ta*=tb;
                   
            //case '/':return ta/=tb;
                    //case '^':return ta=(T)pow(ta,(double)tb);
                    default:
                        cerr<<"
            未定義的運算符:"<<symb<<endl;exit(1);
                    }
                }

                T eval(
            string poststr)
                {
            //返回后綴表達式poststr的值
                    istringstream iss(poststr);//輸入流
                    stack<T> S;
                   
            for (char ch;iss>>ch;){
                       
            if(ch=='o'){
                            T v=Q.front();Q.pop();
                            S.push(v);
                        }
                       
            else{
                            T tb=S.top();S.pop();
                            oper(ch,S.top(),tb);
                        }
                    }
                    T t=S.top();S.pop();
                   
            return t;
                }
            };


            int main()
            {
               
            string line;
               
            while(getline(cin, line)){
                    Expression<Set> e(line);
                    cout<< e.value() <<endl;
                }
            }

             

             

             

            posted on 2008-11-08 22:51 肥仔 閱讀(589) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            av无码久久久久不卡免费网站 | 成人a毛片久久免费播放| 伊人久久大香线蕉av不变影院| 99精品久久久久中文字幕| 精品久久久久久国产| 99久久亚洲综合精品网站| 天天久久狠狠色综合| 久久免费国产精品| 久久精品人成免费| 青青久久精品国产免费看| 久久久久久久97| 亚洲午夜久久久| 久久99久久99精品免视看动漫| 99久久国产综合精品成人影院| 精品久久综合1区2区3区激情| 久久久久久亚洲精品不卡 | 性做久久久久久久久浪潮| 国内精品伊人久久久久777| 国产一区二区三精品久久久无广告| 亚洲婷婷国产精品电影人久久| 人妻少妇精品久久| 久久国产乱子伦免费精品| 久久精品视频91| 久久国产乱子伦精品免费强| 亚洲狠狠婷婷综合久久久久| 久久久久国产亚洲AV麻豆| 久久久久国产一级毛片高清版| 久久婷婷五月综合色奶水99啪| 欧美久久一级内射wwwwww.| 久久精品国产99久久久| 久久中文骚妇内射| 亚洲日本久久久午夜精品| 久久国产免费| 久久国产成人午夜aⅴ影院 | 九九精品99久久久香蕉| 久久久无码精品亚洲日韩京东传媒| 久久综合九色综合久99| 久久久久se色偷偷亚洲精品av | 色88久久久久高潮综合影院 | 久久综合久久综合久久| 国产精品久久久久久|