• <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 肥仔 閱讀(587) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            色综合久久夜色精品国产| 国内精品久久久久影院优| 婷婷五月深深久久精品| 国产69精品久久久久APP下载| 欧美亚洲另类久久综合婷婷| 亚洲色欲久久久久综合网| 亚洲国产一成人久久精品| 精品无码久久久久国产动漫3d| 亚洲AV日韩AV天堂久久| 国内精品伊人久久久久妇| 久久精品中文无码资源站| 人妻精品久久无码区| 国产免费久久精品99久久| 伊色综合久久之综合久久| 亚洲精品乱码久久久久久不卡| 少妇被又大又粗又爽毛片久久黑人| 日本欧美国产精品第一页久久| 亚洲国产成人久久综合野外| 久久午夜夜伦鲁鲁片免费无码影视 | 成人久久精品一区二区三区 | 精品久久人人爽天天玩人人妻| 久久久久久久综合狠狠综合| 亚洲国产精品成人久久| 国产精品久久国产精品99盘| 精品人妻伦九区久久AAA片69| 亚洲国产精品无码久久久秋霞2| 久久天天躁狠狠躁夜夜avapp| 日本精品久久久久中文字幕| 欧美成人免费观看久久| 久久综合狠狠综合久久| 国产精品免费看久久久香蕉| 中文字幕无码久久久| 97久久超碰国产精品旧版| 久久久人妻精品无码一区| 狠狠色婷婷综合天天久久丁香| 久久人妻少妇嫩草AV蜜桃| 国内精品久久久久久99| 青青热久久国产久精品 | 99久久超碰中文字幕伊人| 亚洲Av无码国产情品久久| 东京热TOKYO综合久久精品|