全国计算机二级考试C++教程1.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
coutendl; pCur=pJose + n –1; //指向数组最后一个元素 } ? //数m个小孩 void count(int m) { for(int i=0; im; i++) { pivot = pCur; pCur = pivot-next; } }? //处理未获胜小孩 void process() { int linecount=0; for(int i=1; in; i++) { count(m); //数小孩 if (( linecount++ % 10 ) = = 0) coutendl; coutsetw (4)pCur -code; ? pivot -next=pCur -next; //删除小孩 pCur = pivot; } } 面向对象方法 抽象和分类: 大致确定类的属性(数据成员) 和方法(函数成员) ? Josephus类 属性: Boynumber, BeginPos, Interval 方法:Initial, Getwinner Ring类 属性:First, Pivot, Current 方法:Clear, Print, Count //主函数 定义一个Jose类对象 赋初值initial 求获胜者getwinner ? 面向对象程序设计使用户不需要懂计算机太多,也不需要懂业务太多 ? 面向对象程序的实现 ?文件: ring.h ,jose.h ring.cpp, jose.cpp, jose4.cpp /*ring.h开始*/ ? struct Boy //小孩结构作为链表结点 { int code; Boy *next; }; class Ring //环链表类定义 { public: Ring(int n); void Count(int m); //数小孩 void PutBoy(); //输出当前小孩编号 void ClearBoy(); //删除小孩 ~Ring(); protected: Boy * pBegin; Boy * pivot Boy * pCurrent; }; /*ring.h结束*/ /*ring.cpp开始*/ ? #include iostream.h #include iomanip.h #include “ring.h” ? Ring::Ring(int n) { pBegin = new Boy[n]; pCurrent = pBegin; for(int i=1; in; i++, pCurrent = pCurrent -next) { pCurrent - next = pBegin + i % n;   //将结点链起来 pCurrent - code =i; //小孩编号 PutBoy(); } coutendl; pCurrent = pBegin[n - 1]; //当前小孩位置在最后一个编号 } // Ring::Ring(int n) void Ring ::Count(int m)   //数小孩 { for(int i=0; im; i++) { pivot = pCurrent; pCurrent = pivot - next; } } void Ring::PutBoy() //打印小孩 { static int numInLine=0; if (numInLine ++ % 10 ==0) cout endl; coutsetw(4)pCurrent-code; } void Ring::ClearBoy() //删除小孩 { pivot - next=pCurrent - next; pCurrent = pivot; } Ring::~Ring() //析构 { delete []pBegin; } /* ring.cpp结束*/ /*jose.h 开始*/ class Jose { public: Jo

文档评论(0)

1112111 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档