校园导游系统(Java语言)
生活随笔
收集整理的這篇文章主要介紹了
校园导游系统(Java语言)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
**
利用Java語言寫的校園導(dǎo)游系統(tǒng)
**
這個是主要利用了迪杰斯特拉算法寫成的,有增加路徑,景點等操作。
由于這個地圖是用Java中的io流來讀取的,因此需要寫一個文件。
文件格式如下:
注意:
要記得在GraphMain.java這個文件155行中及時修改你這個文件的路徑
代碼如下:
Graph.java
public class Graph {VertexType[] vexs = new VertexType[20]; //定義20個頂點(地圖上的20個位置)int [][] arcs = new int[20][20]; //用來存取權(quán)值(地圖上兩個地點之間的距離)int vexnum,arcnum; //圖的當(dāng)前點數(shù)和邊數(shù)}VertexType.java
//頂點結(jié)構(gòu) public class VertexType {String name; //定義頂點的名字(地圖某一點的名字)String info; //定義頂點的描述(地圖某一點的描述)public VertexType(){}public VertexType(String name, String info){this.name = name;this.info = info;} }GraphMain.java
package Graph;import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner;public class GraphMain {private static final int MaxInt = 22222;private static Scanner input = new Scanner(System.in);public static void main(String[] args) {//生成一個地圖對象Graph graph = new Graph();//建立地圖createUDN(graph);printf("********************歡迎來到XX大學(xué)*********************");printf("* *");printf("* 1.查詢景點信息 *");printf("* 2.問路查詢 *");printf("* 3.修改已有景點的相關(guān)信息 *");printf("* 4.增加一個新景點及相關(guān)信息 *");printf("* 5.增加一條新的路徑 *");printf("* 6.刪除一個景點及其相關(guān)信息 *");printf("* 7.刪除一個路徑 *");printf("* 8.退出 *");printf("* *");printf("********************XX大學(xué)導(dǎo)游系統(tǒng)*********************");while(true){printf("請選擇需要的服務(wù)(1-6):");int Select = input.nextInt();if(Select == 1){printf("本校景點有:");for(int i = 0; i < graph.vexnum; i ++)printf(" " + (i+1) + "." + graph.vexs[i].name);printf("請選擇您要查詢的景點" + "(1-" + graph.vexnum + "):");int select = input.nextInt();printf(graph.vexs[select - 1].name + " " + graph.vexs[select - 1].info);}else if(Select == 2){printf("本校景點有:");for(int i = 0; i < graph.vexnum; i++)printf(" " + (i+1) + "." + graph.vexs[i].name);printf("請輸入您的位置" + "(1-" + graph.vexnum + "):");int initialPosition = input.nextInt();printf("請輸入您的目的地" + "(1-" + graph.vexnum + "):");int targetPosition = input.nextInt();ShortestPath_DIJ(graph,initialPosition,targetPosition);}else if(Select == 3){printf("請輸入您想修改哪個景點的相關(guān)信息" + "(1-" + graph.vexnum + "):");int number = input.nextInt();System.out.println("請輸入該景點的相關(guān)信息:");String message = input.next();if(number < graph.vexnum + 1 && number > 0)graph.vexs[number - 1].info = message;elseprintf("對不起!您輸入的標(biāo)號有誤!");}else if(Select == 4){printf("請輸入新景點的名字:");String name = input.next();printf("請輸入新景點的相關(guān)信息:");String info = input.next();graph.vexnum++;graph.vexs[graph.vexnum - 1] = new VertexType(name,info);printf("添加景點成功!");}else if(Select == 5){printf("請輸入路徑的始點" + "(1-" + graph.vexnum + "):");int initial = input.nextInt();printf("請輸入路徑的終點" + "(1-" + graph.vexnum + "):");int destination = input.nextInt();printf("請輸入始點和終點的距離:");int distance = input.nextInt();if((0 < initial && initial < graph.vexnum + 1) && (0 < destination && destination < graph.vexnum + 1)){graph.arcs[initial-1][destination-1] = distance;graph.arcs[destination-1][initial-1] = distance;printf("添加路徑成功!");}elseprintf("對不起!您輸入的位置有誤!");}else if(Select == 6){printf("請輸入將要刪除景點的標(biāo)號" + "(1-" + graph.vexnum + "):");int number = input.nextInt();//在范圍內(nèi)if(0 < number && number < graph.vexnum + 1){graph.vexs[number - 1] = null;for(int h = number-1; h < graph.vexnum; h++){//把景點刪除graph.vexs[h] = graph.vexs[h + 1];}for(int w = number - 1; w < graph.vexnum; w++)for(int j = 0; j < graph.arcs[w].length; j++){//把路徑刪除graph.arcs[w][j] = graph.arcs[w + 1][j];graph.arcs[j][w] = graph.arcs[j][w+1];}graph.vexnum--;printf("刪除景點完畢!");}elseprintf("對不起!您輸入的標(biāo)號有誤!");}else if(Select == 7){printf("請輸入將要刪除路徑的始點標(biāo)號" + "(1-" + graph.vexnum + "):");int initial = input.nextInt();printf("請輸入將要刪除路徑的終點標(biāo)號" + "(1-" + graph.vexnum + "):");int destination = input.nextInt();if((0 < initial && initial < graph.vexnum + 1) && (0 < destination && destination < graph.vexnum + 1)){graph.arcs[initial-1][destination-1] = MaxInt;graph.arcs[destination-1][initial-1] = MaxInt;printf("刪除路徑完畢!");}elseprintf("對不起!您輸入的標(biāo)號有誤!");}else if(Select == 8){printf("謝謝您的使用!歡迎下次再來!");break;}elseprintf("對不起,輸入有誤!請重新輸入!");}}public static void printf(Object o){System.out.println(o);}public static void createUDN(Graph graph){try{String encoding="utf-8";//這個路徑要及時修改!!!File file=new File("C:/Users/Lenovo/Desktop/Graph.txt");//判斷文件是否存在if(file.isFile() && file.exists()){//考慮到編碼格式,這里用到utf-8,中文字符更多。//將字節(jié)流輸入流轉(zhuǎn)換成字符輸入流InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//將字符輸入流轉(zhuǎn)換成帶緩沖區(qū)的字符輸入流BufferedReader bufferedReader = new BufferedReader(read);String lineTxt = null;int i = 1;int e,f;while((lineTxt = bufferedReader.readLine()) != null){String[] a = lineTxt.split(" ");if(i == 1){graph.vexnum = Integer.parseInt(a[0]);graph.arcnum = Integer.parseInt(a[1]);}if(1 < i && i <= (graph.vexnum + 1)){graph.vexs[i-2] = new VertexType(a[0],a[1]);}if(i <= (graph.arcnum + graph.vexnum + 1) && i > (graph.vexnum + 1)){e = LocateVex(graph,a[0]);f = LocateVex(graph,a[1]);graph.arcs[e][f] = Integer.parseInt(a[2]);graph.arcs[f][e] = Integer.parseInt(a[2]);}i++;}//關(guān)閉文件,關(guān)閉了read,其他也都關(guān)閉了read.close();for(int h = 0; h < graph.arcs.length; h++)for(int j = 0; j < graph.arcs[h].length; j++)if(h != j && graph.arcs[h][j] == 0)graph.arcs[h][j] = MaxInt;}}catch(Exception e){printf(e.getMessage());}}//查找頂點的位置public static int LocateVex(Graph graph,String name){for(int i = 0; i < graph.vexnum; i++)if(name.equals(graph.vexs[i].name))return i;return -1;}public static void ShortestPath_DIJ(Graph graph,int v0,int v1){int n = graph.vexnum; //先把地圖的位置頂點用n來表示boolean [] S = new boolean[n]; //每執(zhí)行一次循環(huán)找出最小路徑,如果找出一個最小路徑就把該點置為trueint [] D = new int[n]; //存權(quán)值(最小的),如果有更小的將會代替該位置上大的權(quán)值int [] Path = new int[n]; //記錄該索引頂點的前驅(qū)頂點//初始化,先找出v0結(jié)點的鄰居結(jié)點的各個權(quán)值for(int i = 0; i < n; i ++){S[i] = false; //S中先都置為falseD[i] = graph.arcs[v0 -1][i]; //最開始先把v0與其他幾個頂點的權(quán)值賦值給D[i]if(D[i] != MaxInt && D[i] != 0)Path[i] = v0 - 1; //第i個頂點的前驅(qū)結(jié)點就是v0,表示i和v0相鄰elsePath[i] = -1; //表示i頂點和v0頂點沒有相鄰,如果相鄰必有權(quán)值不為無窮大}S[v0 - 1] = true; //v0與v0之間路徑為0,我們不再討論int v = - 1;//對剩下的n-1個頂點循環(huán)。for(int i = 1; i < n; i++){int m = MaxInt;//找出D中最小的路徑for(int w = 0; w < n; w++)if(!S[w] && D[w] < m){m = D[w]; //依次比較D中的權(quán)值,找出最小的權(quán)值v = w; //把這個最小權(quán)值的位置賦值給v(記錄下來)}if(v != -1) //表示我們找到了D中的最小路徑{S[v] = true; //我們找到了最小路徑,我們把它置為true,以防下次還找到它for(int w = 0; w < n; w++)if(!S[w] && D[v] + graph.arcs[v][w] < D[w]) //如果原來的路徑D[w](0-n)依次和v0到v的權(quán)值再加上v到w的權(quán)值進(jìn)行比較{D[w] = D[v] + graph.arcs[v][w]; //如果小于了,等于說有最優(yōu)路徑,我們把最優(yōu)路徑賦值給D[w]Path[w] = v; //并把w的前驅(qū)置為v}} }int [] a = new int[n];for(int i = 0; i < Path.length; i++){a[i] = -1;if(Path[v1 - 1] == v0 - 1){continue;}else{a[i] = Path[v1 - 1];if(Path[v1 - 1] != -1)Path[v1 - 1] = Path[Path[v1 - 1]];}}if(D[v1 - 1] != MaxInt){System.out.print("路徑是:" + graph.vexs[v0 - 1].name);for(int i = a.length - 1; i >= 0; i --){if(a[i] != -1){System.out.print("->" + graph.vexs[a[i]].name);}}System.out.print("->" + graph.vexs[v1 - 1].name + "\n");printf("最短距離為:" + D[v1 - 1]);}elseprintf("對不起!兩者不連通!");} }總結(jié)
以上是生活随笔為你收集整理的校园导游系统(Java语言)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各大浏览器内核和前缀
- 下一篇: 荧光定量PCR检测法的原理和应用领域