GraphOperation
#include "stdafx.h"
#include "iostream"
#include "string"
#include "io.h"
#include "math.h"
#include<stdio.h>
#define MAX_VERTEX_NUM 20
#define INFINITY 9999
using namespace std;
class GraphOperation
{
public:
struct city
{
string name;
int x;
int y;
};
int city_num;
int path_num;
city cities[MAX_VERTEX_NUM];
int distance[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
public:
GraphOperation()
{
int i, j;
for (i = 0;i < MAX_VERTEX_NUM;i++)
{
for (j = 0;j < MAX_VERTEX_NUM;j++)
{
distance[i][j] = INFINITY;
}
}
for (i = 0;i < MAX_VERTEX_NUM;i++)
{
distance[i][i] = 0;
}
//cout << "city_num:";
city_num = 12;
//cout << "path_num:";
path_num = 15;
freopen("F:\\数据结构综合实践\\ConsoleApplication1\\ConsoleApplication1\\city_data.txt", "r", stdin);
}
void Load_City_Data()
{
int i;
double j, k;
for (i = 0;i < city_num;i++)
{
cin >> cities[i].name;
cin >> j;
cin >> k;
cities[i].y = (int)j;
cities[i].x = (int)k;
}
}
int Search_City_Pos(string name)
{
int i;
for (i = 0;i < city_num;i++)
{
if (strcmp(cities[i].name.c_str(), name.c_str()) == 0)
{
return i;
}
}
return INFINITY;
}
int Calculate_City_Distance(string start_city, string end_city)
{
int i = Search_City_Pos(start_city);
int j = Search_City_Pos(end_city);
return Calculate_Distance(cities[i].x, cities[i].y, cities[j].x, cities[j].y);
}
int Calculate_Distance(int x1, int y1, int x2, int y2)
{
int i, j;
i = abs(x1 - x2);
j = abs(y1 - y2);
i *= i;
j *= j;
return (int)sqrt(static_cast<double> (i + j));
}
void Load_City_Path()
{
int i;
string start_city;
string end_city;
for (i = 0;i < path_num;i++)
{
cin >> start_city;
cin >> end_city;
int start_pos = Search_City_Pos(start_city);
int end_pos = Search_City_Pos(end_city);
distance[start_pos][end_pos] = Calculate_City_Distance(start_city, end_city);
distance[end_pos][start_pos] = Calculate_City_Distance(start_city, end_city);
}
}
void Calculate_Distance_Use_Floyd()
{
int i, j, k;
for (i = 0;i < MAX_VERTEX_NUM;i++)
{
for (j = 0;j < MAX_VERTEX_NUM;j++)
{
for (k = 0;k < MAX_VERTEX_NUM;k++)
{
if (distance[j][i] + distance[i][k] < distance[j][k])
{
distance[j][k] = distance[j][i] + distance[i][k];
}
}
}
}
}
void Show_City_Distance(string start_city)
{
int i = Search_City_Pos(start_city);
int j;
for (j = 0;j < MAX_VERTEX_NUM;j++)
{
if (distance[i][j] != INFINITY)
{
cout << endl << start_city << " to " << cities[j].name << " is " << distance[i][j] << endl;
}
}
}
};
city_data.txt
北京
39.55
116.24
兰州
36.04
103.51
广州
23.08
113.14
厦门
24.27
118.06
桂林
25.17
110.17
遵义
27.42
106.55
济南
36.40
117.00
郑州
34.46
113.40
哈尔滨
45.44
126.36
武汉
30.35
114.17
长沙
28.12
112.59
呼和浩特
40.48
111.41
北京
兰州
北京
桂林
北京
武汉
北京
长沙
桂林
广州
桂林
厦门
兰州
厦门
兰州
哈尔滨
广州
遵义
广州
哈尔滨
厦门
呼和浩特
呼和浩特
郑州
郑州
济南
遵义
长沙
遵义
武汉