⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 09.cpp

📁 计算机网络实验上的建立路由表的程序
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>

#define MAXPOINT 3
#define limit 32767
struct record{
	int number;
	int flag;
	int allpath;
}path[MAXPOINT+1];

int cost[MAXPOINT+1][MAXPOINT+1];
void main()
{
	int i,j,temp,front=1,rear=MAXPOINT,N,minnumber;
	int min=32768;
	for(i=1;i<=MAXPOINT;i++)
		for(j=1;j<=MAXPOINT;j++)
		{
			cout<<"请输入从第"<<i<<"点到第"<<j<<"点的路径长度如果没有路径的话请输入'32767' "<<endl;
		    cin>>cost[i][j];
		}

		//cout<<"请输入源点号"<<endl;
		//cin>>N;
		for(N=MAXPOINT;N>=1;N--)
		{
			for(i=front;i<=rear;i++)
			{
				if(i==N)
					path[i].allpath=0;
				else
					path[i].allpath=limit;
				path[i].flag=0;
				path[i].number=i;
			}
			while(rear>=1)
			{
				for(temp=front;temp<=MAXPOINT;temp++)
				{
					if(path[temp].allpath<min&&path[temp].flag==0)
					{
						minnumber=path[temp].number;
						min=path[temp].allpath;
					}
				}
				min=32768;
				path[minnumber].flag=1;
				for(i=1;i<=MAXPOINT;i++)
				{
					if((i!=minnumber)&&(path[minnumber].allpath+cost[minnumber][i]<path[i].allpath))
						path[i].allpath=path[minnumber].allpath+cost[minnumber][i];
				}
				rear--;
			}
			rear=MAXPOINT;
			cout<<"->"<<N<<"点路由表如下:"<<endl;
			cout<<"源节点   目的节点    下一条节点   路径长度"<<endl;
			for(j=1;j<=MAXPOINT;j++)
				cout<<"  "<<N<<"         "<<temp<<"            "<<j<<"           "<<path[j].allpath<<endl;
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -