由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教C++程序中的Maze().exitMaze();
相关主题
在帮忙看看这个吧 C: int->char*please help debug this code
C++ Q13: Input[C++ boost::interprocess] 讨论贴
about namespaceostream& operator << (ostream& s, int cnt) error
请问关于overloading <<C++中怎么传递std::hex这样的参数啊
c++ template question:子类的assignment operator 怎么访问父类的private member
ofstream and cout question一个C++的概念问题
C++重载<<错误?为什么在overloading中,friend <<不能读取private值呢?
partial_sort问题再问两个C++问题
相关话题的讨论汇总
话题: maze话题: row话题: col话题: cols话题: int
进入Programming版参与讨论
1 (共1页)
k*******3
发帖数: 1909
1
下面这个程序来自Data structures and algorithms in C++ by Adam Drozdek
第四章的迷宫(maze)程序。
我的疑问是最后几行main函数里面的Maze().exitMaze();怎么解释,我想不通语法是怎
么样的。
Maze()是调用哪个函数呢?如果是Maze class的Maze() constructor,难道不用先定义
一个Maze 对象吗?
谢谢。
#include
#include
#include
using namespace std;
template
class Stack : public stack {
public:
T pop() {
T tmp = top();
stack::pop();
return tmp;
}
};
class Cell {
public:
Cell(int i = 0, int j = 0) {
x = i; y = j;
}
bool operator== (const Cell& c) const {
return x == c.x && y == c.y;
}
private:
int x, y;
friend class Maze;
};
class Maze {
public:
Maze();
void exitMaze();
private:
Cell currentCell, exitCell, entryCell;
const char exitMarker, entryMarker, visited, passage, wall;
Stack mazeStack;
char **store; // array of strings;
void pushUnvisited(int,int);
int rows, cols;
friend ostream& operator<< (ostream& out, const Maze& maze) {
for (int row = 0; row <= maze.rows+1; row++)
out << maze.store[row] << endl;
out << endl;
return out;
}
};
Maze::Maze() : exitMarker('e'), entryMarker('m'), visited('.'),
passage('0'), wall('1') {
Stack mazeRows;
char str[80], *s;
int col, row = 0;
cout << "Enter a rectangular maze using the following "
<< "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n"
<< "Enter one line at at time; end with Ctrl-z:\n";
while (cin >> str) {
row++;
cols = strlen(str);
s = new char[cols+3]; // two more cells for borderline columns;
mazeRows.push(s);
strcpy(s+1,str);
s[0] = s[cols+1] = wall; // fill the borderline cells with 1s;
s[cols+2] = '\0';
if (strchr(s,exitMarker) != 0) {
exitCell.x = row;
exitCell.y = strchr(s,exitMarker) - s;
}
if (strchr(s,entryMarker) != 0) {
entryCell.x = row;
entryCell.y = strchr(s,entryMarker) - s;
}
}
rows = row;
store = new char*[rows+2]; // create a 1D array of pointers;
store[0] = new char[cols+3]; // a borderline row;
for ( ; !mazeRows.empty(); row--) {
store[row] = mazeRows.pop();
}
store[rows+1] = new char[cols+3]; // another borderline row;
store[0][cols+2] = store[rows+1][cols+2] = '\0';
for (col = 0; col <= cols+1; col++) {
store[0][col] = wall; // fill the borderline rows with 1s;
store[rows+1][col] = wall;
}
}
void Maze::pushUnvisited(int row, int col) {
if (store[row][col] == passage || store[row][col] == exitMarker) {
mazeStack.push(Cell(row,col));
}
}
void Maze::exitMaze() {
int row, col;
currentCell = entryCell;
while (!(currentCell == exitCell)) {
row = currentCell.x;
col = currentCell.y;
cout << *this; // print a snapshot;
if (!(currentCell == entryCell))
store[row][col] = visited;
pushUnvisited(row-1,col);
pushUnvisited(row+1,col);
pushUnvisited(row,col-1);
pushUnvisited(row,col+1);
if (mazeStack.empty()) {
cout << *this;
cout << "Failure\n";
return;
}
else currentCell = mazeStack.pop();
}
cout << *this;
cout << "Success\n";
}
int main() {
Maze().exitMaze();
return 0;
}
p***o
发帖数: 1252
2

Somewhat yes.
No, you are creating a temporary one by saying Maze().
Well, it seems the whole piece of code is of bad taste. I don't think
anyone will write C++ like that in production code. Get a better book
from someone that knows how to write C++ programs.

【在 k*******3 的大作中提到】
: 下面这个程序来自Data structures and algorithms in C++ by Adam Drozdek
: 第四章的迷宫(maze)程序。
: 我的疑问是最后几行main函数里面的Maze().exitMaze();怎么解释,我想不通语法是怎
: 么样的。
: Maze()是调用哪个函数呢?如果是Maze class的Maze() constructor,难道不用先定义
: 一个Maze 对象吗?
: 谢谢。
: #include
: #include
: #include

k*******3
发帖数: 1909
3
Thanks!
For this book, actually my boss recommended it.
And I also check the amazon review, seems not bad.
http://www.amazon.com/Data-Structures-Algorithms-Adam-Drozdek-d
By your saying, I am actually confused whether this is a good book or not,
since I am not C++ expert.
Thanks for you answer anyway.

【在 p***o 的大作中提到】
:
: Somewhat yes.
: No, you are creating a temporary one by saying Maze().
: Well, it seems the whole piece of code is of bad taste. I don't think
: anyone will write C++ like that in production code. Get a better book
: from someone that knows how to write C++ programs.

t****t
发帖数: 6806
4
The data structure part may be good or bad, I don't know.
But obviously, the c++ level of the author is mediocre, if not bad. And as a
code example, it added in too much noise to express the point, e.g.
included but actually used , redefined std::stack to
make pop return top(), defined Cell but actually should use std::pair<>, etc
., basically a total failure.
There are plenty of good books about data structures. Get a better one.

【在 k*******3 的大作中提到】
: Thanks!
: For this book, actually my boss recommended it.
: And I also check the amazon review, seems not bad.
: http://www.amazon.com/Data-Structures-Algorithms-Adam-Drozdek-d
: By your saying, I am actually confused whether this is a good book or not,
: since I am not C++ expert.
: Thanks for you answer anyway.

k*******3
发帖数: 1909
5
Many Thanks!

a
etc

【在 t****t 的大作中提到】
: The data structure part may be good or bad, I don't know.
: But obviously, the c++ level of the author is mediocre, if not bad. And as a
: code example, it added in too much noise to express the point, e.g.
: included but actually used , redefined std::stack to
: make pop return top(), defined Cell but actually should use std::pair<>, etc
: ., basically a total failure.
: There are plenty of good books about data structures. Get a better one.

k*******3
发帖数: 1909
6
能不能推荐本用C++写得算法和数据结构的好书?
谢谢!

a
etc

【在 t****t 的大作中提到】
: The data structure part may be good or bad, I don't know.
: But obviously, the c++ level of the author is mediocre, if not bad. And as a
: code example, it added in too much noise to express the point, e.g.
: included but actually used , redefined std::stack to
: make pop return top(), defined Cell but actually should use std::pair<>, etc
: ., basically a total failure.
: There are plenty of good books about data structures. Get a better one.

t****t
发帖数: 6806
7
不能, 我这两个分开学的

【在 k*******3 的大作中提到】
: 能不能推荐本用C++写得算法和数据结构的好书?
: 谢谢!
:
: a
: etc

k*******3
发帖数: 1909
8
haha, thanks!

【在 t****t 的大作中提到】
: 不能, 我这两个分开学的
1 (共1页)
进入Programming版参与讨论
相关主题
再问两个C++问题c++ template question:
请问这是什么错误呀ofstream and cout question
C++ template question with friend ostreamC++重载<<错误?
Why should i include .cpp instead of .hpartial_sort问题
在帮忙看看这个吧 C: int->char*please help debug this code
C++ Q13: Input[C++ boost::interprocess] 讨论贴
about namespaceostream& operator << (ostream& s, int cnt) error
请问关于overloading <<C++中怎么传递std::hex这样的参数啊
相关话题的讨论汇总
话题: maze话题: row话题: col话题: cols话题: int