高手来看看这个简单的程序

flydream1980 2008-12-30 06:17:45
题目如下,大家帮看看。
给定一个连通图的接口:

class Graph
{
/** Given one node, return all the nodes that are connected with it.
@param [in] nodeID is the input node
@param [out] outwardIds are the nodes which are connected with nodeID.
@param [out] costs are the corresponding costs from nodeID to
each one of outwardIds
*/
virtual void getOutwardNodes(int nodeID, vector<int>& outwardIds,
vector<int>& costs) = 0;
};

下面这个迷宫,也可以看做是一张连通图。

char* g_maze[6] = {
" XXX ",
" XX XXXX",
" XX ",
" XX XX ",
" XX ",
"XXXXXXXX "
};

X - 阻碍

请写出一个Maze类,以g_maze为数据模型,实现Graph接口。

* x, y坐标用高16位,低16位,压缩成一个int,做为nodeID。
* 不许走到数组之外。
* 可以横向、纵向、斜向移动。横向、纵向走一格的距离cost为100。斜着走一格,距离cost为141。


测试用例:坐标按照(x,y)表示
输入为(0,0),输出为(0,1) cost 100, (1,1) cost 141
输入为(1,1),输出为(0,0) cost 141, (0,1) cost 100, (0,2) cost 141, (1,2) cost 100

接口函数定义如下,请实现它们:

int makeId(int x, int y); // 把x作为高16位,y作为低16位,拼出一个整数,作为nodeID
int getX(int id); // 取出id中的高16位,作为X坐标
int getY(int id); // 取出id中的低16位,作为y坐标

class Maze : public Graph {
Maze(char**, int width, int height); // 构造函数。给定一个二维数组,构造一个迷宫对象。数据应该在内部复制一份。
~Maze();
virtual void getOutwardNodes(int nodeID, vector<int>& outwardIds,
vector<int>& costs);
}

我编写的代码如下。 两个问题,1 什么叫面向接口的编程? 2 我下面的不是?高手过来帮小弟看看,欢迎批评指正
#include <iostream>
#include <vector>
using namespace std;

class Graph
{
/** Given one node, return all the nodes that are connected with it.
@param [in] nodeID is the input node
@param [out] outwardIds are the nodes which are connected with nodeID.
@param [out] costs are the corresponding costs from nodeID to
each one of outwardIds
*/
public:
virtual void getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs) = 0;
};
class Maze : public Graph
{
public:
Maze(char**a, int width, int height); // 构造函数。给定一个二维数组,构造一个迷宫对象。数据应该在内部复制一份。
~Maze();
virtual void getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs);
int makeId(int x, int y); // 把x作为高16位,y作为低16位,拼出一个整数,作为nodeID
int getX(int id); // 取出id中的高16位,作为X坐标
int getY(int id); // 取出id中的低16位,作为y坐标
void print();
int m_getOutwardNodes(int x,int y);
private:
char **m_maze;
int width;
int height;
vector<int> outwardIds;
vector<int> costs;
};

Maze::Maze(char**a, int width, int height)
{
this->width = width;
this->height = height;
m_maze = new char *[height];
for(int i=0; i<height;i++)
{
m_maze[i] = new char[width+1];
}
for(i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
m_maze[i][j]=a[i][j];
}
}
}
Maze::~Maze()
{

for(int i=0; i<height; i++)
delete []m_maze[i];
delete []m_maze;
}
int Maze::makeId(int x, int y)
{
int nodeID =0;
nodeID |= y;
nodeID |= x<<16;

return nodeID;
}
int Maze::getX(int id)
{
int x = id>>16;
return x;
}
int Maze::getY(int id)
{
int y =id;
y = y<<16;
y = y>>16;
return y;
}

void Maze::getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs)
{
// 坐标系中的坐标 (x,y) 在数组中的坐标 为 (y,x)
int x= getX(nodeID);
int y= getY(nodeID);
int temp=0;
//求左边的一列,上中下元素
if(x-1>=0)
{
if(y-1>=0&&m_maze[y-1][x-1]==' ')
{
temp=makeId(x-1,y-1);
outwardIds.push_back(temp);
costs.push_back(141);
}
if(m_maze[y][x-1]==' ')
{
temp=makeId(x-1,y);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x-1]==' ')
{
temp=makeId(x-1,y+1);
outwardIds.push_back(temp);
costs.push_back(141);
}
}
//求中间的一列,上面元素和下面元素
if(y-1>=0&&m_maze[y-1][x]==' ')
{
temp=makeId(x,y-1);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x]==' ')
{
temp=makeId(x,y+1);
outwardIds.push_back(temp);
costs.push_back(100);
}
//求右边的一列,上中下元素
if(x+1<width)
{
if(y-1>=0&&m_maze[y-1][x+1]==' ')
{
temp=makeId(x+1,y-1);
outwardIds.push_back(temp);
costs.push_back(141);
}
if(m_maze[y][x+1]==' ')
{
temp=makeId(x+1,y);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x+1]==' ')
{
temp=makeId(x+1,y+1);
outwardIds.push_back(temp);
costs.push_back(141);
}
}
}
void Maze::print()
{
vector<int>::iterator it1, it2;
int temp, x, y;
for ( it1=outwardIds.begin(),it2=costs.begin() ; it1 < outwardIds.end()&& it2 < costs.end() ; it1++,it2++)
{
temp = *it1;
x= getX(temp);
y= getY(temp);
cout<<"( "<<x<<','<<y<<" ) "<<*it2<<endl;
}

}
int Maze::m_getOutwardNodes(int x,int y)
{
if(x<0||y<0||x>=width||y>=height||m_maze[y][x]!=' ') return-1; //值非法
outwardIds.clear(); //清空容器
costs.clear();

getOutwardNodes(makeId(x,y),outwardIds,costs);

print();
return 0;
}
int main()
{
char* g_maze[6] =
{
" XXX ",
" XX XXXX",
" XX ",
" XX XX ",
" XX ",
"XXXXXXXX "
};
cout<<" 0123456789\n";
for(int i=0; i<6;i++)
{
cout<<i<<g_maze[i]<<endl;
}
//构造对象
Maze maze(g_maze,strlen(g_maze[0]),6);
int x,y;
cout<<"\n输入 坐标 x,y\n";
cin>>x>>y;
/*
通过m_getOutwardNodes(x,y) 调用
getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs)
*/
int success=maze.m_getOutwardNodes(x,y);
if(success==-1)
cout<<"输入的值非法\n";
return 0;

}
...全文
109 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
就呆在云上 2008-12-30
  • 打赏
  • 举报
回复

你的接口没有按照题目的要求给啊,这样吧,算是补全,我不能否认你的接口设计:
#include <iostream>
#include <vector>
using namespace std;

#define Interface struct

Interface Graph
{
/** Given one node, return all the nodes that are connected with it.
@param [in] nodeID is the input node
@param [out] outwardIds are the nodes which are connected with nodeID.
@param [out] costs are the corresponding costs from nodeID to
each one of outwardIds
*/
public:
virtual void getOutwardNodes(
int nodeID, vector<int>& outwardIds,vector<int>& costs) = 0;
virtual int getX(int id) = 0; // 取出id中的高16位,作为X坐标
virtual int getY(int id) = 0; // 取出id中的低16位,作为y坐标
};
class Maze : public Graph
{
public:
Maze(char**a, int width, int height); // 构造函数。给定一个二维数组,构造一个迷宫对象。数据应该在内部复制一份。
~Maze();
virtual void getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs);
int makeId(int x, int y); // 把x作为高16位,y作为低16位,拼出一个整数,作为nodeID
int getX(int id); // 取出id中的高16位,作为X坐标
int getY(int id); // 取出id中的低16位,作为y坐标
void print();
int m_getOutwardNodes(int x,int y);
private:
char **m_maze;
int width;
int height;
vector<int> outwardIds;
vector<int> costs;
};

Maze::Maze(char**a, int width, int height)
{
this->width = width;
this->height = height;
m_maze = new char *[height];
for(int i=0; i<height;i++)
{
m_maze[i] = new char[width+1];
}
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
m_maze[i][j]=a[i][j];
}
}
}
Maze::~Maze()
{

for(int i=0; i<height; i++)
delete []m_maze[i];
delete []m_maze;
}
int Maze::makeId(int x, int y)
{
int nodeID =0;
nodeID |= y;
nodeID |= x<<16;

return nodeID;
}
int Maze::getX(int id)
{
int x = id>>16;
return x;
}
int Maze::getY(int id)
{
int y =id;
y = y<<16;
y = y>>16;
return y;
}

void Maze::getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs)
{
// 坐标系中的坐标 (x,y) 在数组中的坐标 为 (y,x)
int x= getX(nodeID);
int y= getY(nodeID);
int temp=0;
//求左边的一列,上中下元素
if(x-1>=0)
{
if(y-1>=0&&m_maze[y-1][x-1]==' ')
{
temp=makeId(x-1,y-1);
outwardIds.push_back(temp);
costs.push_back(141);
}
if(m_maze[y][x-1]==' ')
{
temp=makeId(x-1,y);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x-1]==' ')
{
temp=makeId(x-1,y+1);
outwardIds.push_back(temp);
costs.push_back(141);
}
}
//求中间的一列,上面元素和下面元素
if(y-1>=0&&m_maze[y-1][x]==' ')
{
temp=makeId(x,y-1);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x]==' ')
{
temp=makeId(x,y+1);
outwardIds.push_back(temp);
costs.push_back(100);
}
//求右边的一列,上中下元素
if(x+1<width)
{
if(y-1>=0&&m_maze[y-1][x+1]==' ')
{
temp=makeId(x+1,y-1);
outwardIds.push_back(temp);
costs.push_back(141);
}
if(m_maze[y][x+1]==' ')
{
temp=makeId(x+1,y);
outwardIds.push_back(temp);
costs.push_back(100);
}
if(y+1<height&&m_maze[y+1][x+1]==' ')
{
temp=makeId(x+1,y+1);
outwardIds.push_back(temp);
costs.push_back(141);
}
}
}
void Maze::print()
{
vector<int>::iterator it1, it2;
int temp, x, y;
for ( it1=outwardIds.begin(),it2=costs.begin() ; it1 < outwardIds.end()&& it2 < costs.end() ; it1++,it2++)
{
temp = *it1;
x= getX(temp);
y= getY(temp);
cout<<"( "<<x<<','<<y<<" ) "<<*it2<<endl;
}

}
int Maze::m_getOutwardNodes(int x,int y)
{
if(x<0||y<0||x>=width||y>=height||m_maze[y][x]!=' ') return-1; //值非法
outwardIds.clear(); //清空容器
costs.clear();

getOutwardNodes(makeId(x,y),outwardIds,costs);

print();
return 0;
}
int main()
{
char* g_maze[6] =
{
" XXX ",
" XX XXXX",
" XX ",
" XX XX ",
" XX ",
"XXXXXXXX "
};
cout<<" 0123456789\n";
for(int i=0; i<6;i++)
{
cout<<i<<g_maze[i]<<endl;
}
//构造对象
Maze maze(g_maze,strlen(g_maze[0]),6);
int x,y;
cout<<"\n输入 坐标 x,y\n";
cin>>x>>y;
/*
通过m_getOutwardNodes(x,y) 调用
getOutwardNodes(int nodeID, vector<int>& outwardIds,vector<int>& costs)
*/
int success=maze.m_getOutwardNodes(x,y);
if(success==-1)
cout<<"输入的值非法\n";
return 0;

}
就呆在云上 2008-12-30
  • 打赏
  • 举报
回复
  1.关于接口的理解。
  接口从更深层次的理解,应是定义(规范,约束)与实现(名实分离的原则)的分离。
  我们在一般实现一个系统的时候,通常是将定义与实现合为一体,不加分离的,我认为最为理解的系统设计规范应是所有的定义与实现分离,尽管这可能对系统中的某些情况有点繁烦。
  接口的本身反映了系统设计人员对系统的抽象理解。
  接口应有两类:第一类是对一个体的抽象,它可对应为一个抽象体(abstract class);
  第二类是对一个体某一方面的抽象,即形成一个抽象面(interface);
  一个体有可能有多个抽象面。
  抽象体与抽象面是有区别的。

  2.设计接口的另一个不可忽视的因素是接口所处的环境(context,environment),系统论的观点:环境是系统要素所处的空间与外部影响因素的总和。任何接口都是在一定的环境中产生的。因此环境的定义及环境的变化对接口的影响是不容忽视的,脱离原先的环境,所有的接口将失去原有的意义。
OenAuth.Net 2008-12-30
  • 打赏
  • 举报
回复
两个问题,1 什么叫面向接口的编程?

类成员变量私有,用户使用你写的类时,只调用公有的成员函数

2 我下面的不是?高手过来帮小弟看看,欢迎批评指正

基本算是,呵呵,下班了,顶

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧