05.C语言和设计模式(解释器模式)
来源:互联网 发布:鱼跃龙门网络歌手 编辑:程序博客网 时间:2024/05/16 17:13
原文地址:http://blog.csdn.net/feixiaoxing/article/details/7182466
解释器模式虽然听上去有些费解,但是如果用示例说明一下就不难理解了。我们知道在C语言中,关于变量的定义是这样的:一个不以数字开始的由字母、数字和下划线构成的字符串。这种形式的表达式可以用状态自动机解决,当然也可以用解释器的方式解决。
- typedef struct _Interpret
- {
- int type;
- void* (*process)(void* pData, int* type, int* result);
- }Interpret;
- #define DIGITAL_TYPE 1
- #define LETTER_TYPE 2
- #define BOTTOM_LINE 3
- void* digital_process(void* pData, int* type, int* result)
- {
- UINT8* str;
- assert(NULL != pData && NULL != type && NULL != result);
- str = (UNT8*)pData;
- while (*str >= '0' && *str <= '9')
- {
- str ++;
- }
- if(*str == '\0')
- {
- *result = TRUE;
- return NULL;
- }
- if(*str == '_')
- {
- *result = TRUE;
- *type = BOTTOM_TYPE;
- return str;
- }
- if(*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z')
- {
- *result = TRUE;
- *type = LETTER_TYPE;
- return str;
- }
- *result = FALSE;
- return NULL;
- }
- void* letter_process(void* pData, int* type, int* result)
- {
- UINT8* str;
- assert(NULL != pData && NULL != type && NULL != result);
- str = (UNT8*)pData;
- while (*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z')
- {
- str ++;
- }
- if(*str == '\0')
- {
- *result = TRUE;
- return NULL;
- }
- if(*str == '_')
- {
- *result = TRUE;
- *type = BOTTOM_TYPE;
- return str;
- }
- if(*str >= '0' && *str <= '9')
- {
- *result = TRUE;
- *type = DIGITAL_TYPE;
- return str;
- }
- *result = FALSE;
- return NULL;
- }
- void* bottom_process(void* pData, int* type, int* result)
- {
- UINT8* str;
- assert(NULL != pData && NULL != type && NULL != result);
- str = (UNT8*)pData;
- while ('_' == *str )
- {
- str ++;
- }
- if(*str == '\0')
- {
- *result = TRUE;
- return NULL;
- }
- if(*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z')
- {
- *result = TRUE;
- *type = LETTER_TYPE;
- return str;
- }
- if(*str >= '0' && *str <= '9')
- {
- *result = TRUE;
- *type = DIGITAL_TYPE;
- return str;
- }
- *result = FALSE;
- return NULL;
- }
0 0
- 05.C语言和设计模式(解释器模式)
- C语言和设计模式(解释器模式)
- C语言和设计模式(解释器模式)
- C语言和设计模式(解释器模式)
- 12、C语言和设计模式(解释器模式)
- C语言设计模式:解释器 //tbd
- C语言和设计模式(解释器模式)【文章都是来自网络,只要自己吸取了就是自己的】 .
- C语言和设计模式
- C语言和设计模式
- C语言和设计模式
- C语言和设计模式(之单件模式)
- C语言和设计模式(之原型模式)
- C语言和设计模式(之组合模式)
- C语言和设计模式(之模板模式)
- C语言和设计模式(之原型模式)
- C语言和设计模式(工厂模式)
- C语言和设计模式(责任链模式)
- C语言和设计模式(抽象工厂模式)
- 04.C语言和设计模式(命令模式)
- android 签名key总结:
- 创建IRP实现驱动之间通讯
- 微博CacheService架构浅析
- windows远程桌面访问ubuntu12.04 之一 vnc
- 05.C语言和设计模式(解释器模式)
- Struts2框架提供的结果类型
- JS新窗口与父窗口的交互
- SecureCRT 常用技巧 & Bash快捷键
- ROI 脚本
- ZigBee技术
- hash join 驱动表和被驱动表不一定是全表扫描
- tftp cannot open,Text file busy
- HTML 中的meta元素小结