// work8.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
#include <fstream>
using namespace std;
const int SIGHTS = 10;
const int PATHES = 11;
/*************************************/
typedef struct
{
char name[40];
int number;
char info[1000];
}Scenery;
typedef struct
{
int sight1, sight2;
int distance;
}Path;
typedef struct
{
int flag;// flag: 1-直接就是最短路径; 2-通过其他点存在最短路径.
int midd;
unsigned int distance;
}Map;
/*************************************/
Map map[SIGHTS][SIGHTS];
Scenery sights[SIGHTS];
Path paths[PATHES];
/*************************************/
void Initial();
int FindSight(char *sightName);
void QuerySight();
void ShowSights();
void ShowShortestPath(int st1, int st2);
void QueryPath();
/*************************************/
/*************************************/
int main()
{
char choice;
char choose='y';
Initial();
while (true)
{
cout << endl;
if(choose=='y'||choose=='Y'){
ShowSights();
cout << endl;
cout <<"****************"<<endl;
cout << "(1)景点查询" << endl;
cout << "(2)路径查询" << endl;
cout << "(0)退出" << endl;
cout << "请选择: "<<endl;
cout <<"****************"<<endl;
if ((choice = getchar()) == 10) continue;
getchar();
cout << endl;
switch (choice)
{
case '1':
QuerySight();
cout << "continue?(y/n)"<<endl; cin >> choose;
getchar(); break;
case '2':
QueryPath();
cout << "continue?(y/n)"<<endl; cin >>choose;
getchar();
break;
case '0':
return 0;
default:
cout << "输入错误!请重新输入..." << endl << endl;
}
} else return 0;
}
return 0;
}
/*****************************************************************/
void Initial()
{
ifstream fin("sight.txt");
int n;
if (fin == NULL)
{
printf("open file sight.txt failed...\n");
exit(1);
}
for (int i = 0; i < SIGHTS; i++)
{
for (int j = 0; j < SIGHTS; j++)
{
map[i][j].distance = INT_MAX;
map[i][j].flag = 0;
}
}
for (i = 0; i < SIGHTS; i++)
{
map[i][i].distance = 0;
map[i][i].flag = 1;
}
fin >> n;
for (i = 0; i < n; i++)
{
fin >> sights[i].name >> sights[i].number;
fin.get(); fin.get(); fin.get();
fin.getline(sights[i].info, 1000);
}
fin >> n;
for (i = 0; i < n; i++)
{
fin >> paths[i].sight1 >> paths[i].sight2 >> paths[i].distance;
map[paths[i].sight1][paths[i].sight2].distance = paths[i].distance;
map[paths[i].sight2][paths[i].sight1].distance = paths[i].distance;
map[paths[i].sight2][paths[i].sight1].flag = 1;
map[paths[i].sight1][paths[i].sight2].flag = 1;
}
// calculate the shortest path.
for (int k = 0; k < SIGHTS; k++)
{
for (i = 0; i < SIGHTS; i++)
{
for (int j = 0; j < SIGHTS; j++)
{
if (map[i][j].distance > (map[i][k].distance + map[k][j].distance))
{
map[i][j].distance = map[j][i].distance = map[i][k].distance + map[k][j].distance;
map[i][j].flag = map[j][i].flag = 2;
map[i][j].midd = map[j][i].midd = k;
}
}
}
}
fin.close();
}
int FindSight(int sightNo)
{
for (int i = 0; i < SIGHTS; i++)
{
if ((sightNo-sights[i].number) == 0) return i;
}
return -1;
}
void QuerySight()
{
int s;
int sightNo;
cout << "请输入景点号码: "; cin >> sightNo;
getchar();
if ((s = FindSight(sightNo)) != -1)
{
cout << endl << sights[s].name << "\tNo. " << sights[s].number << endl;
cout << sights[s].info << endl;
}
else
{
cout << "This place doesn't exist." << endl;
}
}
void ShowSights()
{
for (int i = 0; i < SIGHTS; i++)
{
cout << sights[i].name << " No. " << sights[i].number << endl;
}
}
void ShowShortestPath(int st1, int st2)
{
if (map[st1][st2].flag == 1)
{
//cout << " " << sights[st2].name;
//cout << " " << sights[st1].name << endl;
return;
}
else if (map[st1][st2].flag == 0)
{
cout << "\nNo path..." << endl;
return;
}
else
{
cout << " " << sights[map[st1][st2].midd].name;
ShowShortestPath(st1, map[st1][st2].midd);
ShowShortestPath(map[st1][st2].midd, st2);
}
}
void QueryPath()
{
int st1, st2;
int no1,no2;
while (true)
{
cout << "起点(请输入景点号): ";
cin >> no1;
if ((st1 = FindSight(no1)) != -1) break;
}
while (true)
{
cout << "终点(请输入景点号): ";
cin >> no2;
if ((st2 = FindSight(no2)) != -1) break;
}
switch (map[st1][st2].flag)
{
case 0:
cout << "No path..." << endl;
return;
case 1:
cout << "the shortest way is from"<<sights[st1].name<<"to"<<sights[st2].name<< endl;
break;
case 2:
cout << "places on the path:" << endl;
ShowShortestPath(st1, st2);
break;
default: break;
}
cout << endl << "Distance: " << map[st1][st2].distance << endl;
}
10
学校大门 00 两侧有保卫室,正对面有一塑陈毅雕像
行政大楼 01 教师进行会议和办公,以及学生上机的地方
喷泉 02 一道漂亮的风景线
第一教学楼 03 学生学习上课的大楼,有7层高
运动场 04 塑胶跑道,宽敞的足球场
学生活动中心 05 一楼为学生就餐的地方,二楼是进行各种活动的地方
学生宿舍 06 学生住的地方
教师公寓 07 高大的电梯公寓,是教师居住的地方
葡萄园 08 休息与娱乐聚会的地方
游泳池 09 和适合游泳运动的场所
11
00 02 200
01 00 100
08 09 50
08 06 300
05 01 400
04 09 300
03 02 60
07 09 250
02 06 120
06 07 160
04 07 330
|