公交车换乘算法[共同讨论和学习
<?php
//[作者:龙龙,发表时间:2006-11-15,未经同意不得转载]
//本算法实现公交线路一次换乘,多次换乘算法这里不作讨论

//数据库名bus,建立数据表bus
//由于此换乘算法比较简单,只需一张表bus就可以了
//表bus结构如:
/**
* **********************************************************************************************
* id(自动编号 pk)     busline(公交线路 int 4)   busname(站点名称 varchar 20)   busorder(站点顺序 int 4)
* 1                    1                         火车站                           1
* 2                    1                         胜利广场                         2
* 3                    1                         卖渔桥                           3
* ...                  ...                       ...                             ...
* 25                   2                         胜利广场                         1
* 26                   2                         天香电器城                       2
* 27                   2                         五里墩                           3
* ...                  ...                       ...                             ...
* 数据库就是这样插记录的,可以把城市的公交线路数据全部插进去
************************************************************************************************
*/
//定义一个新类
//实现公交换乘
class buss{
     //定义数据库连接成员变量
     var $host;
     var $user;
     var $passwd;
     var $database;
     var $conn;
     //利用构造函数实现变量初始化,连接数据库
     function buss(){
         $this->host="localhost";
         $this->user="root";
         $this->passwd="";
         $this->database="bus";
         $this->conn=mysql_connect($this->host, $this->user,$this->passwd) or
         die("Could not connect to $this->host");
         mysql_select_db($this->database,$this->conn) or
         die("Could not switch to database $this->database");
     }
    
     //统计数据库中所有公交站点名,存入数组
     //返回站点名
     function busstotal(){
         $SQL = "select * from bus group by busname";
         $count = 0;
         $result = mysql_query($SQL);
         while($row = mysql_fetch_object($result)){
             $bustotal[$count]= $row->busname;
             $count++;
         }
         return $bustotal;
     }
    
     //统计数据库中所有公交路线,存入数组
     //返回公交线路
     function busslinetotal(){
         $SQL = "select * from bus group by busline";
         $count = 0;
         $result = mysql_query($SQL);
         while($row = mysql_fetch_object($result)){
             $buslinetotal[$count]= $row->busline;
             $count++;
         }
         return $buslinetotal;
     }
    
     //统计数据库中每一线路经过的站点,存入数组
     //需要参数line,区别每一路车
     //返回站点名
     function bussperline($line){
         $SQL = "select * from bus where busline = '$line'";
         $count = 0;
         $result = mysql_query($SQL);
         while($row = mysql_fetch_object($result)){
             $busperline[$count]= $row->busname;
             $count++;
         }
         return $busperline;
     }
    
     //统计经过某站点的所有公交车的组合
     //需要参数station,表示经过的站点
     //返回公交线路
     function passline($station){
         $SQL = "select * from bus where busname = '$station' group by busline";
         $count = 0;
         $result = mysql_query($SQL);
         while($row = mysql_fetch_object($result)){
             $passline[$count]= $row->busline;
             $count++;
         }
         return $passline;
     }
    
     //实现换乘算法的函数
     //需要提供参数,查询的起点和终点
     function bussStationToStation($start,$end){
         $flag1 = false;
         $flag2 = false;
         //函数回调
         $busstotal = $this->busstotal();
         $busslinetotal = $this->busslinetotal();
         //判断数据库中是否有此站点
         for($i=0;$i<count($busstotal);$i++){
             if($start==$busstotal[$i]) $flag1 = true;
             if($end==$busstotal[$i]) $flag2 = true;
             if($flag1 and $flag2) break;
         }
         //有一个站点不存在
         if(!($flag1 and $flag2)){
             if(!$flag1) die("$start站点不存在!");
             if(!$flag2) die("$end站点不存在!");
         }
         //两个站点都存在的情况
         //首先判断有无直达车
         $strTemp = "";
         //遍历所有车次
         for($i=0;$i<count($busslinetotal);$i++){
             $flag3 = 0;
             //函数回调
             $bussperline = $this->bussperline($busslinetotal[$i]);
             //遍历每一车次经过的站点
             for($j=0;$j<count($bussperline);$j++){
                 if($start==$bussperline[$j]) $flag3 +=1;
                 if($end==$bussperline[$j]) $flag3 +=1;
                 if($flag3==2) break;
             }
             if($flag3==2)
             //保存直达车次,以||分割
             $strTemp = $strTemp.$busslinetotal[$i]."||";
         }
         if($strTemp==""){
             //没有直达车,则计算一次换乘情况
             echo("<strong><font color=#FF0000>".$start. "</font></strong> 到 <strong><font color=#FF0000>"
             .$end."</font></strong> 没有直达车!请参 照下列换乘建议.
");
             //查询一级中转站
             //start起点
             //end终点
             //函数回调,取得经过起点和终点的所有组合
             $statpass = $this->passline($start);
             $endpass = $this->passline($end);
             //得到经过起点和终点的线路的全部组合
             $resultbus = "";
             for($a=0;$a<count($statpass);$a++){
                 for($b=0;$b<count($endpass);$b++){
                     //判断两条线路有没有交叉点
                     $startper = $this->bussperline($statpass[$a]);
                     $endper = $this->bussperline($endpass[$b]);
                     for($c=0;$c<count($startper);$c++){
                         for($d=0;$d<count($endper);$d++){
                             if($startper[$c]==$endper[$d]){
                                 //成功找到交叉点后
                                 //存储交叉点处信息
                                 //此只为一次换乘
                                 $fistid = $statpass[$a];
                                 $secondid = $endpass[$b];
                                 $changestation = $startper[$c];
                                 $resultbus .= $fistid.";".$secondid.";".$changestation."||";
                             }
                         }
                     }
                    
                 }
             }
             if($resultbus=="")
             {
             //没有找到换乘线路
             echo("
抱歉,<strong><font color=#FF0000>" .$start. "</font></strong> 到 <strong><font color=#FF0000>"
             .$end. "</font></strong>没有直达车,换乘一次也无法到达!");
             }
             else{
                 //找到换乘线路
                 $resultbus = substr($resultbus,0,strlen($resultbus)-2);//去掉最右边的"||"
                 $resultbus_ok1 = explode("||",$resultbus);//将字符串分割成数组
                 echo ("<table width=600 border=0 bgcolor=#003399 cellpadding=3 cellspacing=1>");
                 echo ("<tr bgcolor=#FDDD90>");
                 echo ("<td width=150><strong>起点</strong></td>");
                 echo ("<td width=70><strong>车次</strong></td>");
                 echo ("<td width=160><strong>中转站</strong></td>");
                 echo ("<td width=70><strong>车次</strong></td>");
                 echo ("<td width=150><strong>终点</strong></td>");
                 echo ("</tr>");
                 for($mm=0;$mm<count($resultbus_ok1);$mm++){
                     $resultbus_ok2 = explode(";",$resultbus_ok1[$mm]);
                     //计算两辆车的起点和终点
                     $bus1 = $this->bussperline($resultbus_ok2[0]);
                     $bus2 = $this->bussperline($resultbus_ok2[1]);
                     //显示
                     echo ("<tr bgcolor=#E8F3FF onMouseOver = this.style.backgroundColor = '#FFF9EE' onMouseOut = this.style.backgroundColor = ''>");
                     echo ("<td width=150><strong><font color=#FF0000>" .$bus1[0]. "</strong></font></td>");
                     echo ("<td width=70><a href=''>" .$resultbus_ok2[0]. "</a></td>");
                     echo ("<td width=160><strong><font color=#FF0000>" .$resultbus_ok2[2]. "</strong></font> ==> </td>");
                     echo ("<td width=70><a href=''>" .$resultbus_ok2[1]. "</a></td>");
                     echo ("<td width=150><strong><font color=#FF0000>" .$bus2[count($bus2)-1]. "</strong></font></td>");
                     echo("</tr>");
                 }
                 echo("</table>");
             }
         }
         else{
             //有直达车,直接显示直达车情况
             echo ("<table width=600 border=0 bgcolor=#003399 cellpadding=3 cellspacing=1>");
             echo ("<tr bgcolor=#FDDD90>");
             echo ("<td width=150><strong>车次</strong></td>");
             echo ("<td width=70><strong>起点</strong></td>");
             echo ("<td width=160><strong>经过</strong></td>");
             echo ("<td width=70><strong>经过</strong></td>");
             echo ("<td width=150><strong>终点</strong></td>");
             echo ("<td width=150><strong>详情</strong></td>");
             echo ("</tr>");
             $strTemp = substr($strTemp,0,strlen($strTemp)-2);//去掉最右边的"||"
             $strTemp_ok1 = explode("||",$strTemp);//将字符串分割成数组
             for($nn=0;$nn<count($strTemp_ok1);$nn++){
                 //计算车辆的起点和终点
                 $bus = $this->bussperline($strTemp_ok1[$nn]);
                 //显示
                 echo ("<tr bgcolor=#E8F3FF onMouseOver = this.style.backgroundColor = '#FFF9EE' onMouseOut = this.style.backgroundColor = ''>");
                 echo ("<td width=150><font color=#FF0000>" .$strTemp_ok1[$nn]. "</strong></font></td>");
                 echo ("<td width=70><a href=''>" .$bus[0]. "</a></td>");
                 echo ("<td width=160><strong><font color=#FF0000>" .$start. "</strong></font> ==> </td>");
                 echo ("<td width=70><a href=''>" .$end. "</a></td>");
                 echo ("<td width=150><font color=#FF0000>" .$bus[count($bus)-1]. "</strong></font></td>");
                 echo ("<td width=70><a href=''>详情</a></td>");
                 echo("</tr>");
             }
             echo("</table>");
         }
     }
}

/*
定义好抽象类后,使用就非常简单了
*/
$bus = new buss;
$bus->bussStationToStation("火车站","五里墩");
//一切ok,直接就可以看到结果了
?>

相关文章: