比这篇新的文章:
华容道最短路径求解程序
比这篇旧的文章: postfix.h
作者: xpycc, 点击729次, 评论(1), 收藏者(0), , 打分:
所有评论,共1条:( 我也来说两句)
比这篇旧的文章: postfix.h
char_identification.h
语言: C++, 标签: 无 2008/09/30发布 1年前更新 更新记录作者: xpycc, 点击729次, 评论(1), 收藏者(0), , 打分:
C++语言: char_identification.h
001 #ifndef CHAR_IDENTIFICATION_H
002 #define CHAR_IDENTIFICATION_H
003
004 #include <cctype>
005
006 namespace {
007
008 inline bool isoperator(int ch){
009 return ch=='+' || ch=='-' || ch=='*' ||
010 ch=='/' || ch=='^';
011 }
012
013 template<class TChar>
014 struct Reference{
015 typedef const TChar& Result;
016 };
017
018 template<>
019 struct Reference<char>{
020 typedef char Result;
021 };
022
023 template<>
024 struct Reference<short>{
025 typedef short Result;
026 };
027
028 template<>
029 struct Reference<int>{
030 typedef int Result;
031 };
032
033 template<>
034 struct Reference<long>{
035 typedef long Result;
036 };
037
038 }
039
040 template <class TChar>
041 struct ordinaryTCharID{ //对char、short、int、long适用
042 static const unsigned int
043 Digit=0x1,Operator=0x10,sOperator=0x100,
044 preBrackets=0x1000,postBrackets=0x10000;
045 //数字、运算、一元运算、前括号、后括号
046 static const TChar dot=',',parenthesis='(',negative='-',_negative='~';
047 //分隔符、括号、负号、代负号
048 static unsigned int
049 classify(typename Reference<TChar>::Result ch){ //分类
050 if(isdigit(ch) || ch=='.') return Digit;
051 if(isoperator(ch)) return Operator;
052 if(ch=='!' || ch=='~') return sOperator;
053 if(ch=='(' || ch=='[' || ch=='{')
054 return preBrackets;
055 if(ch==')' || ch==']' || ch=='}')
056 return postBrackets;
057 return 0;
058 }
059
060 static unsigned int
061 priority(typename Reference<TChar>::Result ch){ //优先级,也即出栈顺序
062 switch(ch)
063 {
064 case '(':
065 return 0;
066 case '+': case '-': case '~':
067 return 1;
068 case '*': case '/':
069 return 2;
070 case '^':
071 return 3;
072 case '!':
073 return 4;
074 }
075 }
076 };
077
078 template <class Opt>
079 struct ReturnType{ //得到返回类型,函数对象
080 typedef typename Opt::RtnType Result;
081 };
082
083 //偏特化
084 template <class RtnType,class TChar>
085 struct ReturnType
086 <RtnType(*)(RtnType,RtnType,TChar)> //函数指针1:二元
087 {
088 typedef RtnType Result;
089 };
090
091 template <class RtnType,class TChar>
092 struct ReturnType< //函数指针2:二元
093 RtnType(*)(const RtnType&,const RtnType&,const TChar&)
094 >{
095 typedef RtnType Result;
096 };
097
098 template <class RtnType,class TChar>
099 struct ReturnType
100 <RtnType(*)(RtnType,TChar)> //函数指针1:一元
101 {
102 typedef RtnType Result;
103 };
104
105 template <class RtnType,class TChar>
106 struct ReturnType< //函数指针2:一元
107 RtnType(*)(const RtnType&,const TChar&)
108 >{
109 typedef RtnType Result;
110 };
111
112 template<class Opt1,class Opt2>
113 struct OPT{ //捆绑函数类
114 public:
115 typedef typename
116 ReturnType<Opt1>::Result RtnType;
117 OPT(Opt1 _opt1,Opt2 _opt2):opt1(_opt1),opt2(_opt2)
118 {}
119 RtnType operator()(RtnType x1,char opt){
120 return opt1(x1,opt);
121 }
122 RtnType operator()(RtnType x1,RtnType x2,char opt){
123 return opt2(x1,x2,opt);
124 }
125 private:
126 Opt1 opt1;
127 Opt2 opt2;
128 };
129
130 #endif
002 #define CHAR_IDENTIFICATION_H
003
004 #include <cctype>
005
006 namespace {
007
008 inline bool isoperator(int ch){
009 return ch=='+' || ch=='-' || ch=='*' ||
010 ch=='/' || ch=='^';
011 }
012
013 template<class TChar>
014 struct Reference{
015 typedef const TChar& Result;
016 };
017
018 template<>
019 struct Reference<char>{
020 typedef char Result;
021 };
022
023 template<>
024 struct Reference<short>{
025 typedef short Result;
026 };
027
028 template<>
029 struct Reference<int>{
030 typedef int Result;
031 };
032
033 template<>
034 struct Reference<long>{
035 typedef long Result;
036 };
037
038 }
039
040 template <class TChar>
041 struct ordinaryTCharID{ //对char、short、int、long适用
042 static const unsigned int
043 Digit=0x1,Operator=0x10,sOperator=0x100,
044 preBrackets=0x1000,postBrackets=0x10000;
045 //数字、运算、一元运算、前括号、后括号
046 static const TChar dot=',',parenthesis='(',negative='-',_negative='~';
047 //分隔符、括号、负号、代负号
048 static unsigned int
049 classify(typename Reference<TChar>::Result ch){ //分类
050 if(isdigit(ch) || ch=='.') return Digit;
051 if(isoperator(ch)) return Operator;
052 if(ch=='!' || ch=='~') return sOperator;
053 if(ch=='(' || ch=='[' || ch=='{')
054 return preBrackets;
055 if(ch==')' || ch==']' || ch=='}')
056 return postBrackets;
057 return 0;
058 }
059
060 static unsigned int
061 priority(typename Reference<TChar>::Result ch){ //优先级,也即出栈顺序
062 switch(ch)
063 {
064 case '(':
065 return 0;
066 case '+': case '-': case '~':
067 return 1;
068 case '*': case '/':
069 return 2;
070 case '^':
071 return 3;
072 case '!':
073 return 4;
074 }
075 }
076 };
077
078 template <class Opt>
079 struct ReturnType{ //得到返回类型,函数对象
080 typedef typename Opt::RtnType Result;
081 };
082
083 //偏特化
084 template <class RtnType,class TChar>
085 struct ReturnType
086 <RtnType(*)(RtnType,RtnType,TChar)> //函数指针1:二元
087 {
088 typedef RtnType Result;
089 };
090
091 template <class RtnType,class TChar>
092 struct ReturnType< //函数指针2:二元
093 RtnType(*)(const RtnType&,const RtnType&,const TChar&)
094 >{
095 typedef RtnType Result;
096 };
097
098 template <class RtnType,class TChar>
099 struct ReturnType
100 <RtnType(*)(RtnType,TChar)> //函数指针1:一元
101 {
102 typedef RtnType Result;
103 };
104
105 template <class RtnType,class TChar>
106 struct ReturnType< //函数指针2:一元
107 RtnType(*)(const RtnType&,const TChar&)
108 >{
109 typedef RtnType Result;
110 };
111
112 template<class Opt1,class Opt2>
113 struct OPT{ //捆绑函数类
114 public:
115 typedef typename
116 ReturnType<Opt1>::Result RtnType;
117 OPT(Opt1 _opt1,Opt2 _opt2):opt1(_opt1),opt2(_opt2)
118 {}
119 RtnType operator()(RtnType x1,char opt){
120 return opt1(x1,opt);
121 }
122 RtnType operator()(RtnType x1,RtnType x2,char opt){
123 return opt2(x1,x2,opt);
124 }
125 private:
126 Opt1 opt1;
127 Opt2 opt2;
128 };
129
130 #endif
所有评论,共1条:( 我也来说两句)
| 1 |
半瓶墨水
1年前
回复
这一系列,不会是同一个程序的不同文件吧
|
代码
