﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-HonKer-随笔分类-c/c++</title><link>http://www.cppblog.com/honker/category/4167.html</link><description>一个苹果,n种思想</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 07:45:47 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 07:45:47 GMT</pubDate><ttl>60</ttl><item><title>数据结构中栈的应用--迷宫问题</title><link>http://www.cppblog.com/honker/archive/2007/04/28/23147.html</link><dc:creator>honker</dc:creator><author>honker</author><pubDate>Sat, 28 Apr 2007 15:22:00 GMT</pubDate><guid>http://www.cppblog.com/honker/archive/2007/04/28/23147.html</guid><wfw:comment>http://www.cppblog.com/honker/comments/23147.html</wfw:comment><comments>http://www.cppblog.com/honker/archive/2007/04/28/23147.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/honker/comments/commentRss/23147.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/honker/services/trackbacks/23147.html</trackback:ping><description><![CDATA[<span style="color: #1805a0;">&nbsp;&nbsp;&nbsp; 迷宫问题可以说是很经典的问题,也算是栈的一个应用吧,在谈到这道题目时,我首先想到的是迷宫存储问题,我将他初始化成一个数组,为了模拟迷,我将1代表不可走,0代表可走.</span><img style="color: #1805a0;" src="http://www.cppblog.com/CuteSoft_Client/CuteEditor/images/face1.gif" align="absmiddle" border="0"><span style="color: #1805a0;">然后定义迷宫类型,当让包括方向dir,行row,列col.而dir则包括了4个方向:即东南西北,当然也可以设置成8个方向!</span><br style="color: #1805a0;"><span style="color: #1805a0;">&nbsp;&nbsp;&nbsp;  写好了这些,就开始写迷宫算法吧!代码我都加上了注释,以下程序均通过gcc4.1版本测试,运行结果及代码如下:<br><br><img  src="http://www.cppblog.com/images/cppblog_com/honker/Screenshot1254.png" border="0"><br>#include&lt;stdio.h&gt;</span><span style="color: #1805a0;"></span><span style="color: #1805a0;">
<br>#include&lt;stdbool.h&gt;
<br>#define MAX_STACK_SIZE 100
<br>#define EXIT_ROW 9
<br>#define EXIT_COL 9
<br>&nbsp;<br>&nbsp;<br>int maze[10][10] = {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,1,1,1,1,1,1,1,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,0,1,0,0,0,1,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,0,1,0,0,0,1,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,0,0,0,1,1,0,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,1,1,1,0,0,0,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,0,0,1,0,0,0,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,1,0,0,0,1,0,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,0,1,1,1,1,1,1,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,1,0,0,0,0,0,0,0,1},
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {1,1,1,1,1,1,1,1,0,1}};/*初始化迷宫*/
<br>int mark[10][10] = {0};/*初始化标志位,0代表没走过,1代表走过*/
<br>&nbsp;<br>/*方向*/
<br>typedef struct{
<br>&nbsp;&nbsp;&nbsp; short int vert;
<br>&nbsp;&nbsp;&nbsp; short int horiz;
<br>}offsets;
<br>&nbsp;<br>offsets move[4] = {{0,1},{1,0},{0,-1},{-1,0}};/*北,东,南,西*/
<br>&nbsp;<br>&nbsp;<br>/*迷宫类型*/
<br>typedef struct {
<br>&nbsp;&nbsp;&nbsp; short int row;
<br>&nbsp;&nbsp;&nbsp; short int col;
<br>&nbsp;&nbsp;&nbsp; short int dir;
<br>}element;
<br>&nbsp;<br>element stack[MAX_STACK_SIZE];
<br>&nbsp;<br>&nbsp;<br>void path(void);
<br>element delete(int* top);
<br>void add(int* top,element item);
<br>&nbsp;<br>&nbsp;<br>element delete(int* top)/*出栈,top指向栈顶元素*/
<br>{
<br>&nbsp;&nbsp;&nbsp; if(*top == -1)
<br>&nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("stack is empty!\n");
<br>&nbsp;&nbsp;&nbsp; }
<br>&nbsp;&nbsp;&nbsp; return stack[(*top)--];
<br>}
<br>&nbsp;<br>&nbsp;<br>void add(int* top,element item)/*入栈*/
<br>{
<br>&nbsp;&nbsp;&nbsp; if(*top &gt;= MAX_STACK_SIZE - 1)
<br>&nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("stack is full!\n");
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;
<br>&nbsp;&nbsp;&nbsp; }
<br>&nbsp;&nbsp;&nbsp; stack[++*top] = item;
<br>}
<br>&nbsp;<br>&nbsp;<br>void path(void)/*迷宫函数*/
<br>{
<br>&nbsp;&nbsp;&nbsp; element position;
<br>&nbsp;&nbsp;&nbsp; int i,row,col,next_row,next_col,dir,top;
<br>&nbsp;&nbsp;&nbsp; _Bool found = 0;
<br>&nbsp;&nbsp;&nbsp; mark[1][1] = 1,top = 0;/*初始化标志数组元素以及栈*/
<br>&nbsp;&nbsp;&nbsp; stack[0].row = 1,stack[0].col = 1,stack[0].dir = 0;
<br>&nbsp;&nbsp;&nbsp; while(top &gt; -1 &amp;&amp; !found)
<br>&nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position = delete(&amp;top);&nbsp;&nbsp;&nbsp; /*将栈顶元素取出,*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; row = position.row;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /*利用中间变量row,col,dir等候判断*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; col = position.col;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dir = position.dir;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while(dir &lt; 4 &amp;&amp; !found)
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; next_row = row + move[dir].vert;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; next_col = col + move[dir].horiz;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(next_row == EXIT_ROW &amp;&amp; next_col == EXIT_COL)
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; found = 1;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(!maze[next_row][next_col] &amp;&amp; !mark[next_row][next_col])/*判断下一步可走并且没走过,则入栈*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mark[next_row][next_col] = 1;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position.row = row;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position.col = col;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; position.dir = ++dir;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; add(&amp;top,position);/*合理则入栈*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; row = next_row;/*继续向下走*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; col = next_col;dir = 0;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dir++;/*dir&lt;4时,改变方向*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(found)/*判断是否有出口*/
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("the path is:\n");
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("row col\n");
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(i = 0;i &lt;= top;++i)
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("%2d%5d",stack[i].row,stack[i].col);
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("%2d%5d\n",row,col);
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("%2d%5d\n",EXIT_ROW,EXIT_COL);
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;
<br>&nbsp;&nbsp;&nbsp; }
<br>}
<br>&nbsp;<br>&nbsp;<br>int main(void)
<br>{&nbsp;&nbsp;
<br>&nbsp;&nbsp;&nbsp; path();
<br>&nbsp;&nbsp;&nbsp; return 0;
<br>}
<br><br>注释:参见Fundamentals of Data STRUCTURES In C 中文版数据结构(C语言版)<br style="color: #1805a0; font-weight: bold;"></span><br><img src ="http://www.cppblog.com/honker/aggbug/23147.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/honker/" target="_blank">honker</a> 2007-04-28 23:22 <a href="http://www.cppblog.com/honker/archive/2007/04/28/23147.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++课程设计</title><link>http://www.cppblog.com/honker/archive/2007/04/28/23134.html</link><dc:creator>honker</dc:creator><author>honker</author><pubDate>Sat, 28 Apr 2007 12:18:00 GMT</pubDate><guid>http://www.cppblog.com/honker/archive/2007/04/28/23134.html</guid><wfw:comment>http://www.cppblog.com/honker/comments/23134.html</wfw:comment><comments>http://www.cppblog.com/honker/archive/2007/04/28/23134.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/honker/comments/commentRss/23134.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/honker/services/trackbacks/23134.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp; 前些阵子做了C++课程设计,在此将程序源码公布,以便同学之间交流<img src="http://www.cppblog.com/CuteSoft_Client/CuteEditor/images/emsmile.gif" align="absmiddle" border="0">.<span style="color: #1a6ad2;">题目如下:</span><br>1.编写函数bool syntaxCheck(const char* s),功能是检查字符串s中的{和}、<br>[和]、(和)是否匹配,即是否符合C语言的语法要求。<br>2.(5分) 不使用库函数,实现函数char*strStr(char* s, char* t),功能是在s<br>中查找t的第一次出现。若出现,返回出现的位置,否则返回0。<br>3. (5分)编写函数,实现对数组中是否存在某个元素的二分检索。<br>4. (5分)编写函数,实现对数组元素的快速排序。<br>5. (5分)定义一个类,以2个整数为数据成员,提供构造函数、数据成员的访问方<br>法和计算最大公约数和最小公倍数方法。<br>6.(5分) 根据下述描述创建一个时间类Time,具有h、m、s(时分、秒)属性,成<br>员访问方法和其它方法:<br>class Time<br>{ int h, m, s;<br>&nbsp; public:<br>&nbsp;&nbsp;&nbsp; Tmie(int hx = 0, int mx = 0, int sx = 0);<br>&nbsp;&nbsp;&nbsp; ...; //成员访问方法<br>&nbsp;&nbsp;&nbsp; Time&amp; increaseSecond(int s);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //增加秒<br>&nbsp;&nbsp;&nbsp; Time&amp; increaseMinute(int m);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //增加分<br>&nbsp;&nbsp;&nbsp; Time&amp; increaseHour(int h);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //增加小时<br>&nbsp;&nbsp;&nbsp; bool equal(const Time&amp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //判定是否相等<br>&nbsp;&nbsp;&nbsp; void print();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //输出<br>};<br>7.(10分)设计一个字符串类,要求具有计算字符串长度、两个字符串大小比较、<br>两个字符串连接等功能。<br>细节要求:<br>&nbsp;&nbsp; (1)构造:可利用字符串对象,指针,整数,字符,浮点数构造一个字符串以及<br>拷贝构造对象;<br>&nbsp;&nbsp; (2)运算:提供字符串加法,关系运算(即比较字符串大小运算),赋值运算;<br>&nbsp;&nbsp; (3)转换:转换为C 语言字符串,字符串长度,大小写转换,删除字符串前后空<br>格,字符串与整数和浮点数转换等。<br>&nbsp;&nbsp; (4)输入输出:支持输入输出插入运算&gt;&gt;和&lt;&lt;。<br>8.(10分)设计一个能存放整数的一维向量(数组)类Vector,具有以下功能:<br>&nbsp;&nbsp; (1)数组的建立和输入输出。<br>&nbsp;&nbsp; (2)对向量的运算符+,-,+=,-=进行重载。<br>&nbsp;&nbsp; (3)一般操作。<br>&nbsp;&nbsp; 细节要求:<br>&nbsp;&nbsp; 构造:可利用向量对象和C的一维数组构造向量对象以及拷贝构造对象;<br>&nbsp;&nbsp; 向量支持一般的数据类型<br>&nbsp;&nbsp; (3)运算:提供重新设置向量长度,向量加、减、乘(内积)运算以及拷贝构造<br>对象<br>&nbsp;&nbsp; (4)输入输出:支持输入输出插入运算&gt;&gt;和&lt;&lt;。<br>9.(10分)设计一个能存放整数的列表(链表)类List,具有以下功能:<br>&nbsp;&nbsp; (1)利用双向链表形成List类并可进行输入输出。<br>&nbsp;&nbsp; (2)对列表的运算符+,-,+=,-=进行重载。<br>&nbsp;&nbsp; (3)一般操作。<br>&nbsp;&nbsp; 细节要求:<br>&nbsp;&nbsp; 构造:可利用链表对象和C 的一维数组构造链表对象以及拷贝构造对象;<br>&nbsp;&nbsp; 链表支持一般的数据类型<br>&nbsp;&nbsp; (3)运算:提供诸如添加、删除节点等操作<br>&nbsp;&nbsp; (4)输入输出:支持输入输出插入运算&gt;&gt;和&lt;&lt;。<br>10.(10分)定义一个学生类,使其具有学号、姓名(长度不定)、高数、外语和C++<br>程序设计三门课的成绩属性,以及各属性访问方法、求总成绩、求平均成绩、设置<br>和显示学生信息方法。此外,在main函数中以定义学生数组方法模拟一个班的学生<br>信息,利用独立定义函数方式给出常见情况的统计,如最高成绩、最低成绩、平均<br>成绩、学生信息列表等。<br>11.(30分)实现一个计算器类,支持加、减、乘、除等基本运算。<br><br><span style="color: #3c20ff;">源代码下载:<a  href="http://www.cppblog.com/Files/honker/CppClass.zip">http://www.cppblog.com/Files/honker/CppClass.zip</a></span><br><br><br><img src ="http://www.cppblog.com/honker/aggbug/23134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/honker/" target="_blank">honker</a> 2007-04-28 20:18 <a href="http://www.cppblog.com/honker/archive/2007/04/28/23134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>