校园导游服务咨询系统C++源代码

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define N 100 //服务个数最大100
#define Road 1000 //定义路径为1000
int Num=9; //目前服务个数
using namespace std;
typedef struct Elemtype
{
char name[30];
char number[10];
char introduce[200];
}Elemtype;
typedef struct Place
{
int num; //位置序号
Place *next;
Elemtype date; //服务信息
}Place; //定义服务
typedef struct MGraph
{
Place place;
int legs[N][N]; //存放路径长

}MGraph;
MGraph MGr; //全局变量,定义MGr为MGraph类型
int shortest[N][N]; //定义全局变量存贮最小路径
int path[N][N]; //定义存贮路径
Place *p;

void init(Place *&head) //定义服务位置
{
Place *p,*q;
head=(Place *)malloc(sizeof(Place));
head->next=NULL;
q=head;

p=(Place *)malloc(sizeof(Place));
p->num=1;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"购物");
strcpy(p->date.number,"1");
strcpy(p->date.introduce,"位于学校北公寓西行30M处,有两层楼大小,货物十分齐全,地处学校门口,是同学们常去的地方,提供各种购物的服务。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=2;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"上网");
strcpy(p->date.number,"2");
strcpy(p->date.introduce,"位于学校北公寓东行20M处,有两个店铺可供选择,生意火爆,提供上网服务。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=3;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"洗浴");
strcpy(p->date.number,"3");
strcpy(p->date.introduce,"位于学校北公寓南行650M处,是学生们经常去的地方,适合平民消费,价格实惠,水温适度,但距离学校较远。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=4;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"清真小吃");
strcpy(p->date.number,"4");
strcpy(p->date.introduce,"位于学校北公寓南行400M处,作为老店,很受附近居民的欢迎,可提供烧烤,凉菜等服务,适合夜晚在此就餐。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=5;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"打印文件");
strcpy(p->date.number,"5");
strcpy(p->date.introduce,"位于学校北公寓东行80M处,提供打印文件、制作光盘等服务,价格较实惠,营业时间

较长。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=6;
strcpy(p->https://www.360docs.net/doc/5015005306.html,
,"火锅");
strcpy(p->date.number,"6");
strcpy(p->date.introduce,"位于学校北公寓南行150M处,作为试营业的商铺,服务态度较好,菜种齐全,适合多人聚餐。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=7;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"早餐");
strcpy(p->date.number,"7");
strcpy(p->date.introduce,"位于学校北公寓西行300M处,只有早上开放提供服务,其他时间段不营业,菜系有油条、豆浆等。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=8;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"快递");
strcpy(p->date.number,"8");
strcpy(p->date.introduce,"位于学校北公寓南行300M处,可获取收发韵达、申通快递等服务。");
p->next=q->next;
q->next=p;
q=p;

p=(Place *)malloc(sizeof(Place));
p->num=9;
strcpy(p->https://www.360docs.net/doc/5015005306.html,,"照相");
strcpy(p->date.number,"9");
strcpy(p->date.introduce,"位于学校北公寓南行100M处,可获取快照、艺术照、证件照等服务。");
p->next=q->next;
q->next=p;
q=p;


int i,j; //定义服务间的距离
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
MGr.legs[i][j]=Road;
}
}

for(i=1;i<=N;i++)

shortest[i][j]=0;

MGr.legs[0][1]=MGr.legs[1][0]=30;
MGr.legs[0][2]=MGr.legs[2][0]=19;
MGr.legs[0][3]=MGr.legs[3][0]=100;
MGr.legs[3][4]=MGr.legs[4][3]=50;
MGr.legs[3][5]=MGr.legs[5][3]=20;
MGr.legs[4][5]=MGr.legs[5][4]=15;
MGr.legs[5][6]=MGr.legs[6][5]=18;
MGr.legs[6][7]=MGr.legs[7][6]=24;
MGr.legs[1][2]=MGr.legs[2][1]=50;
MGr.legs[2][7]=MGr.legs[7][2]=20;
MGr.legs[8][7]=MGr.legs[7][8]=16;
MGr.legs[7][9]=MGr.legs[9][7]=26;
}

void folyd() //佛洛依德算法
{
int i,j,k;

for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
shortest[i][j]=MGr.legs[i][j];
path[i][j]=0;
}
}
for(k=1;k<=N;k++)
{
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
if(shortest[i][j]>(shortest[i][k]+shortest[k][j]))
{
shortest[i][j]=shortest[i][k]+shortest[k][j];
path[i][j]=k;
path[j][i]=k; //记录经过的路径
}
}
}
}
}

void display(int i,int j) //输出两个服务的路径及最短路径
{
int a,b;
a=i;
b=j;

cout<<"我们向您推荐的最短路径是: ";
if(i<j)
{
cout<<a;
while(path[i][j]!=0)
{
cout<<"->"<<path[i][j];
if(i<j)
j=path[i][j];
else
i=path[j][i];
}
cout<<"->"<<b<<endl;
cout<<" "<<endl;
cout<<"您所

查找的"<<a<<"->"<<b<<"两个服务之间的最短距离为:"<<shortest[a][b]<<"米。"<<endl;
}
else
{
cout<<a;
while(path[i][j]!=0)
{


cout<<"->"<<path[i][j];
if(i<j)
j=path[i][j];
else
i=path[j][i];
}
cout<<"->"<<b<<endl;
cout<<"您所查找的"<<a<<"->"<<b<<"两个服务之间的最短路径是:"<<shortest[a][b]<<" 米。"<<endl;

}
cout<<" "<<endl;
}

int shortdistance(Place *head) //查找两服务之间最短路径
{
int i,j;
Place *t=head->next,*q=head->next;
cout<<"请输入要查询的两个服务的位置序号:"<<endl;
cin>>i>>j;
while(t)
{
if(t->num==i)break;
else t=t->next;
}
while(q)
{
if(q->num==j)break;
else q=q->next;
}
if(q==NULL||t==NULL)
cout<<"抱歉,没有通向的路径!"<<endl;

else
{
folyd();
display(i,j);
}
cout<<endl;
return 1;

}

void add(Place *&head) //增加服务
{
Place *p,*t=head->next;
p=(Place *)malloc(sizeof(Place));
int a;
int b;
int x;
int y;
int i;
cout<<"请输入添加的服务位置(需输入大于9的整数)。";
cin>>p->num;
cout<<endl;
while(t)
{
if(t->num==p->num)
{
cout<<"此位置已被其它服务占用,请重新输入其它空白的位置。"<<endl;
cout<<" "<<endl;
cin>>p->num;continue;
}
else t=t->next;
}
if(t==NULL)
{
cout<<"服务编号:";
cin>>p->date.number;
cout<<"服务名字:";
cin>>p->https://www.360docs.net/doc/5015005306.html,;
cout<<"服务简介:";
cin>>p->date.introduce;
x=p->num;
cout<<endl;
cout<<"请输入路径条数、相关服务的位置y、路径长度"<<endl;
cin>>a;
cout<<endl;
for(i=1;i<=a;i++)
{
cin>>y;
cin>>b;
MGr.legs[x][y]=MGr.legs[y][x]=b;
}
cout<<"服务位置:"<<p->num<<endl;
cout<<"服务编号:"<<p->date.number<<endl;
cout<<"服务名称:"<<p->https://www.360docs.net/doc/5015005306.html,<<endl;
cout<<"服务简介:"<<p->date.introduce<<endl;
cout<<" "<<endl;
Place *q=head->next;
while(q->next)
{
q=q->next;
}
p->next=q->next;
q->next=p;
Num++;
}
}

void deleted(Place *&head) //删除服务
{
int text=0;
char cnum[10];
Pla

ce *p=head;
cout<<"输入删除的服务编号:"<<endl;
cout<<" "<<endl;
cin>>cnum;
while(p->next)
{
if(strcmp(p->next->date.number,cnum)==0)
{
cout<<"恭喜您,您已成功将服务 "<<p->next->https://www.360docs.net/doc/5015005306.html,<<" 关闭!"<<endl;
p->next=p->next->next;
Num--;
break;
}
else
{
if(p->next==NULL)

cout<<"该服务不存在!"<<endl;
cout<<" "<<endl;
p=p->next;

}
}

cout<<endl;
}


void find(Place *&head) //查询服务
{
int n;
Place *p=head;
cout<<"输入查询的服务位置序号:";
cout<<" "<<endl;
c
in>>n;

while(p->next)
{
if(p->next->num==n)
{
cout<<"服务位置:"<<p->next->num<<endl;
cout<<"服务编号:"<<p->next->date.number<<endl;
cout<<"服务名称:"<<p->next->https://www.360docs.net/doc/5015005306.html,<<endl;
cout<<"服务简介:"<<p->next->date.introduce<<endl;
break;
}
else p=p->next;

}
if(p->next==NULL)

cout<<"该服务不存在!"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
}

void replace(Place *&head) //修改服务
{

char name1[20];
char number1[10],number2[10];
char introduce1[100];

Place *p=head;

cout<<"输入要修改的的服务编号:"<<endl;
cout<<" "<<endl;
cin>>number1;
while(p->next)
{
if(strcmp(p->next->date.number,number1)==0)

{

cout<<"请输入新的服务名称:";
cin>>name1;
cout<<"请输入新的服务编号:";
cin>>number2;
cout<<"请输入新的服务简介:";
cin>>introduce1;
strcpy(p->next->https://www.360docs.net/doc/5015005306.html,,name1);
strcpy(p->next->date.number,number2);
strcpy(p->next->date.introduce,introduce1);
cout<<endl;
cout<<"服务位置:"<<p->next->num<<endl;
cout<<"服务编号:"<<p->next->date.number<<endl;
cout<<"服务名称:"<<p->next->https://www.360docs.net/doc/5015005306.html,<<endl;
cout<<"服务简介:"<<p->next->date.introduce<<endl;
c

out<<endl;
break;
}
else p=p->next;
}

if(p->next==NULL)
cout<<"该服务不存在!"<<endl;
cout<<" "<<endl;
cout<<endl;
}

void menu() //主菜单

{ cout<<" "<<endl;
cout<<" "<<endl;
cout<<"1 查询服务请按1键; "<<endl;

cout<<"2 修改服务请按2键; "<<endl;

cout<<"3 增加服务请按3键; "<<endl;

cout<<"4 删除服务请按4键 "<<endl;

cout<<"5 路径搜索请按5键 "<<endl;

cout<<"6 退出系统请按6键; "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;


}
void main() //主函数
{

cout<<" 欢迎使用衡水学院服务查询系统 "<<endl;
cout<<" "<<endl;
cout<<endl;
int x;
init(p);
while(1)
{
cout<<"当前存在的服务个数为:"<<Num<<endl;
cout<<endl;
menu();
cin>>x;
switch(x)
{

case 1:find(p); break;
case 2:replace(p);break;
case 3:add(p);break;
case 4:deleted(p);break;

case 5:shortdistance(p);break;
case 6:

cout<<" 恭喜您,成功退出系统! "<<endl;

exit(0);
}

}

}


相关文档
最新文档