#include <stdio.h> #include <stdlib.h> #include <string.h> #ifndef VIRTUAL #define VIRTUAL #endif #ifndef DELETE #define DELETE(X) do { free(X);X = NULL; } while(0) #endif #define NEW(TYPE,pInstance,SUBTYPE) struct TYPE* pInstance = NULL; \ struct SUBTYPE* SUBTYPE##instance = (struct SUBTYPE*)malloc(sizeof(struct SUBTYPE)); \ SUBTYPE##instance->SUBTYPE##_constructor = SUBTYPE##_constructor; \ SUBTYPE##instance->SUBTYPE##_constructor(SUBTYPE##instance);pInstance = (struct TYPE*)(SUBTYPE##instance); struct State* getProphaseState(void); struct State* getMetaphaseState(void); struct State* getAnaphaseState(void); struct State* getEndState(void); struct War { struct State* m_pState; int m_nDays; void (*War_constructor)(struct War* /* pThis */); void (*War_destructor)(struct War* /* pThis */); int (*War_getDays)(struct War* /* pThis */); void (*War_setDays)(struct War* /* pThis */,int /* nDays */); void (*War_setState)(struct War* /* pThis */,struct State* /* pState */); void (*War_exeState)(struct War* /* pThis */); }; struct State /* base class */ { VIRTUAL void (*Action)(struct State* /* pThis */,struct War* /* pWar */); }; struct EndState /* sub class */ { struct State m_nParent; void (*EndState_constructor)(struct EndState* /* pThis */); }; struct AnaphaseState /* sub class */ { struct State m_nParent; void (*AnaphaseState_constructor)(struct AnaphaseState* /* pThis */); }; struct MetaphaseState /* sub class */ { struct State m_nParent; void (*MetaphaseState_constructor)(struct MetaphaseState* /* pThis */); }; struct ProphaseState { struct State m_nParent; void (*ProphaseState_constructor)(struct ProphaseState* /* pThis */); }; void EndState_Action(struct State* pThis,struct War* pWar) { printf("444\n"); } void AnaphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 30) { printf("333 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getEndState()); pWar->War_exeState(pWar); } } void MetaphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 20) { printf("222 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getAnaphaseState()); pWar->War_exeState(pWar); } } void ProphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 10) { printf("111 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getMetaphaseState()); pWar->War_exeState(pWar); } } void EndState_constructor(struct EndState* pThis) { pThis->m_nParent.Action = EndState_Action; } void AnaphaseState_constructor(struct AnaphaseState* pThis) { pThis->m_nParent.Action = AnaphaseState_Action; } void MetaphaseState_constructor(struct MetaphaseState* pThis) { pThis->m_nParent.Action = MetaphaseState_Action; } void ProphaseState_constructor(struct ProphaseState* pThis) { pThis->m_nParent.Action = ProphaseState_Action; } /* functions releated with the class War */ void War_destructor(struct War* pThis) { if(pThis->m_pState) { DELETE(pThis->m_pState); } } int War_getDays(struct War* pThis) { return pThis->m_nDays; } void War_setDays(struct War* pThis,int nDays) { pThis->m_nDays = nDays; } void War_setState(struct War* pThis,struct State* pState) { if(pThis->m_pState) { printf("delete %p\n",pThis->m_pState); DELETE(pThis->m_pState); } pThis->m_pState = pState; } void War_exeState(struct War* pThis) { pThis->m_pState->Action(pThis->m_pState,pThis); } void War_constructor(struct War* pThis) { pThis->m_pState = NULL; pThis->m_nDays = 0; pThis->War_destructor = War_destructor; pThis->War_getDays = War_getDays; pThis->War_setDays = War_setDays; pThis->War_setState = War_setState; pThis->War_exeState = War_exeState; } /* get instance */ struct State* getProphaseState(void) { NEW(State,pState,ProphaseState); printf("new %p\n",pState); return pState; } struct State* getMetaphaseState(void) { NEW(State,pState,MetaphaseState); printf("new %p\n",pState); return pState; } struct State* getAnaphaseState(void) { NEW(State,pState,AnaphaseState); printf("new %p\n",pState); return pState; } struct State* getEndState(void) { NEW(State,pState,EndState); printf("new %p\n",pState); return pState; } /* test */ int main(void) { NEW(War,pWar,War); printf("init\n"); pWar->War_setDays(pWar,0); pWar->War_setState(pWar,getProphaseState()); for(int i = 1 ; i < 40 ; i += 1) { pWar->War_setDays(pWar,i); pWar->War_exeState(pWar); } pWar->War_destructor(pWar); DELETE(pWar); system("pause"); return 0; }
文章来源:http://www.itnose.net/detail/6038660.html
更多文章:http://www.itnose.net/type/61.html
相关推荐
资源包含文件:课程论文word+源码 本次课程设计是构造一个高级语言的子集的编译器,目标代码可以是汇编语言也可以是其他形式的机器语言。详细介绍参考:https://biyezuopin.blog.csdn.net/article/details/126084323
C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) ...
文件中为百度网盘下载地址 谷歌经常被墙,有的...http://download.csdn.net/detail/holle_word/7571419 http://download.csdn.net/detail/holle_word/7164787 http://download.csdn.net/detail/holle_word/5916313 ...
该代码是卡尔曼滤波学习(7)http://t.csdnimg.cn/4ubGO使用C语言实现的模拟计算。假设小车是匀速运动,运动中行程和速度均会有误差,误差符合正态分布,测量值也有误差符合正态分布,使用卡尔曼获得最优解
资源包含文件:设计报告word+项目源码及可执行exe文件 本项目的开发及运行环境要求: 操作系统:windows 7, windows 10 开发工具:Code Blocks 17.12, Visual Studio Code 开发语言:C 语言 在本次课程设计中,操作...
7. **我要自学网**:[http://www.51zxw.net/bbs/index.asp?boardid=60](http://www.51zxw.net/bbs/index.asp?boardid=60) 提供多种技能的自学资源,C语言版块有详细的视频教程和学习路径。 8. **电大在线论坛**:...
《经典C程序100例》是一本专为C语言初学者设计的实践教程,它包含了一百个精心挑选的C语言编程实例,旨在帮助学习者深入理解和掌握C语言的基本概念、语法以及编程技巧。CHM格式的文档是Windows系统的帮助文件格式,...
c语言经典例题可以看看。其它凑字数第一章 答案:https://blog.csdn.net/qq_43615815/article/details/104092557 第二章 答案:https://blog.csdn.net/qq_43615815/article/details/104302937 第三章 答案:...
使用高级程序语言作为实现语言,实现一个类 C 语言的编译器。编码实现编译器的组成部分。 要求的类 C 编译器是个一遍的编译程序,词法分析程序作为子程序,需要的时候被语法分析程序调用; 使用语法制导的翻译技术,...
https://blog.csdn.net/ida0918/article/detail https://www.zhihu.com/question sql注入https://redtige web:http://hackin 综合:http://www.wechal 综合性黑客game:http://www. 综合性新平台CTF:...
C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) ...
- **网址**:http://www.netyi.net - **简介**:该网站提供了一系列关于C语言的基础知识介绍以及编程技巧,适合编程新手入门。 - **特色**:图文并茂的教学方式使得理论知识更加易于理解,同时还提供了丰富的代码...
C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) ...
- `http://www.yesky.com/72339069014638592/index.shtml` 和 `http://www.itebook.net/download.asp` 包含网络技术和设计软件的学习资源,适合网络管理员和设计师学习新技术。 8. 社区与问答平台: - `...
为了更好地掌握各数据类型、数据结构的使用以及C语言程序设计的思想,以C语言平日课堂知识为主,复刻了前几年在移动端很火的一款游戏Flappy bird。 详细介绍参考:...
C语言实现SHA-224/SHA-256/SHA-384/SHA-512摘要算法。编译环境:VS2010。请参考我的博客: SHA-224:https://blog.csdn.net/u013073067/article/details/86605223 SHA-256:...
有关c语言的一些程序设计,◆经典C源程序100例:http://post.baidu.com/f?kz=8618367 ◆时钟的驻留程序:http://post.baidu.com/f?kz=10822377 ◆数据结构暨若干经典问题和算法:...
资源包含包含:课程设计报告word+答辩PPT+项目源码 我们使用了C和Go两种语言来实现了端口扫描工具,分别由两名组员完成。每种语言分别实现了TCP-connect、SYN、FIN、UDP这四种扫描方式。 为了提高扫描速度,我们分别...
配套文章解析:https://blog.csdn.net/u013469753/article/details/108322140 AES加密算法,AES的全称是Advanced Encryption Standard,意思是高级加密标准。它的出现主要是为了取代DES加密算法的,因为我们都知道...
1. **CSDN.NET**:[http://www.csdn.net/develop/list_article.asp?bigclassid=1](http://www.csdn.net/develop/list_article.asp?bigclassid=1) - CSDN是中国最大的开发者技术社区之一,提供了大量的VC相关的文章...