公交车换乘算法(转)

公交车换乘算法[共同讨论和学习
//[作者:龙龙,发表时间: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              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              $flag3 = 0;
             //函数回调
             $bussperline = $this->bussperline($busslinetotal[$i]);
             //遍历每一车次经过的站点
             for($j=0;$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("".$start. ""
             .$end."
没有直达车!请参 照下列换乘建议.
");
             //查询一级中转站
             //start起点
             //end终点
             //函数回调,取得经过起点和终点的所有组合
             $statpass = $this->passline($start);
             $endpass = $this->passline($end);
             //得到经过起点和终点的线路的全部组合
             $resultbus = "";
             for($a=0;$a                  for($b=0;$b                      //判断两条线路有没有交叉点
                     $startper = $this->bussperline($statpass[$a]);
                     $endper = $this->bussperline($endpass[$b]);
                     for($c=0;$c                          for($d=0;$d                              if($startper[$c]==$endper[$d]){
                                 //成功找到交叉点后
                                 //存储交叉点处信息
                                 //此只为一次换乘
                                 $fistid = $statpass[$a];
                                 $secondid = $endpass[$b];
                                 $changestation = $startper[$c];
                                 $resultbus .= $fistid.";".$secondid.";".$changestation."||";
                             }
                         }
                     }
                    
                 }
             }
             if($resultbus=="")
             {
             //没有找到换乘线路
             echo("
抱歉," .$start. ""
             .$end. "
没有直达车,换乘一次也无法到达!");
             }
             else{
                 //找到换乘线路
                 $resultbus = substr($resultbus,0,strlen($resultbus)-2);//去掉最右边的"||"
                 $resultbus_ok1 = explode("||",$resultbus);//将字符串分割成数组
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 for($mm=0;$mm                      $resultbus_ok2 = explode(";",$resultbus_ok1[$mm]);
                     //计算两辆车的起点和终点
                     $bus1 = $this->bussperline($resultbus_ok2[0]);
                     $bus2 = $this->bussperline($resultbus_ok2[1]);
                     //显示
                     echo ("
");
                     echo ("");
                     echo ("");
                     echo ("");
                     echo ("");
                     echo ("");
                     echo("");
                 }
                 echo("
起点车次中转站车次终点
" .$bus1[0]. "" .$resultbus_ok2[0]. "" .$resultbus_ok2[2]. " ==> " .$resultbus_ok2[1]. "" .$bus2[count($bus2)-1]. "
");
             }
         }
         else{
             //有直达车,直接显示直达车情况
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             echo ("");
             $strTemp = substr($strTemp,0,strlen($strTemp)-2);//去掉最右边的"||"
             $strTemp_ok1 = explode("||",$strTemp);//将字符串分割成数组
             for($nn=0;$nn                  //计算车辆的起点和终点
                 $bus = $this->bussperline($strTemp_ok1[$nn]);
                 //显示
                 echo ("
");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo ("");
                 echo("");
             }
             echo("
车次起点经过经过终点详情
" .$strTemp_ok1[$nn]. "" .$bus[0]. "" .$start. " ==> " .$end. "" .$bus[count($bus)-1]. "详情
");
         }
     }
}

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

转载于:https://www.cnblogs.com/silvester/archive/2008/01/01/1694764.html

你可能感兴趣的:(公交车换乘算法(转))