📄 congsim.cpp
字号:
#include "stdio.h"
#include "iostream.h"
#include "fstream.h" //输入输出流
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define mvnum 100 //最大顶点数
#define minint 0
#define maxnum 1600
#define M 0
#define N 2
//#define R q/c //路阻参数R=q/c,即流量通行能力之比
typedef char vertextype;
typedef int adjmatrix;
struct mgraph
{
vertextype vexs[mvnum]; //顶点数组
adjmatrix arcs[mvnum][mvnum]; //邻接矩阵
};
int d[mvnum][mvnum],p[mvnum][mvnum];
////////////
//路网构造方法函数
void CreateMgraph(mgraph *G)
{
//采用邻接矩阵表示法构造无向图G
int i,j,k,w;
ifstream graphlist("map.txt",ios::in );//定义一个流对象,并且和文件关联
for(i=1;i<=9;i++)
G->vexs[i]=(char)i;
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
G->arcs[i][j]=minint; //初始化邻接矩阵
for(k=1;k<=12;k++)
{
//从磁盘文件接受数据构造网
graphlist>>i;
graphlist>>j;
graphlist>>w;
G->arcs[i][j]=w;
G->arcs[j][i]=G->arcs[i][j]; //构造无向网
}
//cout<<"图的存储结构建立完毕!"<<endl;
graphlist.close(); //关闭文件
}
//道路类型定义
struct Result{
double x;
double y;
int z;
};
struct Result RoadType(int Flag)
{
struct Result r;
if (Flag==1)
{
r.x=1.726;
r.y=3.15;
r.z=68;
}
else if (Flag==2)
{
r.x=2.076;
r.y=2.870;
r.z=56;
}
return r;
}
//测试代码,产生区间随机数
/*int main(void)
{
int i,Y=99,X=0,k;
time_t t;
srand((unsigned) time(&t));
cout<<"Ten random numbers from 0 to 99\n\n";
for(i=0; i<10; i++)
{k=rand()%(Y-X+1)+X;
printf("%d\n", k);
}
return 0;
}*/
//自定义的随机数产生函数
int MyRand(int X,int Y)
{
int k;
time_t t;
srand((unsigned) time(&t));
k=rand()%(Y-X+1)+X; //产生[X,Y]之间的随机数
return(k);
}
//随机产生车的数量及速度并计算其平均速度返回
double CarSpeed(int Flag)
{
int i=0,k=0;
int carnum,V0;
double carspeed,sum=0;
time_t t;
struct Result r;
srand((unsigned) time(&t));
carnum=MyRand(0,1600);
//cout<<carnum<<endl; //随机产生车辆的数量
r=RoadType(Flag);
V0=r.z;
carspeed=MyRand(0,V0);
//carspeed=rand()%(V0+1);
//cout<<carspeed<<endl;
/*for (i=0;i<=carnum;i++)
{
k=rand()%V0; //随机产生车辆的速度
//cout<<k<<endl;
sum+=k;
}
carspeed=sum/carnum; */ //该路段上车辆的时间平均速度
//cout<<carspeed<<endl;
return(carspeed);
}
//测试代码 二分法求解非线性方程
/*float Erfenfa(float m,float n)
{
float x0,x1,x2,fx0,fx1,fx2;
do
{ x1=m;x2=n;
fx1=x1*x1*x1-x1-1;
fx2=x2*x2*x2-x2-1;
}
while(fx1*fx2>0);
do
{ x0=(x1+x2)/2;
fx0=x0*x0*x0-x0-1;
if((fx0*fx1)<0)
{ x2=x0;
fx2=fx0;
}
else
{ x1=x0;
fx1=fx0;
}
}
while(fabs(fx0)>=0.0001);
return x0;
}*/
//非线性方程求解算法
double Erfenfa(double V,double a,double b,int V0)
{
double n0,n1,n2;
double x0,x1,x2,fx0,fx1,fx2;
do
{ x1=M;x2=N;
n1=a+b*pow(x1,3);
fx1=V0/(1+pow(x1,n1))-V;
n2=a+b*pow(x2,3);
fx2=V0/(1+pow(x2,n2))-V;
}
while(fx1*fx2>0);
do
{ x0=(x1+x2)/2;
n0=a+b*pow(x0,3);
fx0=V0/(1+pow(x0,n0))-V;
if((fx0*fx1)<0)
{ x2=x0;
fx2=fx0;
}
else
{ x1=x0;
fx1=fx0;
}
}
while(fabs(fx0)>=0.0001);
return x0;
}
//利用交通流理论中的速度流量模型计算路阻参数R
double CalR(int Flag)
{
double R;
double V;
double a,b;
int V0;
struct Result r;
V=CarSpeed(Flag);
r=RoadType(Flag);
a=r.x;
b=r.y;
V0=r.z;
//cout<<a<<endl;
//cout<<b<<endl;
//cout<<V0<<endl;
//cout<<v<<endl;
R=Erfenfa(V,a,b,V0); //速度流量模型,利用速度反推流量
//cout<<R<<endl;
return(R);
}
//利用路阻参数判断交通流状态
void StaJud(int Flag)
{
double R;
R=CalR(Flag);
if (R<0.4)
{
cout<<"非常畅通";
cout<<" ";
}
else if (R>0.4&&R<0.6)
{
cout<<"畅通 ";
cout<<" ";
}
else if (R>0.6&&R<0.75)
{
cout<<"一般畅通";
cout<<" ";
}
else if (R>0.75&&R<0.9)
{
cout<<"拥堵 ";
cout<<" ";
}
else if(R>0.9&&R<1.0)
{
cout<<"非常拥挤";
cout<<" ";
}
else if (R>1.0)
{
cout<<"堵塞 ";
cout<<" ";
}
else
{
cout<<"DO NOTHING"<<endl;
}
}
//路网操作函数,单条路段上仿真拥堵状况
void RoadSinOper(mgraph *G,int v,int w)
{
int i,j;
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
{
if(G->arcs[i][j]!=minint)
p[i][j]=j;
else
p[i][j]=0;
d[i][j]=G->arcs[i][j]; //路段的权值即道路类型存入数组
}
if (d[v][w]==0)
{
cout<<"无此路段,重新输入!"<<endl;
}
else
{
StaJud(d[v][w]);
cout<<""<<endl;
}
}
//显示整个路网每条路段拥堵
void RoadAllOper(mgraph *G)
{
int i,j;
int count=0;
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
{
if(G->arcs[i][j]!=minint)
p[i][j]=j;
else
p[i][j]=0;
d[i][j]=G->arcs[i][j];
}
for(i=1;i<=9;i++)
for(j=i;j<=9;j++)
{
if (d[i][j]==0)
{
cout<<"";
continue;
}
else
{
if (count%4==0)
{
cout<<""<<endl;
}
cout<<"节点"<<i;
cout<<"->节点"<<j;
StaJud(d[i][j]);
count++;
}
}
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
}
//主函数,显示整个路网每个路段的拥堵状况及查询特定路段拥堵
void main()
{
mgraph *G;
int v,w,k;
int xz=1;
G=(mgraph *)malloc(sizeof(mgraph)); //为路网分配存储空间
cout<<"欢迎进入道路网实时拥堵状态显示查询系统."<<endl;
cout<<endl;
cout<<"节点名称及编号如下"<<endl;
cout<<" 1 节点1 2 节点2 3 节点3 "<<endl;
cout<<" 4 节点4 5 节点5 6 节点6 "<<endl;
cout<<" 7 节点7 8 节点8 9 节点9 "<<endl;
CreateMgraph(G);
while(xz!=0)
{
cout<<"**********实时拥堵状态显示**********"<<endl;
cout<<"================================"<<endl;
cout<<"请选择:1 显示 2 查询 0 结束"<<endl;
cout<<"================================"<<endl;
cin>>xz;
cout<<endl;
if(xz==2)
{
cout<<"请输入源点和终点: "<<endl;
cin>>v>>w;
k=G->arcs[v][w];
// cout<<k<<endl;
cout<<"该路段的拥堵状态:"<<endl;
RoadSinOper(G,v,w);
}
else if (xz==1)
{
RoadAllOper(G);
}
else if (xz==0)
{
cout<<"谢谢使用,祝您生活愉快!"<<endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -