石家庄铁道大学数据结构实验4 图(完整代码)

这个实验应该是最麻烦的一个了
就很烦 写了500多行 洒洒水啦

#include<algorithm>
#include <iostream>
#include<cmath>
using namespace std;
typedef struct 
{
	char vexs[35];
	int arcs[35][35];
	int vexnum, arcnum;
}AMGraph;//邻接矩阵
int LocatedVex(const AMGraph& G,char nam) {
	int i = 0;
	for (i = 0; i < G.vexnum; i++) {
		if (G.vexs[i] == nam) break;
	}
	if (i == G.vexnum) return -1;
	return i;
}

void CreateUDN(AMGraph& G)
{
	cout << "请输入节点数与边数" << endl;
	cin >> G.vexnum >> G.arcnum;
	cout << "请输入各个位置的代号" << endl;
	for (int i = 0; i < G.vexnum; i++)
	{
		cin >> G.vexs[i];
	}
	/*for(int i=0;i<G.vexnum;i++)
		for (int j = 0; j < G.vexnum; j++)
		{
			G.arcs[i][j] = 0;
		}*/
	for (int k = 0; k < G.arcnum; k++)
	{
		int w;
		char v1, v2;
		cout << "请输入两个顶点及其之间的边的权值" << endl;
		cin >> v1 >> v2 >> w;
		int locat1=0,locat2=0;
		for (locat1 = 0; locat1 < G.vexnum; locat1++)
		{
			if (v1 == G.vexs[locat1])
				break;
		}
		for (locat2 = 0; locat2 < G.vexnum; locat2++)
		{
			if (v2 == G.vexs[locat2])
				break;
		}
		G.arcs[locat1][locat2] = w;
		G.arcs[locat2][locat1] = w;

	}

}
//创建邻接矩阵
bool visited[35];
void DFS_AM(AMGraph G, int v)
{
	cout << G.vexs[v]<<" ";
	visited[v] = true;
	for (int i = 0; i < G.vexnum;i++)
	{
		if (G.arcs[v][i]<1000&&G.arcs[v][i] != 0 && !visited[i])
		{
			DFS_AM(G, i);
			
		}
	}
}
//深度遍历邻接矩阵
void insert(AMGraph& G, char a, char b, int w)
{
	cout << "插入一个点并连一条边" << endl;
	G.vexs[G.vexnum] = b;
	//cout << G.vexs[G.vexnum] << endl;
	G.vexnum++;
	//cout << G.vexnum <<" "<<G.vexs[G.vexnum-1]<< endl;
	int i = 0;
	while (a != G.vexs[i])
	{
		i++;
	}
	G.arcs[i][G.vexnum-1] = w;
	//cout << G.vexs[i] << " " << G.vexs[G.vexnum-1] << endl;
	G.arcs[G.vexnum-1][i] = w;
}
//插入节点
void del(AMGraph& G, char a)
{
	//cout << "删除这个点的一条边还是删除这个点?1/2" << endl;
	//int  s;
	//cin >> s;
	/*if (s == 1)
	{
		cout << "请输入要删除的边的对应顶点" << endl;
		char m;
		cin >> m;
		int temp1 = 0, temp2 = 0;
		while (G.vexs[temp1] != a)
		{
			temp1++;
		}
		while (G.vexs[temp2] != m)
		{
			temp2++;
		}
		G.arcs[temp1][temp2] = 0;
		G.arcs[temp2][temp1] = 0;
		G.arcnum--;
	}
	else
	{*/
		int temp1=0 ;
		while (G.vexs[temp1] != a)
		{
			temp1++;
		}
		for (int i = 0; i < G.vexnum; i++)
		{
			if (G.arcs[i][temp1] != 0&&G.arcs[i][temp1]<100 || G.arcs[temp1][i] != 0&&G.arcs[temp1][i]<100)
			{
				G.arcs[i][temp1] = 0;
				G.arcs[temp1][i] = 0;
				G.arcnum--;
			}
		}
	//}
}
//删除节点
int dist[35];
bool st[35];
int dijkstra(AMGraph G,int m)
{
	for (int i = 0; i <35; i++)
	{
		dist[i] = 10010;
		//cout << dist[i] <<" ";
	}
	dist[0] = 0;
	//cout << G.vexnum << endl;
	for (int i = 0; i < G.vexnum; i++)
	{
		int t = -1;
		for (int j = 0; j < G.vexnum; j++)
			if (st[j]==false && (t == -1 || dist[t] > dist[j]))
			{
				//cout << G.vexs[j] << " " << dist[j] << endl;
				t = j;
			}
		//cout<<endl;
		st[t] = true;
		for (int j = 0; j < G.vexnum; j++)
		{
			//cout << dist[t] << " " << G.arcs[t][j] << endl;
			dist[j] = min(dist[j], dist[t] + G.arcs[t][j]);
		}
	}
	/*for (int i = 0; i < G.vexnum; i++)
	{
		cout << dist[i]<<" ";
	}*/
	if (dist[m] == 0x3f3f3f3f)
		return -1;
	else
		return dist[m];

}
//迪杰斯特拉求最短路
AMGraph G;
typedef struct ArcNode
{
	int adjvex;
	struct ArcNode* nextarc;
	int data;
};
typedef struct VNode
{
	char name;
	ArcNode* firstarc;
}VNode,AdjList[35];
typedef struct
{
	AdjList vertices;
	int vexnum, arcnum;
}ALGraph;
//邻接表
ALGraph G2;
void CreatUDG(ALGraph& G2)
{
	cout << "请输入节点数和边数" << endl;
	cin >> G2.vexnum >> G2.arcnum;
	cout << "请输入节点名称" << endl;
	for (int i = 0; i < G2.vexnum; i++)
	{
		cin >> G2.vertices[i].name;
		G2.vertices[i].firstarc = NULL;
	}
	for (int k = 0; k < G2.arcnum; k++)
	{
		char v1, v2;
		int path;
		cout << "请输入两个顶点值及其权值" << endl;
		cin >> v1 >> v2>>path;
		int locat1=0, locat2 = 0;
		for (locat1 = 0; locat1 < G2.vexnum; locat1++)
		{
			if (v1 == G2.vertices[locat1].name)
			{
			
				break;
			}
		}
		for (locat2 = 0; locat2 < G2.vexnum; locat2++)
		{
			if (v2 == G2.vertices[locat2].name)
			{
				break;
			}
		}
		ArcNode* p1 = new ArcNode;
		p1->adjvex = locat2;
		p1->data = path;
		p1->nextarc = G2.vertices[locat1].firstarc;
		G2.vertices[locat1].firstarc = p1;

		ArcNode* p2 = new ArcNode;
		p2->adjvex = locat1;
		p2->data = path;
		p2->nextarc = G2.vertices[locat2].firstarc;
		G2.vertices[locat2].firstarc = p2;

	}
}
//邻接表的创建
bool visited2[35];
void DFS_AL(ALGraph G, int v)
{
	int w;
	cout << G.vertices[v].name << " ";
	visited2[v] = true;
	ArcNode* p = G.vertices[v].firstarc;
	//cout << p->adjvex << endl;
	while (p != NULL)
	{
		w = p->adjvex;
		if (!visited2[w])
			DFS_AL(G, w);
		p = p->nextarc;
	}

}
//邻接表的遍历
void insertvex2(ALGraph& G2,char v)
{
	G2.vertices[G2.vexnum].name = v;
	G2.vertices[G2.vexnum].firstarc = NULL;
	G2.vexnum++;
}
void del2(ALGraph& G2,char vertex)
{
	
	int position = -1;
	ArcNode* p = NULL, * q = NULL, * r = NULL;
	cout << "删除节点以及相关边" << endl;
	for (int i = 0; i < G2.vexnum; i++)
	{
		// cout << G2.vertices[i].name << endl;
		if (G2.vertices[i].name == vertex)
		{
			//cout << G2.vertices[i].name << endl;
			position = i;
			p = G2.vertices[i].firstarc;
			for (int j = i + 1; j < G2.vexnum; j++)
			{
				G2.vertices[j - 1].name = G2.vertices[j].name;
				G2.vertices[j - 1].firstarc = G2.vertices[j].firstarc;
			}
			G2.vexnum--;
			break;
		}
	}
	if(position==-1)
	{
		cout << "不存在数据信息为" << vertex << "的顶点" << endl;
	}
	else
	{
		while (p)
		{
			q = p->nextarc;
			delete p;
			p = q;
		}
		for (int i = 0; i < G2.vexnum; i++)
		{
			p = G2.vertices[i].firstarc;
			while (p)
			{
				if (p->adjvex == position)
				{
					if (p == G2.vertices[i].firstarc)
						G2.vertices[i].firstarc = p->nextarc;
					else
					{
						r->nextarc = p->nextarc;
					}
					q = p;
					p = p->nextarc;
					delete q;
				}
				else
				{
					if (p->adjvex > position)
					{
						p->adjvex--;
					}
					r = p;
					p = p->nextarc;
				}
			}
		}
	}
}
int locatedvex2(ALGraph& G2, char v)
{
	for (int i = 0; i < G2.vexnum; i++)
	{
		//cout << G2.vertices[i].name << endl;
		if (G2.vertices[i].name == v)
			return i;
	}
	return -1;
}
void insertarc(ALGraph& G2, char x, char y, int s)
{
	int i = locatedvex2(G2, x);
	int j = locatedvex2(G2, y);
	//cout << i <<" "<< j << endl;
	ArcNode* p,* pr=NULL,*temp;
	for (p = G2.vertices[i].firstarc; p != NULL; p = p->nextarc)
	{
		pr = p;
	}
	temp = new ArcNode();
	temp->adjvex = j;
	temp->nextarc = NULL;
	temp->data = s;
	if (G2.vertices[i].firstarc == NULL)
	{
		G2.vertices[i].firstarc = temp;
	}
	else pr->nextarc = temp;
	temp = new ArcNode();
	temp->adjvex = i;
	temp->nextarc = NULL;
	temp->data = s;
	if (G2.vertices[j].firstarc == NULL)
		G2.vertices[j].firstarc = temp;
	else
	{
		p = G2.vertices[j].firstarc;
		while (p->nextarc != NULL) p = p->nextarc;
		p->nextarc = temp;
	}
	G2.arcnum++;
}
void floyd(ALGraph& G2, int path[35][35], int d[35][35])
{
	for (int i = 0; i < G2.vexnum; i++)
	{
		for (int j = 0; j < G2.vexnum; j++)
		{
			if (i == j)
				d[i][j] == 0;
			else
			{
				ArcNode* p = G2.vertices[i].firstarc;
				while (p != NULL && p->adjvex != j)
					p = p->nextarc;
				if (p == NULL)
					d[i][j] = 100;
				else
					d[i][j] = p->data;
			}
			if (d[i][j] < 100 && i != j)
				path[i][j] = i;
			else
				path[i][j] = -1;
		}
	}
	for (int i = 0; i < G2.vexnum; i++)
	{
		for (int j = 0; j < G2.vexnum; j++)
		{
			for (int k = 0; k < G2.vexnum; k++)
			{
				if (d[j][i] + d[i][k] < d[j][k])
				{
					d[j][k] = d[j][i] + d[i][k];
					path[j][k] = path[i][k];
				}
			}
		}
	}
}
int main()
{
	cout << "欢迎使用张润轩的校园向导系统!" << endl;
	cout << "选择你的方式?1邻接矩阵2邻接表...." << endl;
	int selectaim;

	cin >> selectaim;
	if (selectaim == 1)
	{
		memset(visited, false, sizeof visited);
		memset(G.arcs, 0x3f, sizeof G.arcs);
		CreateUDN(G);
		cout << "邻接矩阵:" << endl;
		cout << "初始化校园图......" << endl;
		while (1)
		{
			//DFS_AM(G, 1);
			memset(visited, false, sizeof visited);
			char a, b;
			cout << "请输入要执行的操作?1插入 2删除节点 3求最短路径 4遍历校园 5 结束" << endl;
			int secondselect;
			cin >> secondselect;
			if (secondselect == 1)
			{
				cout << "请选择插入方式本程序提供插入操作为三种1插点2插边3插点连边" << endl;
				int minimi;
				cin >> minimi;
				if (minimi == 1)
				{
					char nameboy;
					cout << "请输入插入的顶点名称" << endl;
					cin >> nameboy;
					G.vexs[G.vexnum++] = nameboy;
					int k = LocatedVex(G, nameboy);
					for (int i = 0; i < G.vexnum; i++)
					{
						G.arcs[i][k] == 0x3f3f3f3f;
						G.arcs[k][i] == 0x3f3f3f3f;
					}
					
				}
				else if (minimi == 2)
				{
					char nameboy1, nameboy2;
					int bilib;
					cout << "请输入要插入的两个点及其权值" << endl;
					cin >> nameboy1 >> nameboy2 >> bilib;
					int r, c;
					r = LocatedVex(G, nameboy1);
					c = LocatedVex(G, nameboy2);
					G.arcs[r][c] = G.arcs[c][r] =bilib;
					G.arcnum++;
				}
				else
				{
					cout << "请输入要插入的节点、和它连接的节点及权值" << endl;
					int zrx;
					cin >> b >> a >> zrx;
					insert(G, a, b, zrx);
					memset(visited, false, sizeof visited);
					//DFS_AM(G, 1);
					//memset(visited, false, sizeof visited);
				}
			}
			else if (secondselect == 2)
			{
				cout << "请输入要删掉的节点" << endl;
				cin >> a;
				del(G, a);
				memset(visited, false, sizeof visited);
				//DFS_AM(G, 1);
				memset(visited, false, sizeof visited);
			}
			else if (secondselect == 3)
			{
				memset(st, false, sizeof st);
				cout << "查询最短路径从校门口A到各个位置的最短路径" << endl;
				cout << "请输入你要查询的位置?" << endl;
				char m;
				cin >> m;
				int temp3 = 0;
				while (G.vexs[temp3] != m)
				{
					temp3++;
				}
				cout << dijkstra(G, temp3) << endl;
			}
			else if (secondselect == 4)
			{
				DFS_AM(G, 1);
				memset(visited, false, sizeof visited);
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		cout << "邻接表" << endl;
		CreatUDG(G2);
		//cout << G2.vexnum <<"  "<< G2.arcnum << endl;
		//cout << G2.vertices[0].firstarc->adjvex << endl;
		memset(visited2,false,sizeof visited2);
		DFS_AL(G2, 0);
		int path[35][35] = { 0 };
		int d[35][35] = { 0 };
		while (1)
		{
			cout << "请输入要执行的操作?1插入 2删除节点 3求最短路径 4遍历校园 5 结束" << endl;
			int select2;
			cin >> select2;
			if (select2 == 1)
			{
				int select23;
				cout << "请选择你的操作1插入节点2插入边" << endl;
				cin >> select23;
				if (select23 == 1)
				{
					char names;
					cout << "请输入你要插入的节点名称" << endl;
					cin>>names;
					insertvex2(G2, names);
				}
				else
				{
					char name1, name2;
					int weight;
					cout << "请输入你要插入的两个节点以及他们的权值" << endl;
					cin >> name1 >> name2 >> weight;
					insertarc(G2, name1, name2, weight);
				}
			}
			else if (select2 == 2)
			{
				char mimi;
				cout << "请输入要删除的节点" << endl;
				cin >> mimi;
				//cout << mimi << endl;
				del2(G2, mimi);
				memset(visited2, false, sizeof visited2);
				DFS_AL(G2,0);
			}
			else if(select2 ==4)
			{
				memset(visited2, false, sizeof visited2);
				DFS_AL(G2, 0);
			}
			else if (select2 == 3)
			{
				floyd(G2, path, d);
				cout << "path:" << endl;
				for (int i = 0; i < G2.vexnum; i++)
				{
					for (int j = 0; j < G2.vexnum; j++)
					{
						cout << G2.vertices[i].name << " " << G2.vertices[j].name<<" : ";
						if (d[i][j] == 0)
							cout << "∞" << endl;
						else
						cout << d[i][j] << endl;
					}
				}
			}
		}
	}
}
//by zrx###############################

注意 邻接矩阵实现最短路径方法为 迪杰斯特拉
邻接表实现 最短路径方法为 弗洛伊德

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个简单的C++代码示例,实现了一个基本的铁路订票系统: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义车票类 class Ticket { public: string trainName; string departure; string destination; string date; int seatNum; Ticket(string train, string depart, string dest, string d, int seat) { trainName = train; departure = depart; destination = dest; date = d; seatNum = seat; } void displayTicket() { cout << "Train: " << trainName << endl; cout << "Departure: " << departure << endl; cout << "Destination: " << destination << endl; cout << "Date: " << date << endl; cout << "Seat Number: " << seatNum << endl; } }; // 定义订票系统类 class TicketSystem { private: vector<Ticket> tickets; public: void bookTicket(string train, string depart, string dest, string date, int seat) { Ticket ticket(train, depart, dest, date, seat); tickets.push_back(ticket); cout << "Ticket booked successfully!" << endl; } void displayAllTickets() { for (int i = 0; i < tickets.size(); i++) { tickets[i].displayTicket(); cout << endl; } } }; int main() { TicketSystem system; system.bookTicket("G123", "Beijing", "Shanghai", "2022-01-01", 10); system.bookTicket("G456", "Shanghai", "Beijing", "2022-01-02", 15); system.displayAllTickets(); return 0; } ``` 这个代码示例中,我们定义了一个`Ticket`类来表示车票,包含了车次、出发地、目的地、日期和座位号等信息。然后,我们定义了一个`TicketSystem`类来管理订票系统,其中包括了订票和展示所有车票的功能。 在`main`函数中,我们创建了一个`TicketSystem`对象`system`,然后使用`bookTicket`方法来订购两张车票,并使用`displayAllTickets`方法展示所有的车票信息。 这只是一个简单的示例,实际的铁路订票系统可能需要更复杂的功能和数据结构来支持更多的操作和信息管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值