📄 公园导游.txt
字号:
#include "stdio.h"
#define SIZE_view 20
#define SIZE_way 300
struct view_info /*景点信息结构*/
{
char name[20];
char info[100];
} 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;
void ReadViewsFile()
{
int i;
FILE *fp;
view_count = 0;
if ((fp = fopen("VIEWS.TXT", "rb")) == NULL) /*读取VIEWS文件*/
{
printf(" Open file VIEWS failed.\n");
//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);
}
void ReadWaysFile()
{
int i;
FILE *fp;
way_count = 0;
if ((fp = fopen("WAYS.TXT", "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);
}
void AddView()
{
//int i;
//int count;
FILE *fp;
struct view_info new_view;
printf("Adding a view...\n");
if ((fp = fopen("VIEWS.TXT", "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);
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 successfully.");
}
else
printf(" Write error.\n");
fclose(fp);
printf("\n\n");
//return;
}
/*删除景点的同时,遍历路径表,将大于所删序号的编号减小1*/
void 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\n\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);
printf(" Press 1-%d to delete a view, or press 0 to cancel: ", 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\n");
//return;
}
void ShowView()
{
int i, select_id;
//FILE *fp;
printf("Showing the info of views...\n");
if (view_count == 0)
{
printf(" There is not any view.\n\n\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);
select_id = 1;
while (select_id != 0)
{
printf(" Press 1-%d to show the info of view, or press 0 to cancel: ", 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);
}
printf("\n\n");
//return;
}
void 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\n\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\n\n");
//return;
}
if ((fp = fopen("WAYS.TXT", "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("\n\n");
//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("\n\n");
//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("\n\n");
//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 view successfully.");
}
else
printf(" Write error.\n");
fclose(fp);
printf("\n\n");
//return;
}
void 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\n\n");
//return;
}
printf(" All ways in the school:\n");
for (i = 0; i < way_count; i++)
printf(" %d: from %s to %s, distance is %d\n", 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.TXT", "wb+");
for (i = 0; i < way_count; i++)
fwrite(&ways[i], sizeof (struct way_info), 1, fp);
fclose(fp);
}
}
printf("\n\n");
//return;
}
void ShowWay()
{
int i;
printf("Showing the info of ways...\n");
if (way_count == 0)
{
printf(" There is not any way.\n\n\n");
//return;
}
printf(" All ways in the school:\n");
for (i = 0; i < way_count; i++)
printf(" %d: from %s to %s, distance is %d\n", i+1, views[ways[i].start_id].name,
views[ways[i].end_id].name, ways[i].dist);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -