#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
相关推荐
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语言实现的模拟计算。假设小车是匀速运动,运动中行程和速度均会有误差,误差符合正态分布,测量值也有误差符合正态分布,使用卡尔曼获得最优解
C语言程序设计.pdf http://ishare.iask.sina.com.cn/f/19668052.html C语言教程讲义(谭浩强).pdf http://ishare.iask.sina.com.cn/f/23827898.html C语言程序设计(第三版)-谭浩强[开始免费了].ppt ...
c语言经典例题可以看看。其它凑字数第一章 答案:https://blog.csdn.net/qq_43615815/article/details/104092557 第二章 答案:https://blog.csdn.net/qq_43615815/article/details/104302937 第三章 答案:...
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) ...
经典C程序100例,初学者学习C语言的好东东
为了更好地掌握各数据类型、数据结构的使用以及C语言程序设计的思想,以C语言平日课堂知识为主,复刻了前几年在移动端很火的一款游戏Flappy bird。 详细介绍参考:...
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) ...
资源包含文件:任务书+设计说明书+需求分析说明书+项目总结报告+源码及可执行exe文件 VC++6.0或以上版本; 操作系统:Windows7及以上。 目标是在VS下,使用Win32网络编程和MFC框架实现多人在线聊天的服务器端和...
有关c语言的一些程序设计,◆经典C源程序100例:http://post.baidu.com/f?kz=8618367 ◆时钟的驻留程序:http://post.baidu.com/f?kz=10822377 ◆数据结构暨若干经典问题和算法:...
源代码在这里:http://download.csdn.net/detail/jingwei12345/3762336
C语言实现SHA-224/SHA-256/SHA-384/SHA-512摘要算法。编译环境:VS2010。请参考我的博客: SHA-224:https://blog.csdn.net/u013073067/article/details/86605223 SHA-256:...
适合初学C语言C++的小白,帮助你快速理解这两种语言的基础知识。内含练习项目哦。
需要使用c语言实现,因此学习如何将excel表格信息导入程序中进行处理 ## 简单演示 运行可执行文件demo.exe会将相同文件夹中的grade.csv文件打印 ## 结构 源代码为: main.cpp readFile.cpp readFile.h ## 相关文章 ...
计算机C语言程序设计考试附带答案 本文链接:https://blog.csdn.net/Candy5204/article/de 现需要设计一个处理二维平面整型数据的程序,平面的行数和列数不可预知(需要在程序运行过程中输入)。要求处理的方式是其...
习题答案:http://download.csdn.net/source/2419155 *************************************************************** C语言经典之作,被誉为“近10年来最好的一部C语言著作” 哈佛、麻省理工、斯坦福、加州...
使用51单片机制作的心形流水灯,详情可以参考 https://blog.csdn.net/qq_34802028/article/details/111825417
基于Objective-C语言实现 大部分Cocoa Touch的功能是用Objective-C实现的,速度很快。采用了真正的动态运行时系统,从而增添了灵活性。由于Objective-C是C的超集,因而可以很容易的将C甚至C++代码添加到Cocoa Touc