c++简单的云南交通咨询系统

在这里插入代码片
#include	<iostream>
#include	 <cstdlib>
#include	 <cstring>
using namespace std;

typedef struct linkNode		
{
	int data;
	struct linkNode* next;
}Node;          //顶点结构体 

int vNum=0;						//记录点的数量
Node* nodes[30];				//建立邻接表
int status[30];						//记录节点访问状态
int queue[30];						//广度优先遍历时记录遍历序列
int queLen = 0;					//队列长度
int front = 0;						//队列头
int tail = 0;							//队列尾

void push(int element) {		//入队列操作
	queue[tail] = element;
	tail++;
	queLen++;
}

int pop() {							//出队列操作
	int res = queue[front];
	front++;
	queLen--;
	return res;
}

void insert(int v1, int v2) {						//建立邻接表时往链表里插入新的元素
	if (nodes[v1]== NULL) {									//若该元素没有记录,
		nodes[v1]= (Node*)malloc(sizeof(Node));	//将该元素放入新的空间
		nodes[v1]->data = v2;							//该元素指向“v2”
		nodes[v1]->next = NULL;							//将尾指针置空
	}
	else {													//若该元素已有记录
		Node* tmp = nodes[v1];								//使用临时指针指向该元素
		while (tmp->next != NULL) {							//遍历找到以该元素为头结点的链表的尾节点
			tmp = tmp->next;
		}
		tmp->next = (Node*)malloc(sizeof(Node));	
		tmp = tmp->next;
		tmp->data = v2;
		tmp->next = NULL;
	}
}

void graphCreat() {													//创建邻接表
	cout << "城市的数量:" << endl;
	vNum=16;
	cout<<vNum<<endl;

	cout << "请输入高速路的数量:" << endl;   //管理员登录后输入高速路的数量并且录入高速公路网信息 
	int m = 0;
	cin >> m;

	int x = 0;
	int y = 0; 
	int i;
	cout<<"请录入高速公路信息:"<<endl;
	for (i = 1; i <=m; i++) {									//重复执行录入操作
		cout << "Line "<<i<<": ";
		cin >> x >> y;
		insert(x, y);											//无向图需要重复录入
		insert(y, x);
	}
	cout<<"高速公路网图信息获取成功!"<<endl;  //高速公路图网建立成功,输出各州市代号 
	cout<<"各城市代号:"<<endl;
	cout<<"1 昆明"<<endl;
	cout<<"2 曲靖"<<endl;
	cout<<"3 玉溪"<<endl;
	cout<<"4 保山"<<endl;
	cout<<"5 昭通"<<endl;
	cout<<"6 丽江"<<endl;
	cout<<"7 普洱"<<endl;
	cout<<"8 临沧"<<endl;
	cout<<"9 德宏"<<endl;
	cout<<"10 怒江"<<endl;
	cout<<"11 迪庆"<<endl;
	cout<<"12 大理"<<endl;
	cout<<"13 楚雄"<<endl;
	cout<<"14 红河"<<endl;
	cout<<"15 文山"<<endl;
    cout<<"16 西双版纳"<<endl;
}

void graphReset() {		//将访问状态位清零
	int i = 0;
	for (i ; i < 30; i++) {
		status[i] = 0;
	}
}

void bfs(int index) {													//从指定元素开始广度优先遍历,输出离所选城市最近的州市 
	push(index);															//首部元素入队列
	status[index] = 1;
	while (queLen > 0) {											// 如果队列此时非空
		int fr = pop();													//得到队列首部									
		switch(fr){
    	case 1:
    		cout<<"昆明 "<<endl;break;
    	case 2:
    		cout<<"曲靖 "<<endl;break;
    	case 3:
    		cout<<"玉溪 "<<endl;break;
		case 4:
    		cout<<"保山 "<<endl;break;
		case 5:
    		cout<<"昭通 "<<endl;break;
		case 6:
    		cout<<"丽江 "<<endl;break;
		case 7:
    		cout<<"普洱 "<<endl;break;
		case 8:
    		cout<<"临沧 "<<endl;break;
		case 9:
    		cout<<"德宏 "<<endl;break;
		case 10:
    		cout<<"怒江 "<<endl;break;
		case 11:
    		cout<<"迪庆 "<<endl;break;
		case 12:
    		cout<<"大理 "<<endl;break;
		case 13:
    		cout<<"楚雄 "<<endl;break;
		case 14:
    		cout<<"红河 "<<endl;break;
		case 15:
    		cout<<"文山 "<<endl;break;
    	case 16:
    		cout<<"西双版纳 "<<endl;break;
	}				
		Node* tmp = nodes[fr];									// 首部元素节点指针

		while (tmp != NULL) {									
			if (status[tmp->data] == 0) {				
				status[tmp->data] = 1;	
				push(tmp->data);									// 加入到队列首部
			}
			tmp = tmp->next;										//继续找下一个
		}
	}
	int left;
	for (left=1 ; left <= vNum; left++) {
		if (status[left] == 0)
			bfs(left);
		}
}

int path[30];														//记录路径的数组
int pathLength = 1;											//路径长度处初始值是1
void search(int start, int end, int length) {	//迭代法寻找路径,输入开始城市的代号,结束城市的代号以及途径城市个数,然后输出规划路线以及路况信息 
	if (length == 0) {													//迭代结束条件
		return;
	}
	status[start] = 1;											//将此次被遍历的元素访问状态置1
	if (start == end && length==1) {											//打印符合指定长度的路径
		cout<<"\n";
		for (int i = 0; i < pathLength - 1; i++) {
				switch(path[i]){
    	case 1:
    		cout<<"昆明"<<"->";break;
    	case 2:
    		cout<<"曲靖"<<"->";break;
    	case 3:
    		cout<<"玉溪"<<"->";break;
		case 4:
    		cout<<"保山"<<"->";break;
		case 5:
    		cout<<"昭通"<<"->";break;
		case 6:
    		cout<<"丽江"<<"->";break;
		case 7:
    		cout<<"普洱"<<"->";break;
		case 8:
    		cout<<"临沧"<<"->";break;
		case 9:
    		cout<<"德宏"<<"->";break;
		case 10:
    		cout<<"怒江"<<"->";break;
		case 11:
    		cout<<"迪庆"<<"->";break;
		case 12:
    		cout<<"大理"<<"->";break;
		case 13:
    		cout<<"楚雄"<<"->";break;
		case 14:
    		cout<<"红河"<<"->";break;
		case 15:
    		cout<<"文山"<<"->";break;
		case 16:
    		cout<<"西双版纳"<<"->";break;	
	}	
	   int x;
	   x=rand()%4;
	   switch(x){
	   	case 0:
	   		cout<<"(当前路段畅通!)";break;
	   	case 1:
	   		cout<<"(当前路段畅通!)";break;
	   	case 2:
	   		cout<<"(当前路段不畅,请注意慢行!)";break;
	   	case 3:
	   		cout<<"(当前路段拥堵,建议绕行!)";break;
	   }			
		}
			switch(path[pathLength - 1]){
    	case 1:
    		cout<<"昆明"<<endl;break;
    	case 2:
    		cout<<"曲靖"<<endl;break;
    	case 3:
    		cout<<"玉溪"<<endl;break;
		case 4:
    		cout<<"保山"<<endl;break;
		case 5:
    		cout<<"昭通"<<endl;break;
		case 6:
    		cout<<"丽江"<<endl;break;
		case 7:
    		cout<<"普洱"<<endl;break;
		case 8:
    		cout<<"临沧"<<endl;break;
		case 9:
    		cout<<"德宏"<<endl;break;
		case 10:
    		cout<<"怒江"<<endl;break;
		case 11:
    		cout<<"迪庆"<<endl;break;
		case 12:
    		cout<<"大理"<<endl;break;
		case 13:
    		cout<<"楚雄"<<endl;break;
		case 14:
    		cout<<"红河"<<endl;break;
		case 15:
    		cout<<"文山"<<endl;break;
		case 16:
    		cout<<"西双版纳"<<endl;break;	
	}	
	    cout<<endl;			
		status[start] = 0;
		return;
	}
	Node* tmp = nodes[start];							
	while (tmp != NULL)									//继续进行迭代
	{
		if (status[tmp->data] == 0) {					//若该元素尚未被访问过
			path[pathLength] = tmp->data;			//将其记录下来
			pathLength++;
			search(tmp->data, end, length - 1);				//进行迭代
			pathLength--;
		}
		tmp = tmp->next;
	}
	status[start] = 0;											//若迭代结束,将此次遍历到的元素的访问状态位置零
}
string login(string name,int num){   //管理员输入正确名字及密码登录 
	if(name.compare("limingrui")==0&&num==123456)
	   return "登录成功!";
	else{
	   return "登录失败!";
    }
}
int main()
{
	int start = 0;
	int end = 0;
	int length = 0;
	int num1;
	char a;
	string login1="登录失败!";
	string name1;
	cout<<"欢迎进入云南省各州市高速交通路况信息查询系统!"<<endl; 
	while(login1.compare("登录失败!")==0)
	{    cout<<"请管理员先登录并录入信息:"<<endl;
	     cout<<"姓名为:"<<endl;
	     cin>>name1;
	     cout<<"密码为:"<<endl;
	     cin>>num1;
	     login1=login(name1,num1);  //若login方法返回“登录成功!”则跳出循环 
	     cout<<login1<<endl; 
	} 	
	graphCreat();
	graphReset();
	while(1)
	{
	cout<<"用户您好!七彩云南欢迎您!请选择您所需功能:"<<endl;
	cout<<"查询某市周边城市请输入A,查询某市到某市的路况信息请输入B"<<endl;
	cin>>a;
	switch(a)
	{
		case 'A':
		    cout << "请输入开始城市代号" << endl;
	        cin >> start;

	        cout << "由近到远城市序列为" << endl;
	        bfs(start);
	        graphReset();
	        cout << endl;break;
	    case 'B':
	    	cout << "请输入起始城市代号 终点城市代号 途径城市数量" << endl;
	        cin >> start >> end >> length;
	        path[0] = start;									
	        search(start, end, length);break;
	}
}
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读