\\graph.h---类的声明,包含在这个文件中
class Graph
{public:
Graph(int point);
~Graph();
Graph& addpath(int point1,int point2);
Graph& addview(int point);
void addinformation(int point);
bool searchview(int point);
void shortdistance();
void showgraph();
private:
int **graph; -------二维数组
char **information ----景点的信息说明
int number;};
\\graph.cpp---类的实现
#include<iostream>
#include"graph.h"
Graph::Graph(int point)
{number=point;
graph=new int*[point+1];
fot(int i=0;i<=point;i++)
graph[i]=new int[point];
for(int i=0;i<=number;i++)
for(int j=0;j<=number;j++)
graph[i][j]=0;
information=new char*[number+1];
for(int i=0;i<=number;i++)
information[i]=new char[100];}
void Graph::addinformation(int point)
{cout<<"请输入第"<<point<<"个景点的信息"<<endl;
cin>>information[point];}
Graph::~Graph()
{for(int i=0;i<number;i++)
delete[]graph[i];
delete[]graph;
number=0;}
bool Graph::addpath(int point1,int point2)
{if(graph[point1][point2]==0)
{cout<<"这条边已经存在,已经不能在插入!"<<endl;
return o;}
graph[point1][[point2]=1;
return 1;
}
Graph& Graph::addview(int point)
{int **p=new int*[point+1];
for(int i=0;i<=point;i++)
p[i]=new int[point];
for(int i=0;i<=point;i++)
for(int j=0;j<=point;j++)
p[i][j]=0;
for(int j=1;j<=number;j++)
for(int i=1;i<=number;i++)
p[i][j]=graph[i][j];
char**pp=new char*[point+1];
for(int i=0;i<=point;i++)
pp[i]=new char[100];
for(int i=1;i<=number;i++)
strcpy(pp[i],information[i])
number=point;
information=pp;
graph=p;
return *this;}
Graph& Graph::addpath(int point1,int point2)
{if(graph[point1][point2]==1||graph[point2][point1]==1)
throw error; ----这个还没有开始定义了
graph[point1][point2]=graph[point2][point1]=1;
return *this;
}
bool Graph::searchview(int point)
{if(point<0||point>number)
{cout<<"你所查询的景点不存在"<<endl;
return 0;}
cout<<information(point)<<endl;
return 1;}
void Graph::shortdistance(int point1,int point2)
{int **t=new int*[number+1];
for(int i=0;i<=number;i++)
t[i]=new int[number+1];
for(int i=0;i<=number;i++)
for(int j=0;j<=number;j++)
t[i][j]=graph[i][j];
int **path=new int*[number+1];
for(int i=0;i<=number;i++)
t[i]=new int[number+1];
for(int i=0;i<=number;i++)
for(int j=0;j<=number;j++)
t[i][j]=0;
for(int k=1;k<=number;k++)
for(int i=1;i<=number;i++)
for(int j=1;j<=number;j++)
{int t1=t[i][j];
int t2=t[i][k];
int t3=t[j][k];
if(t1==0&&t2!=0&&t3!=0||t2+t3<t1)
t[i][j]=t2+t3;
path[i][j]=k;}
}
int end=point2;
cout<<"你所要的最短路径经过"<<point1<<endl;
while(t[point1][point2]!=0)
{cout<<t[point1][point2]<<endl;
point2=t[point1][point2];
}
cout<<"到"<<end<<endl;
}
void Graph::showgraph()
{cout<<"该图形是:"<<endl;
for(int i=1;i<=number;i++)
for(int j=1;j<=number;j++)
if(graph[i][j]!=0)
cout<<graph[i][j]<<"->";
}
//main函数的实现
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
void main()
{char judgement='Y';
int flags;
while(judgement=='Y')
{
cout<<setiosflags(ios_base::left)<<"1.要初始化几个景点"<<endl;
cout<<"2.增加新的路径"<<endl;
cout<<"3. 增加一个景点"<<endl;
cout<<"4.增加一个景点的有关信息!"<<endl;
cout<<"5.要查询一个景点的有关信息!"<<endl;
cout<<"6.要看两个景点之间的最短路径!"<<endl;
cout<<"7.要看全部景点的路径!"<<endl;
cout<<"请选择一个!"<<endl;
cin>>flags;
switch(flags)
{
case 1:{int max;cout<<"请输入景点的个数"<<endl;cin>>max;Graph photo(max);break;}
case 2:{int p1,p2;cout<<"请是输入你要增加路径的两个景点"<<endl;cin>>p1>>p2;photo(p1,p2);break;}
case 3:{int p;cout<<"请输入你要增加的景点"<<endl;cin>>p;photo.addview(p);break;}
case 4:{int p;cout<<" 请输入你要加入有关信息的景点名字"<<endl;cin>>p;photo.addinformation(p);break;}
case 5:{int p;cout<<"请输入要查询的景点"<<endl;cin>>p;photo.searchview(p);break;}
case 6:{int p1,p2;cout<<"请输入两个景点的名字"<<endl;cin>>p1>>p2;photo.shortdistance(p1,p2);break;}
case 7:photo.showview();break;
}
cout<<"是否要继续操作,如果是,请按“Y”,否则请按任意键"<<endl;
cin>>judgement;
}
}