BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #74010同步于 2013/9/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

看看C++的魅力吧

fentoyal
2013/9/28镜像同步58 回复
C++模板是图灵完备的,以下这个例子是昨天周末晚随性写的,本身并没什么实际价值,也远不足以证明C++模板的能力,但希望通过这个例子,可稍微展示下C++的魅力: 编译时间将一个罗马数字字符串转为整数。这个过程全部由编译器完成,出来的结果也是编译常量,你可以用它来声明静态数组大小,没任何问题。本身很鲁棒,对一个非法的输入(含非法字符的)将导致编译错误,对合法的字母但非法的组合(比如IIII)将输出0. (注,罗马数字本身计数能力范围是1-3999) PS:需要C++11支持 #include <iostream> class R2I { const static int DIG; public: constexpr static char IN [] = "MMMDCCXCIV"; //Input goes here const static int OUT; }; //// template <char C> struct ID {}; template <> struct ID <'I'> {static const int I = 0; static const int V = 1;}; template <> struct ID <'V'> {static const int I = 1; static const int V = 5;}; template <> struct ID <'X'> {static const int I = 2; static const int V = 10;}; template <> struct ID <'L'> {static const int I = 3; static const int V = 50;}; template <> struct ID <'C'> {static const int I = 4; static const int V = 100;}; template <> struct ID <'D'> {static const int I = 5; static const int V = 500;}; template <> struct ID <'M'> {static const int I = 6; static const int V = 1000;}; template <bool A, bool B, int I0, int I1> struct DiffHelper{ static const int D = 9;}; template<int I0, int I1> struct DiffHelper <true, true, I0, I1>{ static const int VAL0 = ID<R2I::IN[I0]>::I; static const int VAL1 = ID<R2I::IN[I1]>::I; static const int D = (VAL0 - VAL1 <= -3) ? -3 : (( VAL0 - VAL1 >= 1) ? 1 : (VAL0 - VAL1)); }; template <int I0, int I1> struct Diff {static const int D = DiffHelper<(I0 < sizeof(R2I::IN) - 1), (I1 < sizeof(R2I::IN) - 1), I0, I1>::D;}; //// template <int N, int C, int P> struct R2IHelper {const static int DIG = -4000;}; template <int P, int N> struct R2IHelper<N, 9, P> {const static int DIG = ID<R2I::IN[P]>::V;}; template <int P> struct R2IHelper<9, 1, P> {const static int DIG = ID<R2I::IN[P]>::V * 4/5 + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I) % 2, Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<9, 9, P> {const static int DIG = ID<R2I::IN[P]>::V * 4/5;}; template <int P> struct R2IHelper<6, 1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I) % 2, Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<6, 0, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<2, Diff<P+1, P+2>::D, P+1>::DIG;}; template <int P> struct R2IHelper<4, 1, P> {const static int DIG = ID<R2I::IN[P]>::V * 3/5 + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I + 1) % 2, Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<4, 9, P> {const static int DIG = ID<R2I::IN[P]>::V * 3/5;}; template <int P> struct R2IHelper<3, 1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I) % 2 , Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<2, 1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I) % 2 , Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<2, 0, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<3, Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<1, 1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I==1)*6 + (ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I + 1) % 2, Diff<P+1, P+2>::D, P+1>::DIG;}; template <int P> struct R2IHelper<0, 1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<(ID<R2I::IN[P]>::I - ID<R2I::IN[P+1]>::I) % 2 , Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<0, 0, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<2, Diff<P+1, P+2>::D, P+1>::DIG; }; template <int P> struct R2IHelper<0, -1, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<4, Diff<P, P+2>::D, P+1>::DIG;}; template <int P> struct R2IHelper<0, -2, P> {const static int DIG = ID<R2I::IN[P]>::V + R2IHelper<9, Diff<P, P+2>::D, P+1>::DIG;}; //// const int R2I::DIG = R2IHelper<ID<R2I::IN[0]>::I % 2 , Diff<0, 1>::D, 0>::DIG; const int R2I::OUT = DIG > 0 ? DIG : 0; using namespace std; int main() { char carr[R2I::OUT]; cout<<sizeof(carr)<<endl; }
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
gsl2011机器人#1 · 2013/9/29
太长没看 个人还是很喜欢c++滴
tonyjansan机器人#2 · 2013/9/29
这种代码有可读性可言吗?你还真是将C++最糟糕的一面展现得淋漓尽致~ 不能共事的人终将被社会淘汰,不能让他人高效合作开发的代码都该去见图灵! 【 在 fentoyal 的大作中提到: 】 : C++模板是图灵完备的,以下这个例子是昨天周末晚随性写的,本身并没什么实际价值,也远不足以证明C++模板的能力,但希望通过这个例子,可稍微展示下C++的魅力: : 编译时间将一个罗马数字字符串转为整数。这个过程全部由编译器完成,出来的结果也是编译常量,你可以用它来声明静态数组大小,没任何问题。本身很鲁棒,对一个非法的输入(含非法字符的)将导致编译错误,对合法的字母但非法的组合(比如IIII)将输出0. (注,罗马数字本身计数能力范围是1-3999) : PS:需要C++11支持 : ...................
gaoweiwei机器人#3 · 2013/9/29
ls激动了,每年还举行c语言混乱代码大赛呢,总不能让参赛者都去见图灵吧
fentoyal机器人#4 · 2013/9/30
【 在 tonyjansan 的大作中提到: 】 : 这种代码有可读性可言吗?你还真是将C++最糟糕的一面展现得淋漓尽致~ : 不能共事的人终将被社会淘汰,不能让他人高效合作开发的代码都该去见图灵! : 我只是展示下C++的能力,我没写注释因为这本来就是随便写的玩的东西,又不是要release的产品。你说这是C++最糟糕的一面就搞笑了,你读过boost的代码么?我这段代码基本用两倍长度的注释就能注释的清清楚楚,boost的估计得10倍吧。所以boost就是C++最糟糕的代码了? 当然看不懂这个代码也因为你还不到火候,其实这段代码并不难理解,和我公事的人我是不会让他们看的,因为他们会一上来给我挑出N个可以优化的地方,我还要解释这只是写了个玩具玩。所以你不必担心和我公事的人,他们阅读这种程度的代码不会有任何问题的。
zhaoyu1999机器人#5 · 2013/9/30
所谓程序员相轻啊 谁都看不上谁
fentoyal机器人#6 · 2013/9/30
【 在 zhaoyu1999 的大作中提到: 】 : 所谓程序员相轻啊 : 谁都看不上谁 你想多了,不过是个论坛帖子。
mengzi2008机器人#7 · 2013/9/30
这是魅力吗。。让人远离c++还差不多
paper777机器人#8 · 2013/9/30
那你所认知的魅力是指什么呢? 【 在 mengzi2008 的大作中提到: 】 : 这是魅力吗。。让人远离c++还差不多
ForAlice机器人#9 · 2013/9/30
评论总是比正文更有魅力。。