写在前面:没有进行非常难的代码编写,仅仅是算法思想的抽象实现
街道模型car类
由于我对matlab的了解很少,所以这里采用vs c++实现的街道模型,主要有三个类
首先是car类,是对街道上的车辆和车流浪进行的模拟,包含的元素有
车辆的名称,或者说ID,用来指代车辆,
当前的车流量,陷于代码能力,目前只能用一个定值来表示,后续可以通过函数的方式,实现一个变量的效果
contectname,用来表示想要和谁建立连接
以及flag,用来表示当前状态,是否建立连接等等
属性后续可以继续加入,以达到更加仿真的模型
街道模型node类
然后是node类,用来表示地图中的城市,包含的元素有
城市的名称,或者说ID,用来指代城市,
与城市相连的城市数目
每条道路的长度
每条道路的角度
每条道路的名称
属性后续可以继续加入,以达到更加仿真的模型
街道模型map类
然后是map类,用来表示地图,包含的元素有
城市的数量
以及每个城市
后续可以加入城市的分区、程序的大小之类的属性
c++代码
#include <iostream>
#include "string.h"
using namespace std;
class nodecar {
string name = "";
string contectname = "";
};
class car {
string name = "";
double liuliang = 0;
string contectname = "";
bool flag = false;
void inp(string name1, double ll, string cname, bool flag1) {
name = name1;
liuliang = ll;
contectname = cname;
flag = flag1;
}
};
class node {
public:
string name = "";
int num = 0;
double length [99];
double degree [99];
string names [99];
/*在这里加属性,包括街区、建筑物、车流量等*/
void inp(string name1,int num1) {
name = name1;
num = num1;
for (int i = 0; i < num1; i++) {
cout << "请输入每条道路的目的地、长度、角度" << endl;
cin >> names[i] >> length[i] >> degree[i];
}
}
void po() {
for (int i = 0; i < num; i++) {
cout << "城市:" << names[i] << " 相距:" << length[i] << " 角度为:" << degree[i] << endl;
}
}
};
class map {
public:
int n = 0;
node city[99];
void inp(int n1) {
n = n1;
string na;
int nu;
for (int i = 0; i < n; i++) {
cout << "请依次输入城市和与之相连的个数" << endl;
cin >> na >> nu;
city[i].inp(na, nu);
}
}
void cou() {
for (int i = 0; i < n; i++) {
cout << "城市" << city[i].name;
cout << "与以下城市相连" << endl;
city[i].po();
cout << endl;
}
}
};
int main()
{
map m;
int nc = 0;
cout << "请输入城市个数" << endl;
cin >> nc;
m.inp(nc);
m.cou();
return 0;
}