如果嫌复制粘贴麻烦,可以到我的资源下载:http://download.csdn.net/detail/dancheng1/9708649
这个下下来直接可以使用。
这些代码直接就可以复制粘贴进行操作(编译器:不支持 vc++6.0)
需要建立的文件有:
GraphBian.cpp
GraphDian.cpp
PrimNum.cpp
graph.h
graph.cpp
Utils.cpp
graphmain.cpp
GraphBian.cpp中的代码为:
class bb{
private:
int arcNum;
public:
int B[maxn][maxn]; //边
int Flag[maxn][maxn]; //记录边是否被遍历过
bb(){
memset(B, -1, sizeof(B));
memset(Flag, 0, sizeof(Flag));
arcNum = 0;
}
int getArcNum(){
return arcNum;
}
void setArcNum(int sarcNum){
arcNum = sarcNum;
}
void zero(){ //归零
memset(B, 0, sizeof(B));
memset(Flag, 0, sizeof(Flag));
arcNum = 0;
}
};
GraphDian.cpp中的代码为:
class dd{
private:
string name[maxn];
int qz[maxn];
int vertexNum; //顶点个数
public:
dd(){
for(int i = 0; i < vertexNum; i++){
name[i] = "";
}
memset(qz, 0, sizeof(qz));
vertexNum = 0;
}
string * getName(){
return name;
}
void setName(string sname[]){
for(int i = 0; i < vertexNum && i < maxn; i++){
name[i] = sname[i];
}
}
int * getQz(){
return qz;
}
void setQz(int sqz[]){
for(int i = 0; i < vertexNum && i < maxn; i++){
qz[i] = sqz[i];
}
}
int getVertexNum(){
return vertexNum;
}
void setVertexNum(int svertexNum){
vertexNum = svertexNum;
}
void zore(){
for(int i = 0; i < vertexNum; i++){
name[i] = "";
}
memset(qz, 0, sizeof(qz));
vertexNum = 0;
}
};
PrimNum.cpp中的代码为:
class Prime{
private:
int index;
int qz;
public:
void setIndex(int sindex){
index = sindex;
}
int getIndex(){
return index;
}
void setQz(int sqz){
qz = sqz;
}
int getQz(){
return qz;
}
};
graph.h中的代码为:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 20;
const int INF = 0xfffff;
#include "GraphBian.cpp"
#include "GraphDian.cpp"
#include "PrimNum.cpp"
#include "jjdd.h"
class ALGraph{
private:
bb side; //边的信息
dd spot; //点的信息
public:
dd getDd(){
return spot;
}
bb getBb(){
return side;
}
//---------------图的创建模块----------------------------------------------------
void creatOneY(string a[], int n);
void creatOneW(string a[], int n);
void creatTwoY();
void creatTwoW();
//---------------添加模块--------------------------------------------------------
bool addGraphVex(); //增加顶点
bool addGraphArc(); //增加边
//---------------删除模块--------------------------------------------------------
bool deleteGraphVex(int i); //利用图的下标删除顶点 (i = [1,n]) 在进入删除模块时显示每个顶点对应的name
bool deleteGraphArc(int i, int j); //利用图的下标删除边(i = [1,n], j = [1,n])先显示信息
//---------------修改模块--------------------------------------------------------
bool updateGraphVex(int i); //修改顶点信息
bool updateGraphArc(int i, int j, int q); //修改边的信息
//---------------查询模块--------------------------------------------------------
void showGraphVex(); //显示图的所有点
void showGraphArc(); //显示图的所有边的信息
void showGraphAll(); //显示图的全部信息
string findVexByVextexNum(int i); //利用顶点下标显示顶点信息(i = [1,n]) -----未完成
int findVexByVextexName(string sname); //利用顶点的名称查询这个顶点的信息
//---------------使用图----------------------------------------------------------
bool DFSTraverse(int v); //深度优先非递归遍历图
void DFSTraverseDg(int v); //深度优先递归遍历图
bool BFSTraverse(int v); //广度优先非递归遍历图
void BFSTraverseDg(int v); //广度优先递归遍历图
void Prim(); //Prim方法的最小生成树
//---------------摧毁图----------------------------------------------------------
void deleteGraph(); //摧毁图
};
graph.cpp中的代码为:
#include "graph.h"
int choose = 1, choose1 = 1;
string a[maxn]; //获取本图的名字
ALGraph algraph;
int stack[maxn]; //深度优先遍历存储结构
int queue[maxn * maxn]; //广度优先遍历存储结构
struct PrimNumber{ //存储最小生成树的边的;
int oneindex;
int twoindex;
void setAll(int x, int y){
oneindex = x;
twoindex = y;
}
}priNumber[maxn * maxn];
Prime pri[maxn];
int f[maxn] = {0}; //标记点是否被遍历过
int twotemp, threetemp, fourtemp, fivetemp, sixtemp, seventemp, ninetemp, tentemp;
void ALGraph::creatOneY(string a[], int n){
spot.setVertexNum(n);
spot.setName(a);
int e, first, last, q, iii = 0;
cout<<"请输入有向图边的个数?"<<endl;
cin>>e;
cout<<endl<<endl;
side.setArcNum(e);
cout<<"请设定"<<e<<"条边每个边的两个定点,以及每条边所对应的权值,"<<endl<<"输入格式(端点i 端点j 权值q)【每两个参数之间有一个空格,并且保证输入的i, j值在[1, "<<n<<"]之间】"<<endl;
while(iii < e){
cout<<"第"<<iii + 1<<"条边信息:";
cin>>first>>last>>q;
if(first >= 1 && first <= n && last >= 1 && last <= n){
side.B[first - 1][last - 1] = q; //有向图的只有一面//设置权值
iii++;
} else {
cout<<"您输入的信息有误,请重新输入!"<<endl;
}
}
cout<<endl<<endl<<"------------------------------------------------------"<<endl;
}
void ALGraph::creatOneW(string a[], int n){
spot.setVertexNum(n);
spot.setName(a);
int e, first, last, q, iii = 0;
cout<<"请输入无向图边的个数?"<<endl;
cin>>e;
cout<<endl<<endl;
side.setArcNum(e);
cout<<"请设定"<<e<<"条边每个边的两个定点,以及每条边所对应的权值,"<<endl<<"输入格式(端点i 端点j 权值q)【每两个参数之间有一个空格,并且保证输入的i, j值在[1, "<<n<<"]之间】"<<endl;
while(iii < e){
cout<<"第"<<iii + 1<<"条边信息:";
cin>>first>>last>>q;
if(first >= 1 && first <= n && last >= 1 && last <= n){
side.B[first - 1][last - 1] = q;
side.B[last - 1][first - 1] = q;
iii++;
} else {
cout<<"您输入的信息有误,请重新输入!"<<endl;
}
}
}
void ALGraph::creatTwoY(){
cout<<endl<<"您选择的图形为:"<<endl<<endl;
printf(" 10 \n");
printf(" v0-------------->v1 \n");
printf(" ^ \\ | \n");
printf(" 15 | \\ 20 | 20 \n");
printf(" | \\ | \n");
printf(" | \\ | \n");
printf(" | >> \n");
printf(" v3<--------------v2 \n");
printf(" \\ 10 > \n");
printf(" 20 \\ / 10 \n");
printf(" > / \n");
printf(" v4 \n");
cout<<endl<<" 请选择对此图的操作吧!"<<endl<<endl;
string a[5] = {"v0", "v1", "v2", "v3", "v4"};
spot.setVertexNum(5);
spot.setName(a);
side.setArcNum(7);
side.B[0][1] = 10;
side.B[1][2] = 20;
side.B[0][2] = 20;
side.B[3][0] = 15;
side.B[2][3] = 10;
side.B[3][4] = 20;
side.B[4][2] = 10;
}
void ALGraph::creatTwoW(){
cout<<endl<<"您选择的图形为:"<<endl<<endl;
printf(" 10 \n");
printf(" v0-------------->v1 \n");
printf(" ^ \\ | \n");
printf(" 15 | \\ 20 | 20 \n");
printf(" | \\ | \n");
printf(" | \\ | \n");
printf(" | >> \n");
printf(" v3<--------------v2 \n");
printf(" \\ 10 > \n");
printf(" 20 \\ / 10 \n");
printf(" > / \n");
printf(" v4 \n");
cout<<endl<<" 请选择对此图的操作吧!"<<endl<<endl;
string a[5] = {"v0", "v1", "v2", "v3", "v4"};
spot.setVertexNum(5);
spot.setName(a);
side.setArcNum(7);
side.B[0][1] = 10;
side.B[1][0] = 10;
side.B[1][2] = 20;
side.B[2][1] = 20;
side.B[0][2] = 20;
side.B[2][0] = 20;
side.B[3][0] = 15;
side.B[0][3] = 15;
side.B[2][3] = 10;
side.B[3][2] = 10;
side.B[3][4] = 20;
side.B[4][3] = 20;
side.B[4][2] = 10;
side.B[2][4] = 10;
}
//--------添加模块--------------------------------------------------------------------------------
bool ALGraph::addGraphVex(){
string * ssname = spot.getName();
cout<<"已有的端点:"<<endl;
for(int i = 0; i < spot.getVertexNum(); i++){
cout<<ssname[i]<<" ";
}
cout<<endl;
if(spot.getVertexNum() < maxn){
string sname;
cout<<endl<<"请输入要添加点的名称:";
cin>>sname;
cout<<endl;
ssname[spot.getVertexNum()] = sname;
spot.setVertexNum(spot.getVertexNum() + 1);
spot.setName(ssname);
for(int i = 0; i < spot.getVertexNum(); i++){
side.B[spot.getVertexNum() - 1][i] = -1;
}
for(int i = 0; i < spot.getVertexNum(); i++){
side.B[i][spot.getVertexNum() - 1] = -1;
}
return true;
} else {
return false;
}
}
bool ALGraph::addGraphArc(){
int i, j, k;
cout<<"请输入要添加边的两个顶点下标和这个边的权值:"<<endl;
cout<<"注意:边的下标范围是 i, j = [1, "<<spot.getVertexNum()<<"],输入格式是 i j q 每两个数之间有空格间隔"<<endl;
cout<<" 你所要添加的边在原图中存在的话,会返回修改失败的!"<<endl;
cout<<"您的输入是:";
cin>>i>>j>>k;
cout<<endl;
if(side.B[i - 1][j - 1] != -1 || i < 1 || j < 1 || i > spot.getVertexNum() || j > spot.getVertexNum()){
return false;
} else {
side.B[i - 1][j - 1] = k;
return true;
}
}
//--------删除模块--------------------------------------------------------------------------------
bool ALGraph::deleteGraphVex(int i){ //利用图的下标删除顶点 (i = [1,n]) 在进入删除模块时显示每个顶点对应的name
string * a = spot.getName();
if(i > 0 && i <= spot.getVertexNum()){
for(int j = i; j < spot.getVertexNum(); j++){
a[j - 1] = a[j];
}
for(int j = 0; j < spot.getVertexNum(); j++){
for(int k = i; k < spot.getVertexNum(); k++){
side.B[j][k - 1] = side.B[j][k];
side.Flag[j][k - 1] = side.Flag[j][k];
}
}
for(int j = 0; j < spot.getVertexNum(); j++){
for(int k = i; k < spot.getVertexNum(); k++){
side.B[k - 1][j] = side.B[k][j];
side.Flag[k - 1][j] = side.Flag[k][j];
}
}
spot.setVertexNum(spot.getVertexNum() - 1);
int n = 0;
if(twotemp == 1){
for(int i = 0; i < spot.getVertexNum(); i++){
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[i][j] != -1){
n++;
}
}
}
} else {
for(int i = 0; i < spot.getVertexNum(); i++){
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[i][j] != -1){
n++;
}
}
}
n /= 2;
}
side.setArcNum(n);
return true;
} else {
return false;
}
}
bool ALGraph::deleteGraphArc(int i, int j){
if(twotemp == 1){
if(i > 0 && i <= side.getArcNum() && j > 0 && j <= side.getArcNum() && side.B[i - 1][j - 1] != -1){
side.B[i - 1][j - 1] = -1;
return true;
} else {
return false;
}
} else {
if(i > 0 && i <= side.getArcNum() && j > 0 && j <= side.getArcNum() && side.B[i - 1][j - 1] != -1){
side.B[i - 1][j - 1] = -1;
side.B[j - 1][i - 1] = -1;
return true;
} else {
return false;
}
}
}
//--------修改模块--------------------------------------------------------------------------------
bool ALGraph::updateGraphVex(int i){
string * sname = spot.getName();
if(i >= 1 && i <= spot.getVertexNum()){
string ssname;
cout<<"请输入这个点的新名字:";
cin>>ssname;
cout<<endl;
sname[i - 1] = ssname;
spot.setName(sname);
return true;
} else {
return false;
}
}
bool ALGraph::updateGraphArc(int i, int j, int q){
if(i >= 1 && i <= spot.getVertexNum() && j >= 1 && j <= spot.getVertexNum()){
side.B[i - 1][j - 1] = q;
return true;
} else {
return false;
}
}
//--------查询模块--------------------------------------------------------------------------------
void ALGraph::showGraphVex(){
string * a = spot.getName();
for(int i = 0; i < spot.getVertexNum(); i++){
cout<<i + 1<<"."<<a[i]<<" ";
}
cout<<endl;cout<<endl;
}
void ALGraph::showGraphArc(){
string * a = spot.getName();
for(int i = 0; i < spot.getVertexNum(); i++){
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[i][j] != -1){
cout<<i + 1<<"."<<a[i]<<" ----> "<<j + 1<<"."<<a[j]<<" 的值为:"<<side.B[i][j]<<endl;
}
}
}
cout<<endl;cout<<endl;
}
void ALGraph::showGraphAll(){
string * a = spot.getName();
cout<<"此图有"<<spot.getVertexNum()<<"个顶点,他们分别是:"<<endl;
for(int i = 0; i < spot.getVertexNum(); i++){
cout<<i + 1<<"."<<a[i]<<" ";
}
cout<<endl<<"此图有"<<side.getArcNum()<<"条边,每条边的信息为:"<<endl;
for(int i = 0; i < spot.getVertexNum(); i++){
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[i][j] != -1){
cout<<i + 1<<"."<<a[i]<<" ----> "<<j + 1<<"."<<a[j]<<" 的值为:"<<side.B[i][j]<<endl;
}
}
}
cout<<endl;cout<<endl;
}
string ALGraph::findVexByVextexNum(int i){ //利用下标显示顶点信息i = [1,n]
string * a = spot.getName();
string ssname = "NULL";
int n = 0;
for(int j = 0; j < spot.getVertexNum(); j++){
if(j == i - 1){
ssname = a[i - 1];
}
if(side.B[i - 1][j] != -1){
n++;
}
}
if(ssname != "NULL"){
cout<<"顶点"<<i<<"."<<a[i - 1]<<"的度为:"<<n<<endl<<endl;
cout<<"所依附此点的边有:"<<endl;
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[i - 1][j] != -1){
cout<<i<<"."<<a[i - 1]<<" ---> "<<j + 1<<"."<<a[j]<<" 值为:"<<side.B[i - 1][j]<<endl;
}
}
cout<<endl<<endl;
}
return ssname;
}
int ALGraph::findVexByVextexName(string sname){
string * a = spot.getName();
int jl = -2, n = 0;
for(int j = 0; j < spot.getVertexNum(); j++){
if(a[j] == sname){
jl = j;
}
}
if(jl != -2){
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[jl][j] != -1){
n++;
}
}
cout<<"顶点"<<jl + 1<<"."<<sname<<"的度为:"<<n<<endl<<endl;
cout<<"所依附此点的边有:"<<endl;
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[jl][j] != -1){
cout<<jl + 1<<"."<<sname<<" ---> "<<j + 1<<"."<<a[j]<<" 值为:"<<side.B[jl][j]<<endl;
}
}
cout<<endl<<endl;
}
return jl + 1;
}
//--------遍历图----------------------------------------------------------------------------------
bool ALGraph::DFSTraverse(int v){ //输入进来的这个点是 v = [1, n]
if(v >= 1 && v <= spot.getVertexNum()){
int i = v - 1, j = 0;
string * sname = spot.getName();
stack[j++] = i;
f[i] = 1;
cout<<sname[i]<<" ";
while(j != -1){
bool pd = false;
for(int k = 0; k < spot.getVertexNum(); k++){
if(side.B[i][k] != -1 && f[k] != 1){
i = k;
cout<<sname[i]<<" ";
stack[j++] = i;
f[i] = 1;
pd = true;
break;
}
}
if(pd == false){
i = stack[--j];
}
}
cout<<endl<<endl;
return true;
} else {
return false;
}
}
void ALGraph::DFSTraverseDg(int v){ //这个参数的范围是: v = [0, n - 1]
string * ssname = spot.getName();
cout<<ssname[v]<<" "; f[v] = 1;
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[v][j] != 1 && f[j] != 1)
DFSTraverseDg(j);
}
}
bool ALGraph::BFSTraverse(int v){ //参数范围是 v = [1, n]
if(v >= 1 && v <= spot.getVertexNum()){
int i = v - 1, j_start = 0, j_end = 0;
string * sname = spot.getName();
queue[j_end++] = i;
f[i] = 1;
cout<<sname[i]<<" ";
while(j_start != j_end){
for(int k = 0; k < spot.getVertexNum(); k++){
if(side.B[i][k] != -1 && f[k] != 1){
f[k] = 1;
cout<<sname[k]<<" ";
queue[j_end++] = k;
}
}
j_start++;
i = queue[j_start];
}
cout<<endl<<endl;
return true;
} else {
return false;
}
}
void ALGraph::BFSTraverseDg(int v){ ////这个参数的范围是: v = [0, n - 1]
string * ssname = spot.getName();
int j_start = 0, j_end = 0;
cout<<ssname[v]<<" "; f[v] = 1; queue[j_end++] = v;
while(j_start != j_end){
v = queue[j_start++];
for(int j = 0; j < spot.getVertexNum(); j++){
if(side.B[v][j] != -1 && f[j] != 1){
cout<<ssname[j]<<" "; f[j] = 1; queue[j_end++] = j;
}
}
}
}
void ALGraph::Prim(){
int v = 0;
for(int i = 0; i < spot.getVertexNum(); i++){
pri[i].setIndex(v);
pri[i].setQz(INF);
}
f[v] = 1; int jl = 0, num = 0;
while(jl != INF){
for(int j = 0; j < spot.getVertexNum(); j++){
if(f[j] != 1){
if(side.B[v][j] != -1){
if(side.B[v][j] < pri[j].getQz()){
pri[j].setIndex(v);
pri[j].setQz(side.B[v][j]);
}
} else {
}
}
}
jl = INF; int jlll;
for(int i = 0 ; i < spot.getVertexNum(); i++){
if(jl > pri[i].getQz() && f[i] != 1){
jl = pri[i].getQz();
jlll = pri[i].getIndex();
v = i;
}
}
f[v] = 1;
priNumber[num].setAll(jlll, v);
num++;
}
for(int i = 0; i < spot.getVertexNum() - 1; i++){
cout<<priNumber[i].oneindex<<"---->"<<priNumber[i].twoindex<<endl;
}
}
//--------摧毁图----------------------------------------------------------------------------------
void ALGraph::deleteGraph(){
spot.zore();
side.zero();
}
Utils.cpp中的代码为:
#include "graph.cpp"
void fourtempisOne(){
cout<<"------添加功能-------"<<endl;
cout<<"---------------------"<<endl;
cout<<"|增加顶点请按-----1 |"<<endl;
cout<<"|增加边请按-------2 |"<<endl;
cout<<"|返回上一层请选---3 |"<<endl;
cout<<"|退出程序请选-----0 |"<<endl;
cout<<"---------------------"<<endl;
cout<<"你的选择是:";
cin>>fivetemp;
cout<<endl;
switch(fivetemp){
case 1:{
bool pd = algraph.addGraphVex();
if(pd == true){
cout<<"添加成功!"<<endl<<endl;
} else {
cout<<"添加失败!也许是顶点数到上限了!"<<endl<<endl;
}
break;
}
case 2:{
bool pd = algraph.addGraphArc();
if(pd == true){
cout<<"添加成功!"<<endl<<endl;
} else {
cout<<"添加失败!要听从注意的内容!"<<endl<<endl;
}
break;
}
case 3:
choose = 0;
break;
case 0:
exit(0);
break;
default:
cout<<endl<<"你脑子有泡!!!,告诉你选0-3的,还选别的"<<endl<<endl;
break;
}
}
void fourtempisTwo(){
cout<<"--------删除功能---------"<<endl;
cout<<"-------------------------"<<endl;
cout<<"|利用下标删顶点请按---1 |"<<endl;
cout<<"|利用下标删边---------2 |"<<endl;
cout<<"|返回上一层请选-------3 |"<<endl;
cout<<"|退出程序请选---------0 |"<<endl;
cout<<"-------------------------"<<endl;
cout<<"你的选择是:";
cin>>fivetemp;
cout<<endl;
switch(fivetemp){
case 1:{
cout<<"由点的下标删除此点,点的信息:";
algraph.showGraphVex();
cout<<"你想删除哪个点,输入范围:i = [1, "<<algraph.getDd().getVertexNum()<<"]"<<endl;
cout<<"您的选择是:i = ";
cin>>sixtemp;
cout<<endl;
bool pd = algraph.deleteGraphVex(sixtemp);
if(pd){
cout<<endl<<"删除成功!"<<endl<<endl;
} else {
cout<<endl<<"删除失败!"<<endl<<endl;
}
break;
}
case 2:{
cout<<"由点的下标删除此边,边的信息:"<<endl;
algraph.showGraphArc();
cout<<"你想删除哪个边,输入范围:i, j = [1, "<<algraph.getDd().getVertexNum()<<"]"<<endl;
cout<<"输入格式:i j(中间要有一个空格)";
cin>>sixtemp>>seventemp;
cout<<endl;
bool pd = algraph.deleteGraphArc(sixtemp, seventemp);
if(pd){
cout<<endl<<"删除成功!"<<endl<<endl;
} else {
cout<<endl<<"删除失败!"<<endl<<endl;
}
break;
}
case 3:
choose = 0;
break;
case 0:
exit(0);
break;
default:
cout<<endl<<"你脑子有泡!!!,告诉你选0-6的,还选别的"<<endl<<endl;
break;
}
}
void fourtempisThree(){
cout<<"-------修改功能-------"<<endl;
cout<<"----------------------"<<endl;
cout<<"|修改顶点名字请按--1 |"<<endl;
cout<<"|修改边的权值请按--2 |"<<endl;
cout<<"|返回上一层请选----3 |"<<endl;
cout<<"|退出程序请选------0 |"<<endl;
cout<<"----------------------"<<endl;
cout<<"你的选择是:";
cin>>fivetemp;
cout<<endl;
switch(fivetemp){
case 1:{
cout<<"您想修改哪个顶点的名字?"<<endl;
algraph.showGraphVex();
cout<<endl<<"修改哪个点输入此点的序号,输入范围是 i = [1, "<<algraph.getDd().getVertexNum()<<"]"<<endl;
cout<<"您修改的点序号为:i = ";
cin>>tentemp;
cout<<endl;
bool pd = algraph.updateGraphVex(tentemp);
if(pd == true){
cout<<"修改成功!"<<endl<<endl;
} else {
cout<<"您输入有误!不能修改!"<<endl<<endl;
}
break;
}
case 2:{
cout<<"您想修改哪个边的权值?"<<endl;
algraph.showGraphArc();int q;
cout<<"修改哪个边,请用这条边所依附的两个顶点表示,输入范围是 i = [1, "<<algraph.getDd().getVertexNum()<<"]"<<endl;
cout<<"输入格式为 i j q 其中每两个数之间要有空格!"<<endl;
cout<<"您要修改的边和新的权值是:";
cin>>ninetemp>>tentemp>>q;
cout<<endl;
bool pd = algraph.updateGraphArc(ninetemp, tentemp, q);
if(pd == true){
cout<<"修改成功!"<<endl<<endl;
} else {
cout<<"您输入有误!不能修改!"<<endl<<endl;
}
}
case 3:
choose = 0;
break;
case 0:
exit(0);
break;
default:
cout<<endl<<"你脑子有泡!!!,告诉你选0-6的,还选别的"<<endl<<endl;
break;
}
}
void fourtempisFour(){
cout<<"--------------查询功能---------------"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"|显示图的所有点请选---------------1 |"<<endl;
cout<<"|显示图的所有边请选---------------2 |"<<endl;
cout<<"|显示图的所有信息-----------------3 |"<<endl;
cout<<"|利用顶点下标查询点的信息请选-----4 |"<<endl;
cout<<"|利用顶点的名字查询点的信息请选---5 |"<<endl;
cout<<"|返回上一层请选-------------------6 |"<<endl;
cout<<"|退出程序请选---------------------0 |"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"你的选择是:";
cin>>fivetemp;
cout<<endl;
switch(fivetemp){
case 1:
algraph.showGraphVex();
break;
case 2:
algraph.showGraphArc();
break;
case 3:
algraph.showGraphAll();
break;
case 4:{
cout<<"您想查看哪个点的信息? (点的范围是 i = [1, "<<algraph.getDd().getVertexNum()<<"])"<<endl;
cout<<"i = ";
cin>>sixtemp;
cout<<endl;
string sname = algraph.findVexByVextexNum(sixtemp);
if(sname != "NULL"){
cout<<endl<<"成功查找此点!"<<endl<<endl;
} else {
cout<<endl<<"查找此点失败!"<<endl<<endl;
}
break;
}
case 5:{
string * a = algraph.getDd().getName();
string sname;
cout<<"你想查看那个点的信息?(点的名字有 name = {";
for(int i = 0; i < algraph.getDd().getVertexNum(); i++){
cout<<a[i];
if(i != algraph.getDd().getVertexNum() - 1){
cout<<", ";
}
}
cout<<"})"<<endl<<"name = ";
cin>>sname;
cout<<endl;
int pd = algraph.findVexByVextexName(sname);
if(pd != -1){
cout<<endl<<"成功查找此点!"<<endl<<endl;
} else {
cout<<endl<<"查找此点失败!"<<endl<<endl;
}
break;
}
case 6:
choose = 0;
break;
case 0:
exit(0);
break;
default:
cout<<endl<<"你脑子有泡!!!,告诉你选0-6的,还选别的"<<endl<<endl;
break;
}
}
void fourtempisFive(){
cout<<"-----------使用图------------"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"|深度优先非递归遍历请按---1 |"<<endl;
cout<<"|深度优先递归遍历请按-----2 |"<<endl;
cout<<"|广度优先非递归遍历请按---3 |"<<endl;
cout<<"|广度优先递归遍历请按-----4 |"<<endl;
cout<<"|Prim方法的最小生成树-----5 |"<<endl;
cout<<"|返回上一层请选-----------6 |"<<endl;
cout<<"|退出程序请选-------------0 |"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"你的选择是:";
cin>>fivetemp;
cout<<endl;
switch(fivetemp){
case 1:{
memset(f, 0, sizeof(f));
cout<<endl<<"可选择的点有:";algraph.showGraphVex();cout<<endl;
cout<<"您想从哪个点开始遍历?【输入范围 i = [1, "<<algraph.getDd().getVertexNum()<<"]】"<<endl;
cout<<"您的选择是:i = ";
cin>>tentemp;
cout<<endl;
bool pd = algraph.DFSTraverse(tentemp);
if(pd == false){
cout<<"遍历失败!也许是你输入的点超出范围!"<<endl<<endl;
} else {
cout<<endl<<"遍历成功!"<<endl<<endl;
}
break;
}
case 2:{
memset(f, 0, sizeof(f));
cout<<endl<<"可选择的点有:";algraph.showGraphVex();cout<<endl;
cout<<"您想从哪个点开始遍历?【输入范围 i = [1, "<<algraph.getDd().getVertexNum()<<"]】"<<endl;
cout<<"注意:一定要输入下标!"<<endl;
cout<<"您的选择是:i = ";
cin>>tentemp;
cout<<endl;
if(tentemp >= 1 && tentemp <= algraph.getDd().getVertexNum()){
algraph.DFSTraverseDg(tentemp - 1);
cout<<endl<<"遍历成功!"<<endl;
} else {
cout<<"遍历失败!也许是你输入的点超出范围!"<<endl<<endl;
}
break;
}
case 3:{
memset(f, 0, sizeof(f));
cout<<endl<<"可选择的点有:";algraph.showGraphVex();cout<<endl;
cout<<"您想从哪个点开始遍历?【输入范围 i = [1, "<<algraph.getDd().getVertexNum()<<"]】"<<endl;
cout<<"注意:一定要输入下标!"<<endl;
cout<<"您的选择是:i = ";
cin>>tentemp;
cout<<endl;
bool pd = algraph.BFSTraverse(tentemp);
if(pd == false){
cout<<"遍历失败!也许是你输入的点超出范围!"<<endl<<endl;
} else {
cout<<endl<<"遍历成功!"<<endl<<endl;
}
break;
}
case 4:{
memset(f, 0, sizeof(f));
cout<<endl<<"可选择的点有:";algraph.showGraphVex();cout<<endl;
cout<<"您想从哪个点开始遍历?【输入范围 i = [1, "<<algraph.getDd().getVertexNum()<<"]】"<<endl;
cout<<"注意:一定要输入下标!"<<endl;
cout<<"您的选择是:i = ";
cin>>tentemp;
cout<<endl;
if(tentemp >= 1 && tentemp <= algraph.getDd().getVertexNum()){
algraph.BFSTraverseDg(tentemp - 1);
cout<<endl<<"遍历成功!"<<endl;
} else {
cout<<"遍历失败!也许是你输入的点超出范围!"<<endl<<endl;
}
break;
}
case 5:{
memset(f, 0, sizeof(f));
algraph.Prim();
break;
}
case 6:
choose = 0;
break;
case 0:
exit(0);
break;
default:
cout<<endl<<"你脑子有泡!!!,告诉你选0-6的,还选别的"<<endl<<endl;
break;
}
}
void fourtempisSix(){
algraph.deleteGraph();
choose = 0;
choose1 = 0;
cout<<endl<<" 摧毁图成功!"<<endl;
cout<<" ^_^ "<<endl;
cout<<"------------------------"<<endl;
cout<<"|您是退出还是重新选图!|"<<endl;
cout<<"|----------------------|"<<endl;
cout<<"|重新选图请选 1 |"<<endl;
cout<<"|退出程序请选 0 |"<<endl;
cout<<"------------------------"<<endl;
cout<<"您的选择是:";
cin>>fourtemp;
cout<<endl;
if(fourtemp == 0){
exit(0);
}
}
bool empty(){ //判断图是否存在
if(algraph.getDd().getVertexNum() != 0 || algraph.getBb().getArcNum() != 0){
return true;
} else {
return false;
}
}
bool xz(int onetemp){ //选择图
cout<<endl<<endl;
if(onetemp == 1){
cout<<"------------------------------"<<endl;
cout<<"|您想新建一个有向图?无向图?|"<<endl;
cout<<"|----------------------------|"<<endl;
cout<<"|有向图选 1 |"<<endl;
cout<<"|无向图选 0 |"<<endl;
cout<<"------------------------------"<<endl;
cout<<"您的选择是:";
cin>>twotemp;
cout<<endl<<endl;
if(twotemp == 1){
cout<<"您需要几个顶点?"<<endl;
cin>>threetemp;
cout<<endl<<endl;
cout<<"请为每一个点起一个名字:"<<endl;
for(int i = 0; i < threetemp; i++){
cout<<"第"<<i + 1<<"个:";
cin>>a[i];
}
cout<<endl<<endl;
algraph.creatOneY(a, threetemp);
} else if(twotemp == 0){
cout<<"您需要几个顶点?"<<endl;
cin>>threetemp;
cout<<endl<<endl;
cout<<"请为每一个点起一个名字:"<<endl;
for(int i = 0; i < threetemp; i++){
cout<<"第"<<i + 1<<"个:";
cin>>a[i];
}
cout<<endl<<endl;
algraph.creatOneW(a, threetemp);
}
} else if(onetemp == 0){
cout<<"----------------------------"<<endl;
cout<<"|你想要默认有向图?无向图?|"<<endl;
cout<<"|--------------------------|"<<endl;
cout<<"|默认有向图请选 1 |"<<endl;
cout<<"|默认无向图请选 0 |"<<endl;
cout<<"----------------------------"<<endl;
cout<<"您的选择是:";
cin>>twotemp;
if(twotemp == 1){
algraph.creatTwoY();
} else if(twotemp == 0){
algraph.creatTwoW();
}
}
if(algraph.getDd().getVertexNum() != 0 || algraph.getBb().getArcNum() != 0){
return true;
} else {
return false;
}
}
graphmain.cpp
#include "Utils.cpp"
int main(){
while(1){
choose1 = 1;
if(!empty()){
cout<<"-------------------------------"<<endl;
cout<<"|您想新建一个图? 使用默认图?|"<<endl;
cout<<"|-----------------------------|"<<endl;
cout<<"|新建一个图选 1 |"<<endl;
cout<<"|使用默认图选 0 |"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"您的选择是:";
int onetemp;
while(1){
cin>>onetemp;
if(xz(onetemp))
break;
else{
cout<<"你选择出错了,请重新选择!";
}
}
}
while(choose1){
choose = 1;
cout<<"-----功能页面------"<<endl;
cout<<"-------------------"<<endl;
cout<<"|添加功能请选---1 |"<<endl;
cout<<"|删除功能请选---2 |"<<endl;
cout<<"|修改功能请选---3 |"<<endl;
cout<<"|查询功能请选---4 |"<<endl;
cout<<"|使用图请选-----5 |"<<endl;
cout<<"|摧毁图请选-----6 |"<<endl;
cout<<"|退出程序请选---0 |"<<endl;
cout<<"-------------------"<<endl;
cout<<"您的选择是:";
cin>>fourtemp;
cout<<endl;
switch(fourtemp){
case 1:
while(choose){
fourtempisOne();
}
break;
case 2:
while(choose){
fourtempisTwo();
}
break;
case 3:
while(choose){
fourtempisThree();
}
break;
case 4:
while(choose){
fourtempisFour();
}
break;
case 5:
while(choose){
fourtempisFive();
}
break;
case 6:
while(choose){
fourtempisSix();
}
break;
case 0:
exit(0);
default:
cout<<endl<<"你个傻缺,告诉你选0-5的,还选别的"<<endl<<endl;
break;
}
}
}
return 0;
}