这是本人第一次写博客文章
刚把课程设计写完,很开心,之前一直有bug,搞了几天,没有调试出来,于是换了一种写法,避免了bug。希望我的这些代码可以给正在写课程设计的同学,一点帮助。里面有的代码也是来自博客,然后结合我自己的代码而成,写的不是很好,不喜勿喷。
代码如下:
#include <iostream>
#include <string>
#include<iomanip>//引入输入输出格式头文件
using namespace std;
const int Maxsize = 50;
typedef struct
{
string code;
string name;
string intro;
}vertex;
class MGraph
{
public:
MGraph(int n, int e);
void Seek(int i);
void Floyd();
void Allpaths();
void Allpathlengths();
void Printshortpath();
private:
vertex a[Maxsize];
int arc[Maxsize][Maxsize];
int vertexNum, arcNum;
int dist[Maxsize][Maxsize];
string path[Maxsize][Maxsize];
};
MGraph::MGraph(int n, int e)
{
int i, j;
vertexNum = n;
arcNum = e;
a[0].code = "a";
a[0].name = "校门";
a[0].intro = "该门为正门,也叫南门";
a[1].code = "b";
a[1].name = "音乐楼";
a[1].intro = "刚建成不久,是音乐学院教学用楼,设备齐全,是本校最好的教学楼";
a[2].code = "c";
a[2].name = "体育馆";
a[2].intro = "用于进行体育活动与教学";
a[3].code = "d";
a[3].name = "医务室";
a[3].intro = "给学生提高基本的医疗救助,大病还需前往大医院治疗";
a[4].code = "e";
a[4].name = "教B";
a[4].intro = "也称为弘文楼";
a[5].code = "f";
a[5].name = "图书馆";
a[5].intro = "读书与学习的地方,是我校标志性建筑物";
a[6].code = "g";
a[6].name = "教A";
a[6].intro = "也称为崇文楼";
a[7].code = "h";
a[7].name = "生化楼";
a[7].intro = "jjfgkjh";
a[8].code = "i";
a[8].name = "宿舍";
a[8].intro = "学生生活的地方";
a[9].code = "j";
a[9].name = "操场";
a[9].intro = "运动场所,也是夜晚小情侣们约会的场所,夜晚慎入,避免吃狗粮,嘻嘻";
a[10].code = "k";
a[10].name = "理工楼";
a[10].intro = "理科生教学用楼";
for (i = 0; i<vertexNum; i++) //初始化边,请将不到达边初始值为最大值,这里使用10000
for (j = 0; j<vertexNum; j++)
arc[i][j] = 10000;
arc[0][1] = 500;
arc[0][8] = 1000;
arc[0][3] = 400;
arc[0][6] = 500;
arc[1][2] = 100;
arc[2][3] = 100;
arc[3][6] = 100;
arc[6][8] = 400;
arc[2][4] = 50;
arc[2][5] = 50;
arc[5][6] = 100;
arc[4][7] = 100;
arc[5][7] = 100;
arc[6][9] = 300;
arc[7][10] = 200;
arc[9][10] = 400;
arc[8][9] = 500;
}
void MGraph::Seek(int i)
{
cout << "景点代号:" << a[i].code << endl;
cout << "景点名称:" << a[i].name << endl;
cout << "景点简介:" << a[i].intro << endl;
cout << "***************************************" << endl;
}
void MGraph::Floyd()
{
int i, j, k;
for (i = 0; i<vertexNum; i++) //初始化dist和path
for (j = 0; j<vertexNum; j++)
{
dist[i][j] = arc[i][j];
if (dist[i][j] != 10000)
path[i][j] = a[i].code + a[j].code;
else path[i][j] = " ";
}
for (k = 0; k<vertexNum; k++) //判定顶点i j之间是否经过k
for (i = 0; i<vertexNum; i++)
for (j = 0; j<vertexNum; j++)
if (dist[i][k] + dist[k][j]<dist[i][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = path[i][k] + "-" + path[k][j];
}
}
void MGraph::Allpaths()
{
cout << "***************************************" << endl;
cout << "***景点代号: ***" << endl;
cout << "***a校门 b音乐楼 c体育馆 ***" << endl;
cout << "***d医务室 e教B f图书馆 ***" << endl;
cout << "***g教A h生化楼 i宿舍 ***" << endl;
cout << "***j操场 k理工楼 ***" << endl;
cout << "***************************************" << endl;
cout << "图的所有路径如下:" << endl;
for (int i = 0; i<vertexNum; i++) //输出图的所有路径信息
{
for (int j = 0; j<vertexNum; j++)
cout << setw(12) << setiosflags(ios::left) << path[i][j] << " "; //10个字符位置,且左对齐
cout << endl;
}
}
void MGraph::Allpathlengths()
{
cout << "图的所有路径长如下:" << endl;
for (int i = 0; i<vertexNum; i++) //输出图的各边长信息
{
for (int j = 0; j<vertexNum; j++)
if (dist[i][j] == 10000)
cout << " ";
else
cout << setw(5) << setiosflags(ios::left) << dist[i][j] << " ";//3个字符位置,且左对齐
cout << endl;
}
}
void MGraph::Printshortpath() //结点m到n的最短路径
{
cout << "***************************************" << endl;
cout << "***景点代号: ***" << endl;
cout << "***a校门 b音乐楼 c体育馆 ***" << endl;
cout << "***d医务室 e教B f图书馆 ***" << endl;
cout << "***g教A h生化楼 i宿舍 ***" << endl;
cout << "***j操场 k理工楼 ***" << endl;
cout << "***************************************" << endl;
cout << "您想了解哪两个点的最短路径?"<<endl;
cout << "代号如上图,请分别输入这两个点的代号并以空格分开" << endl;
int n, m, i;
string ch1, ch2;
cin >> ch1 >> ch2; //输入要判定是的顶点,请输入顶点字符。
for (i = 0; i < vertexNum; i++)
if (ch1 == a[i].code) n= i;
for (i = 0; i<vertexNum; i++)
if (a[i].code==ch2) m= i;
cout << ch1 << "到" << ch2 << "的最短路径为:" << path[n][m] << endl;
cout << ch1 << "到" << ch2 << "的最短路径长为:" << dist[n][m] << endl;
system("pause");
}
void meau()
{
cout << "***************************************" << endl;
cout << "***欢迎使用校园导游咨询系统 ***" << endl;
cout << "***输入1执行景点信息查询 ***" << endl;
cout << "***输入2执行最短路径查询 ***" << endl;
cout << "***输入3执行输出所有路径 ***" << endl;
cout << "***输入4执行输出所有路径长度 ***" << endl;
cout << "***输入0执行退出系统 ***" << endl;
cout << "***************************************" << endl;
}
int main()
{
MGraph m(11, 17);
meau();
int choice;
while (true)
{
cout << "请输入您要执行功能的编号:" ;
cin >> choice;
switch (choice)
{
case 1:
cout << "***************************************" << endl;
cout << "***景点编号如下: ***" << endl;
cout << "***0校门 1音乐楼 2体育馆 ***" << endl;
cout << "***3医务室 4教B 5图书馆 ***" << endl;
cout << "***6教A 7生化楼 8宿舍 ***" << endl;
cout << "***9操场 10理工楼 ***" << endl;
cout << "***************************************" << endl;
cout << "编号如上图,请输入您要查询景点的编号:";
int code;
cin >> code;
m.Seek(code);
break;
case 2:m.Floyd();
m.Printshortpath();
break;
case 3:m.Floyd();
m.Allpaths();
break;
case 4:m.Floyd();
m.Allpathlengths();
break;
case 0:return 0;
}
}
return 0;
}
仅供参考