综合评价——重力模型法 C++代码
时间: 2023-09-01 15:12:57 浏览: 94
重力模型法(Gravity Model)是一种常用的地理空间分析方法,用于预测人口流动、货物运输、信息传播等空间交互活动的强度和方向。以下是一个基于C++语言实现的重力模型法代码示例:
```c++
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int MAXN = 1000; // 最大节点数
const double G = 6.673e-11; // 万有引力常数
int n; // 节点数
double pos[MAXN][2]; // 节点位置坐标
double mass[MAXN]; // 节点质量
double weight[MAXN][MAXN]; // 重力权重
void gravity_model() {
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
double dx = pos[j][0] - pos[i][0];
double dy = pos[j][1] - pos[i][1];
double dis = sqrt(dx*dx + dy*dy);
weight[i][j] = weight[j][i] = G * mass[i] * mass[j] / (dis*dis);
}
}
}
int main() {
ifstream fin("input.txt"); // 输入文件
fin >> n;
for(int i = 0; i < n; i++) {
fin >> pos[i][0] >> pos[i][1] >> mass[i];
}
fin.close();
gravity_model(); // 计算重力权重矩阵
ofstream fout("output.txt"); // 输出文件
fout << n << endl;
for(int i = 0; i < n; i++) {
fout << pos[i][0] << " " << pos[i][1] << " " << mass[i] << " ";
for(int j = 0; j < n; j++) {
fout << weight[i][j] << " ";
}
fout << endl;
}
fout.close();
return 0;
}
```
该代码中,我们首先从文件中读入节点数、节点位置坐标和节点质量,并计算出节点间的重力权重。最后将结果输出到文件中。