📄 xydy.c
字号:
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define SIZE_view 20
#define SIZE_way 200
/*定义所需所有结构*/
struct view_info /*景点信息结构*/
{
char name[20];
char info[100];
int x;
int y;
int a;
int b;
} views[SIZE_view];
struct way_info /*路径信息结构*/
{
int start_id;
int end_id;
int dist;
} ways[SIZE_way];
struct path_info /*路径查询结构*/
{
int count;
int path[SIZE_view];
};
int view_count, way_count;/*景点数,边数*/
main()/*主函数*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
ReadViewsFile();
ReadWaysFile();
menu();
printf("\n");
printf("Quiting\n");
}
void map()/*绘地图*/
{ int i; char str[20];
setbkcolor(7);
setcolor(6);
/*地图匡架*/
rectangle(2,2,638,478);
line(360,2,360,478);
rectangle(360,100,500,240);
rectangle(500,100,638,478);
setfillstyle(1,1);
setcolor(3);
/*绘出学校的主干道路*/
bar(25,150,32,364);
bar(25,198,240,206);
bar(96,88,104,414);
bar(2,358,216,366);
line(205,478,253,168);
line(216,478,264,168);
line(253,168,360,72);
line(264,168,360,83);
bar(176,358,180,414);
bar(140,386,360,394);
bar(96,268,360,276);
bar(334,258,342,478);
fillellipse(104,80,5,5);
fillellipse(415,460,5,5);
bar(432,280,440,400);
line(432,400,410,460);
line(440,400,420,460);
setcolor(5);
line(380,170,480,170);
line(430,120,430,220);
line(430,120,425,130);
line(430,120,435,130);
/*地图上的基本文字标识*/
outtextxy(440,120,"N");
outtextxy(35,266,"Playground");
outtextxy(85,75,"south enter of turl");
outtextxy(410,450,"north enter");
outtextxy(410,460,"of turl");
outtextxy(380,20,"Thank you for looking the map");
outtextxy(380,50,"If you need help,call:63168540");
outtextxy(20,20,"Map 1");
outtextxy(370,250,"Map 2");
outtextxy(370,110,"Zuobiao");
outtextxy(510,101,"Intruduction");
ReadViewsFile();/*读景点信息*/
for(i=0;i<view_count;i++)/*给出景点名称注释*/
{outtextxy(520,120+i*20,views[i].name);}
for(i=-1;i<view_count;i++) /*根据读出的坐标绘制景点*/
{ setfillstyle(1,2);setcolor(2);
bar(views[i+1].x-views[i+1].a,views[i+1].y-views[i+1].b,views[i+1].x+views[i+1].a,views[i+1].y+views[i+1].b);
}
for(i=0;i<view_count;i++)/*在地图上标注景点号*/
{ setcolor(4);
sprintf(str,"%d",i+1);
outtextxy(views[i].x-5,views[i].y-5,str);
outtextxy(503,110+i*20,str);
}
getch();
closegraph();
return(menu());
}
passw()/*管理员密码验证*/
{ int pword;
printf("请输入登陆密码:");
scanf("%s",&pword);
if(pword=='l')
manage();
else
printf("错误,重试,");
passw();
}
manage()/*管理员操作界面*/
{ int mg_menu;
printf(" 1-------- 添加景点\n"); /*增加景点*/
printf(" 2-------- 删除景点\n"); /*删除景点*/
printf(" 3-------- 添加路径\n"); /*增加路径*/
printf(" 4-------- 删除路径\n"); /*删除路径*/
printf(" 0--------返回\n");/**/
scanf("%d",&mg_menu);
if(mg_menu==1) AddView();
else if (mg_menu==2) DelView() ;
else if(mg_menu==3) AddWay();
else if(mg_menu==4) DelWay();
else if(mg_menu==0) return(menu());
}
menu()/*主菜单*/
{int c_menu;
clrscr();cleardevice();
printf("欢迎使用地质大学校园导游系统\n");
printf("\n");
printf("\n");
printf("\n");
printf("你可以:\n");
printf(" 1 -------- 查询景点信息\n"); /*显示景点信息*/
printf(" 2 -------- 查询最佳路径\n"); /*寻找最佳路径*/
printf(" 3 -------- 查看地图\n");/*查看地图*/
printf(" 4 -------- 管理员系统\n");/*管理员登陆*/
printf(" 请选择您需要的服务: ");
scanf("%d", &c_menu);
switch(c_menu)
{
case 1:
SelectView();
break;
case 2:
SearchWay();
break;
case 3:
map();
break;
case 4:
passw();
break;
}
return;
}
ReadViewsFile()
{
int i;
FILE *fp;
view_count = 0;
if ((fp = fopen("VIEWS", "rb")) == NULL) /*读取VIEWS文件*/
{
/* printf(" FAILED TO OPEN THE FILE");
exit(0); */
return;
}
for (i = 0; i < SIZE_view; i++)
{
if (fread(&views[i], sizeof (struct view_info), 1, fp) == 1)
view_count = view_count + 1;
else
break;
}
fclose(fp);
}
ReadWaysFile()
{
int i;
FILE *fp;
way_count = 0;
if ((fp = fopen("WAYS", "rb")) == NULL) /*读取WAYS文件*/
{
return;
}
for (i = 0; i < SIZE_way; i++)
{
if (fread(&ways[i], sizeof (struct way_info), 1, fp) == 1)
way_count = way_count + 1;
else
break;
}
fclose(fp);
}
AddView()/*添加景点*/
{
int i;
int count;
FILE *fp;
struct view_info new_view;
printf("Adding a view...n");
if ((fp = fopen("VIEWS", "ab")) == NULL) /*读取VIEWS文件*/
{
printf(" Open file VIEWS failed.n");
exit(0);
}
printf(" The name of new view: ");
scanf("%s", new_view.name);
printf(" The info of new view: ");
scanf("%s", new_view.info);
printf("The locate of the view (x,y,a,b):");
scanf("%d,%d,%d,%d",&new_view.x,&new_view.y,&new_view.a,&new_view.b);
if (fwrite(&new_view, sizeof (struct view_info), 1, fp) == 1)
{
views[view_count] = new_view;
view_count = view_count + 1;
printf("Add a view successful.\n");
}
else
printf(" Write error.n");
fclose(fp);
printf("\n");
return;
}
/*删除景点,同时遍历路径表,将大于所删序号的编号减小1*/
DelView()
{
int i, select_id;
FILE *fp;
printf("Deleting a view...\n");
select_id = 1;
while (select_id != 0)
{
if (view_count == 0)
{
printf(" There is not any view. You can’t delete any view.\n");
return;
}
printf(" All views in the school:n");
for (i = 0; i < view_count; i++)
printf(" %d: %sn", i + 1, views[i].name);
printf(" Press 1-%d to delete a view, or press 0 back: ", view_count);
scanf("%d", &select_id);
if (select_id > 0 && select_id <= view_count)
{
view_count = view_count - 1;
for (i = select_id - 1; i < view_count; i++)
views[i] = views[i+1];
fp = fopen("VIEWS", "wb+");
for (i = 0; i < view_count; i++)
fwrite(&views[i], sizeof (struct view_info), 1, fp);
fclose(fp);
}
}
printf("\n");
return;
}
SelectView()/*查询景点信息*/
{
int i, select_id;
FILE *fp;
printf("正在搜索景点信息。。。。\n");
if (view_count == 0)
{
printf(" 没有景点.\n");
return;
}
printf(" 本校所有景点列表:\n");/*列出所有景点*/
for (i = 0; i < view_count; i++)
printf(" %d: %s\n", i+1, views[i].name);
select_id = 1;
while (select_id != 0)
{
printf(" Press 1-%d to select the info of view, or press 0 back: ", view_count);
scanf("%d", &select_id);
if (select_id > 0 && select_id <= view_count)/*查询景点信息*/
printf(" %s: %s\n", views[select_id-1].name, views[select_id-1].info);
if(select_id==0) menu();
}
printf("\n");
return;
}
AddWay()/*添加路径*/
{
int i;
int tmp_num;
FILE *fp;
struct way_info new_way;
printf("Adding a way...\n");
if (view_count == 0)
{
printf(" There is not any view. You can’t add a way.\n");
return;
}
printf(" All views in the school:\n");
for (i = 0; i < view_count; i++)
printf(" %d: %s\n", i+1, views[i].name);
if (view_count == 1)
{
printf(" There is only one view. You can’t add a way.\n");
return;
}
if ((fp = fopen("WAYS", "ab")) == NULL)
{
printf(" Open file WAYS failed.n");
exit(0);
}
tmp_num = 0;
while (1)
{
printf(" ID of 1st view: ");
scanf("%d", &tmp_num);
if (tmp_num == 0)
{
printf("nn");
return;
}
if (tmp_num > 0 && tmp_num <= view_count)
{
new_way.start_id = tmp_num - 1;
break;
}
else
printf(" Wrong ID! Please input 1-%d, or 0 to cancel.\n", view_count);
}
while (1)
{
printf(" ID of 2nd view: ");
scanf("%d", &tmp_num);
if (tmp_num == 0)
{
printf("nn");
return;
}
if (tmp_num > 0 && tmp_num <= view_count)
{
new_way.end_id = tmp_num - 1;
break;
}
else
printf(" Wrong ID! Please input 1-%d, or 0 to cancel.\n", view_count);
}
while (1)
{
printf(" Distance: ");
scanf("%d", &tmp_num);
if (tmp_num == 0)
{
printf("nn");
return;
}
if (tmp_num > 0)
{
new_way.dist = tmp_num;
break;
}
else
printf(" Error! Please input positive whole number, or 0 to cancel.\n");
}
if (fwrite(&new_way, sizeof (struct way_info), 1, fp) == 1)
{
ways[way_count] = new_way;
way_count = way_count + 1;
printf("Add a way successfully.");
}
else
printf(" Write error.\n");
fclose(fp);
printf("\n");
return;
}
DelWay()/*销毁路径*/
{
int i, select_id;
FILE *fp;
printf("Deleting a way...\n");
select_id = 1;
while (select_id != 0)
{
if (way_count == 0)
{
printf(" There is not any way. You can’t delete any way.\n");
return;
}
printf(" All ways in the school:\n");
for (i = 0; i < way_count; i++)
printf(" %d: from %s to %s, distance is %dn", i+1, views[ways[i].start_id].name,
views[ways[i].end_id].name, ways[i].dist);
printf(" Press 1-%d to delete a way, or press 0 to cancel: ", way_count);
scanf("%d", &select_id);
if (select_id > 0 && select_id <= way_count)
{
way_count = way_count - 1;
for (i = select_id - 1; i < way_count; i++)
ways[i] = ways[i+1];
fp = fopen("WAYS", "wb+");
for (i = 0; i < way_count; i++)
fwrite(&ways[i], sizeof (struct way_info), 1, fp);
fclose(fp);
}
}
printf("\n");
return;
}
SearchWay()
{
int select_id;
printf("正在查找最佳路径...\n");
if (view_count == 0)
{
printf(" 缺乏景点.\n");
return;
}
if (way_count == 0)
{
printf(" 缺乏路径.\n");
return;
}
printf(" 请选择:\n");
printf(" 1: 查找路径"); /*用弗洛伊德(Floyed)算法求取最佳路径*/
printf(" 0: 退出"); /*退出求取最佳路径*/
select_id = 1;
while (1)
{
printf(" 请选择: ");
scanf("%d", &select_id);
switch (select_id)
{
case 0:
return;
break;
case 1:
Floyed();
return;
break;
}
}
}
Floyed() /*弗洛伊德(Floyed)算法*/
{
int i, j, k, m, start_num, end_num;
int dist_list[SIZE_view][SIZE_view];
struct path_info path_list[SIZE_view][SIZE_view];
for (i = 0; i< view_count; i++)
for (j = 0; j< view_count; j++)
{
if (i == j)
{
dist_list[i][j] = 0; /*两点是同一点*/
continue;
}
dist_list[i][j] = -1;
path_list[i][j].count = 0;
for (k = 0; k< way_count; k++)
{
if (ways[k].start_id == i && ways[k].end_id == j)
{
dist_list[i][j] = ways[k].dist;
path_list[i][j].count = 2;
path_list[i][j].path[0] = i;
path_list[i][j].path[1] = j;
break;
}
}
}
for (k = 0; k< view_count; k++)
for (i = 0; i < view_count; i++)
for (j = 0; j< view_count; j++)
{
if (i == k || j == k || i == j)
continue;
if (dist_list[i][k] == -1 || dist_list[k][j] == -1)
continue;
if ((dist_list[i][j] == -1) || ((dist_list[i][j] != -1) &&(dist_list[i][k] + dist_list[k][j] < dist_list[i][j])))
{
dist_list[i][j] = dist_list[i][k] + dist_list[k][j];
path_list[i][j].count = path_list[i][k].count + path_list[k][j].count - 1;
for (m = 0; m < path_list[i][k].count; m++)
path_list[i][j].path[m] = path_list[i][k].path[m];
for (m = 0; m < path_list[k][j].count; m++)
path_list[i][j].path[m+path_list[i][k].count] = path_list[k][j].path[m+1];
}
}
printf(" 本校景点列表:\n");
for (i = 0; i < view_count; i++)
printf(" %d: %s\n", i+1, views[i].name);
printf(" 请输入您要查找的路径的起点和终点号: ");
scanf("%d,%d", &start_num, &end_num);
start_num = start_num - 1;
end_num = end_num - 1;
printf(" From %s to %s: ", views[start_num].name, views[end_num].name);
if (dist_list[start_num][end_num] == -1)
printf("无路径");
else
{
printf("距离: %d, 路径是: ", dist_list[start_num][end_num]);
k = path_list[start_num][end_num].path[0];
printf("%s", views[k].name);
for (m = 1; m < path_list[start_num][end_num].count; m++)
{
k = path_list[start_num][end_num].path[m];
printf(" -> %s", views[k].name);
}
}
printf("\n");
getch();
return(menu());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -