寫在前面:沒有進行非常難的代碼編寫,僅僅是算法思想的抽象實現
街道模型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;
}