65,211
社区成员
发帖
与我相关
我的任务
分享#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;
}#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;
} 1.关于接口的理解。
接口从更深层次的理解,应是定义(规范,约束)与实现(名实分离的原则)的分离。
我们在一般实现一个系统的时候,通常是将定义与实现合为一体,不加分离的,我认为最为理解的系统设计规范应是所有的定义与实现分离,尽管这可能对系统中的某些情况有点繁烦。
接口的本身反映了系统设计人员对系统的抽象理解。
接口应有两类:第一类是对一个体的抽象,它可对应为一个抽象体(abstract class);
第二类是对一个体某一方面的抽象,即形成一个抽象面(interface);
一个体有可能有多个抽象面。
抽象体与抽象面是有区别的。
2.设计接口的另一个不可忽视的因素是接口所处的环境(context,environment),系统论的观点:环境是系统要素所处的空间与外部影响因素的总和。任何接口都是在一定的环境中产生的。因此环境的定义及环境的变化对接口的影响是不容忽视的,脱离原先的环境,所有的接口将失去原有的意义。