车辆匹配和平均车速计算
生活随笔
收集整理的這篇文章主要介紹了
车辆匹配和平均车速计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據測試內容以及詳情見
https://github.com/xueyeyu/avgsp
?
?
/* 作者:雪夜羽 平均車速計算(sqlserver)基于電警 QQ:1412900482 */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; //import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; //import java.text.SimpleDateFormat; //import java.util.Date;public class avgsp {//變量聲明//////定義SQL server數據庫驅動final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//定義SQL server鏈接final String dburl="jdbc:sqlserver://localhost:1433;";private final String sheet;private final String dbname;private final String dbuser;private final String dbpassword;//定義數據private final int []fx = {1,2,3,4};//定義存儲結果//0車牌,1時間,3方向 4車道號 可存儲 1 0000 條數據private String rs1[][]=new String [100*100][4];private String rs2[][]=new String [100*100][4];//定義不重復車牌 (distinct)private String []chepai1 = new String[100*100];private String []chepai2 = new String[100*100];//記錄數組存放的條數int rs1i=0;int rs2i=0;//記錄不重復車輛的條數int chepai1i=0;int chepai2i=0;String sql1,sql2;//構造器 傳遞參數public avgsp(String dbname,String dbuser,String dbpassword,String sheet) {this.dbname=dbname;this.dbuser=dbuser;this.dbpassword=dbpassword;this.sheet=sheet;//考慮加入e字段(卡口編號)this.sql1="SELECT a,f,i,h FROM " + sheet +" where e = ? and a <> 'f941c96603e51c3c3f6297dc385d4cdc' and a <> '39d6d1ed9e4cb2571d8a3dd937827396' and f between ? and ? order by a,f asc";this.sql2="SELECT distinct a FROM " + sheet +" where e = ? and f between ? and ? order by a asc";}public void query(String time1,String time2,String kakou1,String kakou2) {//////private String time1;//private String time2;//private String kakou1;//private String kakou2;//加載驅動try {Class.forName(driver); System.out.println("加載數據庫驅動成功");}//拋出異常 catch(ClassNotFoundException e) { System.out.println("加載數據庫驅動失敗"); }//嘗試鏈接數據庫//建立鏈接try {Connection conn =DriverManager.getConnection(dburl+"databasename="+dbname,dbuser,dbpassword); System.out.println("數據庫連接成功");//System.out.println("開始讀取數據庫");//////初始化循環變量//int fxi=0;//執行查詢操作//定義查詢語句//String sql1 = "SELECT a,f,j,i FROM " + sheet +" where e = ? and f between ? and ? order by a asc";//搜索不重復車牌//String sql2 = "SELECT distinct a FROM " + sheet +" where e = ? and f between ? and ? order by a asc"; PreparedStatement pstmt1=conn.prepareStatement(sql1);pstmt1.setString(1, kakou1);pstmt1.setString(2, time1);pstmt1.setString(3, time2);//pstmt.setInt(4, fx[fxi]);//獲取結果ResultSet result1 = pstmt1.executeQuery();//逐行(條)讀取數據while(result1.next()) {//賦值車牌this.rs1[rs1i][0] = result1.getString(1);//賦值時間this.rs1[rs1i][1] = result1.getString(2);//賦值方向this.rs1[rs1i][2] = result1.getString(3);//賦值車道號this.rs1[rs1i][3] = result1.getString(4);rs1i++;}result1.close();//執行查詢不重復車牌操作PreparedStatement pstmt1x=conn.prepareStatement(sql2);pstmt1x.setString(1, kakou1);pstmt1x.setString(2, time1);pstmt1x.setString(3, time2);ResultSet rschepai = pstmt1x.executeQuery();while(rschepai.next()) {//賦值不重復車牌this.chepai1[chepai1i] = rschepai.getString(1);chepai1i++;}rschepai.close();//System.out.println("卡口 "+kakou1+" 記錄數量為 "+rs1i);//System.out.println("卡口 "+kakou1+" 不重復車輛數量為 "+chepai1i);////**************************************************//****************************************************//**************************************************// PreparedStatement pstmt2=conn.prepareStatement(sql1);pstmt2.setString(1, kakou2);pstmt2.setString(2, time1);pstmt2.setString(3, time2);//pstmt.setInt(4, fx[fxi]);ResultSet result2 = pstmt2.executeQuery();while(result2.next()){//賦值車牌this.rs2[rs2i][0] = result2.getString(1);//賦值時間this.rs2[rs2i][1] = result2.getString(2);//賦值方向this.rs2[rs2i][2] = result2.getString(3);//賦值車道號this.rs2[rs2i][3] = result2.getString(4);rs2i++;}result2.close();//執行查詢不重復車牌操作PreparedStatement pstmt2x=conn.prepareStatement(sql2);pstmt2x.setString(1, kakou2);pstmt2x.setString(2, time1);pstmt2x.setString(3, time2);ResultSet rschepai2 = pstmt2x.executeQuery();while(rschepai2.next()) {//賦值不重復車牌this.chepai2[chepai2i] = rschepai2.getString(1);chepai2i++;}rschepai2.close();//System.out.println("卡口 "+kakou2+" 記錄數量為 "+rs2i);//System.out.println("卡口 "+kakou2+" 不重復車輛數量為 "+chepai2i); }//拋出異常//catch(SQLException | ParseException e) catch(SQLException e) {e.printStackTrace();}}//傳遞記錄的數組數據public String[][] returnrs1() {return rs1;}public String[][] returnrs2() {return rs2;}public String[] returnchepai1() {return chepai1;}public String[] returnchepai2() {return chepai2;}//傳遞記錄的條數public int returnrs1i() {return rs1i;}public int returnrs2i() {return rs2i;}public int returnchepai1i() {return chepai1i;}public int returnchepai2i() {return chepai2i;} }/*//初始化循環變量//int fxi=0;//記錄數組存放的條數int rs1i=0;int rs2i=0;//記錄不重復車輛的條數int chepai1i=0;int chepai2i=0;//執行查詢操作PreparedStatement pstmt1=conn.prepareStatement(sql1);pstmt1.setString(1, kakou1);pstmt1.setString(2, time1);pstmt1.setString(3, time2);//pstmt.setInt(4, fx[fxi]);//獲取結果ResultSet result1 = pstmt1.executeQuery();//逐行(條)讀取數據while(result1.next()){//賦值車牌this.rs1[rs1i][0] = result1.getString(1);//賦值時間this.rs1[rs1i][1] = result1.getString(3);//賦值方向this.rs1[rs1i][2] = result1.getString(4);//賦值車道號this.rs1[rs1i][3] = result1.getString(5);rs1i++;}result1.close();//執行查詢不重復車牌操作PreparedStatement pstmt1x=conn.prepareStatement(sql2);pstmt1x.setString(1, kakou1);pstmt1x.setString(2, time1);pstmt1x.setString(3, time2);ResultSet rschepai = pstmt1x.executeQuery();while(rschepai.next()) {//賦值不重復車牌this.chepai[chepai1i] = rschepai.getString(1);chepai1i++;}rschepai.close();System.out.println("卡口 "+kakou2+" 數量為 "+rs1i);////**************************************************//****************************************************//**************************************************//int rs2i=0;//定義sql語句 distinctString sq2 = "SELECT a,b,c,d FROM " + sheet +" where b = ? and c between ? and ? ";PreparedStatement pstmt2=conn.prepareStatement(sql1);pstmt2.setString(1, kakou2);pstmt2.setString(2, time1);pstmt2.setString(3, time2);//pstmt.setInt(4, fx[fxi]);ResultSet result2 = pstmt2.executeQuery();while(result2.next()){//賦值車牌this.rs2[i][0] = result2.getString(1);//賦值時間this.rs2[i][1] = result2.getString(3);//賦值方向this.rs2[i][2] = result2.getString(4);//賦值車道號this.rs1[i][3] = result1.getString(5);rs2i++; }result2.close();//執行查詢不重復車牌操作PreparedStatement pstmt2x=conn.prepareStatement(sql2);pstmt2x.setString(1, kakou2);pstmt2x.setString(2, time1);pstmt2x.setString(3, time2);ResultSet rschepai2 = pstmt2x.executeQuery();while(rschepai2.next()) {//賦值不重復車牌this.rschepai2[chepai2i] = rschepai2.getString(1);rschepai2i++;}rschepai2.close();System.out.println("卡口 "+kakou2+" 數量為 "+rs2i);} }*//*try {Class.forName(driver); //加載驅動System.out.println("加載數據庫驅動成功");//創建新實例//jisuan ins = new jisuan(time1,time2,kakou1,kakou2,sheet);}//拋出異常 catch(ClassNotFoundException e) { System.out.println("加載數據庫驅動失敗"); }//嘗試鏈接數據庫//建立鏈接try {Connection conn=DriverManager.getConnection(dburl+"databasename="+dbname,dbuser,dbpassword); System.out.println("數據庫連接成功!");//System.out.println("開始讀取數據庫");jisuan A = new jisuan(sheet);A.query(time1,time2,kakou1,kakou2);}//拋出異常//catch(SQLException | ParseException e) catch(SQLException e) {e.printStackTrace();} }//變量聲明//定義SQL server數據庫驅動final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//定義SQL server服務//final String conn="jdbc:sqlserver://localhost:1433;databasename=kakou";final String dburl="jdbc:sqlserver://localhost:1433;";//定義數據庫名稱final String dbname="kakou";//定義數據庫用戶名 ‘sa’ 為數據庫管理員final String dbuser="sa";//定義數據庫密碼final String dbpassword="password";//定義表final String sheet="d028";//定義時間final String time1="2016-03-01 03:00:00";final String time2="2016-03-01 04:00:00";//定義每次循環增加的時間/******time1 和 time2都增加,可自定義容錯時間例如 time1和time2的時間間隔為20分鐘,每次循環增加時間為15分鐘,03:00:00 03:20:0003:15:00 03:35:0003:30:00 03:50:0003:45:00 04:05:00 合理設置容錯時間可以避免忽略如下情況03:18:00 進入卡口103:22:00 進入卡口2//15為15分鐘,以此類推final int timex=15;//定義卡口編號final String kakou1="3701033112";final String kakou2="3701033109";//定義卡口之間的距離final int distance=1000;//嘗試加載數據庫驅動try {Class.forName(driver); //加載驅動System.out.println("加載數據庫驅動成功");//創建新實例//jisuan ins = new jisuan(time1,time2,kakou1,kakou2,sheet);}//拋出異常 catch(ClassNotFoundException e) { System.out.println("加載數據庫驅動失敗"); }//嘗試鏈接數據庫//建立鏈接try {Connection conn=DriverManager.getConnection(dburl+"databasename="+dbname,dbuser,dbpassword); System.out.println("數據庫連接成功!");//System.out.println("開始讀取數據庫");jisuan A = new jisuan(sheet);A.query(time1,time2,kakou1,kakou2);}//拋出異常//catch(SQLException | ParseException e) catch(SQLException e) {e.printStackTrace();}} } */
?
?
/* 作者:雪夜羽 平均車速計算(sqlserver)基于電警 QQ:1412900482 */import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; //import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; //import java.text.SimpleDateFormat; import java.util.Date; //打印數組值的類 import java.util.Arrays;// /*找出兩個卡口的數量結果集rs1和rs2 找出兩個卡口的不重復車牌chepai1和chepai2 參照chepai1和chepai2尋找rs1和rs2的結果集 */ ////計算模塊 public class jisuan {////類變量不放在構造器里////////定義和修改數據/***********************************************///定義數據庫名稱final String dbname="002";//定義數據庫用戶名 ‘sa’ 為數據庫管理員final String dbuser="sa";//定義數據庫密碼final String dbpassword="SQLServer";//定義表final String sheet="[dbo].[01]";//定義時間//循環查詢(每次加時間)都會重新定義 不用final變量static String time1="2016-05-01 10:00:00 000";static String time2="2016-05-01 18:00:00 000";//定義每次循環增加的時間/******time1 和 time2都增加,可自定義容錯時間例如 time1和time2的時間間隔為20分鐘,每次循環增加時間為15分鐘,03:00:00 03:20:0003:15:00 03:35:0003:30:00 03:50:0003:45:00 04:05:00 合理設置容錯時間可以避免忽略如下情況03:18:00 進入卡口103:22:00 進入卡口2******///15為15分鐘,以此類推final static int timex=15;//定義卡口編號private final static String kakou1="3701022049";private final static String kakou2="3701022116";//定義卡口之間的距離private final int distance=222;//定義下游進口駛入交叉口的方向//1 南向北 2 東向西 3 北向南 4 西向東private final static int downfx=1;//定義合理速度區間private final double maxspeed=120/3.6;private final double minspeed=0.001/3.6;//定義每輛車最多能被卡口記錄幾次(每輛車能經過幾次)private final int maxitem=10;//定義車道,//第一維是車道號碼,第二維代表著//private final int[][]/*********************************************///private String sheet;//定義日期格式//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");//Calendar calendar = Calendar.getInstance();//未知??//Date myDate1;//Date myDate2;//定義基本參數//基礎數據//定義同時跑過卡口1和卡口2的車輛數(不重復)private static int liuliang;//定義卡口1、卡口2經過的不重復的車牌號private static String[] chepai1 = new String [100*1000];private static String[] chepai2 = new String [100*1000];//定義sql直接導出的卡口1和卡口2的數據,//二維數組,第一列表示車牌,第二列表示時間,第三列表示行車方向,第四列表示車道號private static String rs1[][]=new String [100*100][4];private static String rs2[][]=new String [100*100][4];//定義sql記錄卡口1和卡口2的條數,卡口1和卡口2不重復車輛的條數private static int rs1i,rs2i,chepai1i,chepai2i;//定義兩個卡口都有的車牌private static String chepai[] = new String [100*100];//private static int time[] = new int [5000][5000];//按車道劃分平均行程時間和平均行程車速//計算卡口的方向和車道流量//根據卡口進口道方向分類車輛//第二維 1 南向北 2 東向西 3 北向南 4 西向東String [][] fxchepai = new String[100*100][4];//每個卡口進口道方向和車道分類車牌和流量//1 南向北 2 東向西 3 北向南 4 西向東String [][] fx1chedaochepai = new String [100*100][9];String [][] fx2chedaochepai = new String [100*100][9];String [][] fx3chedaochepai = new String [100*100][9];String [][] fx4chedaochepai = new String [100*100][9];//根據卡口進口道分類各股車道的流量//第一維表示方向,第二維表示車道//向前推一個 0 南向北 1 東向西 2 北向南 3 西向東int [][] chedaoliuliang = new int [4][9];//temp3中為匹配車輛,例如//90d48643ef3807461a2095f14be68e11 2016-03-02 07:10:22 970 4 2 //90d48643ef3807461a2095f14be68e11 2016-03-02 07:12:43 240 4 3 String [][] pipei1 = new String [5000][8]; //pipei1為第一個車道String [][] pipei2 = new String [5000][8]; String [][] pipei3 = new String [5000][8]; String [][] pipei4 = new String [5000][8]; String [][] pipei5 = new String [5000][8]; String [][] pipei6 = new String [5000][8]; String [][] pipei7 = new String [5000][8]; String [][] pipei8 = new String [5000][8]; //定義匹配的行數,匹配車輛等于x除以二//定義8個車道,每個車道匹配數量為200int x1=0;int x2=0;int x3=0;int x4=0;int x5=0;int x6=0;int x7=0;int x8=0;public static void main (String args[]) {jisuan A = new jisuan();Calendar calendar = Calendar.getInstance();Date myDate1;int i=0;for(;i<=0;i++) {avgsp link=new avgsp(A.dbname,A.dbuser,A.dbpassword,A.sheet);//time1轉換成日期格式//執行查詢 link.query(A.time1,A.time2,A.kakou1,A.kakou2);//獲取結果 給本類的變量賦值 A.getchepai1(link.returnchepai1());A.getchepai2(link.returnchepai2());A.getrs1(link.returnrs1());A.getrs2(link.returnrs2());// A.getrs1i(link.returnrs1i());A.getrs2i(link.returnrs2i());A.getchepai1i(link.returnchepai1i());A.getchepai2i(link.returnchepai2i());//System.out.println("chepai1i "+chepai1i);//System.out.println("rs1i "+rs1i);System.out.println("time1 "+time1);System.out.println("time2 "+time2);//計算流量 A.liuliang();////A.find1(chepai[0]);//計算平均行程時間和平均行程速度 A.function();A.chedaoliuliang(A.rs1,A.rs1i,1);A.chedaoliuliang(A.rs1,A.rs1i,2);A.chedaoliuliang(A.rs1,A.rs1i,3);A.chedaoliuliang(A.rs1,A.rs1i,4);A.chedaoliuliang(A.rs2,A.rs2i,1);A.chedaoliuliang(A.rs2,A.rs2i,2);A.chedaoliuliang(A.rs2,A.rs2i,3);A.chedaoliuliang(A.rs2,A.rs2i,4);//時間變化myDate1=StrToDate(A.time1);calendar.setTime(myDate1);//加上timex時間 calendar.add(Calendar.MINUTE,timex);myDate1 = calendar.getTime();A.time1=DateToStr(myDate1);myDate1=StrToDate(A.time2);calendar.setTime(myDate1);//加上timex時間 calendar.add(Calendar.MINUTE,timex);myDate1 = calendar.getTime();A.time2=DateToStr(myDate1);A.chetime();}//********************//主函數結束 }//計算車頭時距public void chetime() {//System.out.println(Arrays.deepToString(rs1)); }//處理函數,可調用任何函數public void function() {int i=0;//定義臨時變量傳遞chepai數組里的值 String tempchepai;//循環卡口1//System.out.println("卡口 "+this.kakou1+"\n車牌\t時間\t方向\t車道\t");//定義二維數組,作為每輛車的數據//0 車牌 1 時間 2 方向 3 車道String [][] temp1 = new String [maxitem][4];String [][] temp2 = new String [maxitem][4];//不同車存儲時間int [] sumtime = new int [9];//接受qujiantime的返回值并按車道傳遞給temp3int [][] temp3 = new int [maxitem][9];//chedaoliuliang用于計算匹配的車輛流量,用于計算平均行程車速,不包含速度過大或速度過小的流量int [] matchliuliang = new int [9];//chedaoliuliang2用于全部有記錄的車輛,包括速度過大或速度過小的留戀那個int [] matchliuliang2=new int [9];double [] avgtime=new double [9];double [] avgspeed = new double [9];//debug//temp1=this.find1("f941c96603e51c3c3f6297dc385d4cdc",this.rs1,this.rs1i);//temp2=this.find1("f941c96603e51c3c3f6297dc385d4cdc",this.rs2,this.rs2i);//System.out.println("車牌\t時間\t方向\t車道\t");//temp1=Arrays.sort(temp1);//temp1=Arrays.sort(temp2);//debug//for(;i<=temp1.length;i++)//System.out.println(Arrays.toString(temp1[i])+Arrays.toString(temp2[i]));//select * from (select a,f,j,i from k0302 where e='3701033112' and f between '2016-03-02 03:00:00' and '2016-03-02 04:00:00' and a='f941c96603e51c3c3f6297dc385d4cdc') a join (select a,f,j,i from k0302 where e='3701033109' and f between '2016-03-02 03:00:00' and '2016-03-02 04:00:00' and a='f941c96603e51c3c3f6297dc385d4cdc') b on a.a=b.a //注意 find1已更改為有返回值//System.out.println("\n車牌\t時間\t方向\t車道\t");//kakou1和kakou2同時計算//循環每輛車for (;i<=this.liuliang-1;i++) {tempchepai=this.chepai[i];//取出一輛車兩個卡口的數據temp1=this.find1(tempchepai,this.rs1,this.rs1i);temp2=this.find1(tempchepai,this.rs2,this.rs2i);//debug//System.out.println("temp1 "+Arrays.deepToString(temp1));//System.out.println("temp2 "+Arrays.deepToString(temp2));//調用接口//調用處理函數接口//int timei=0;//50-1為定義數組的長度//for (;temp1[timei][1]!=null&&timei<=50-1;timei++) /temp3=this.qujiantime(temp1,temp2,downfx);//System.out.println("temp3 "+Arrays.deepToString(temp3));int j=0;int k=0;//長度為10for (;j<=temp3.length-1;j++) {//長度為8for (;k<=temp3[0].length-1;k++) {if (temp3[j][k]!=0) {matchliuliang2[k]++;if ((distance/maxspeed)*1000<temp3[j][k] && (distance/minspeed)*1000>temp3[j][k]) {//按列相加sumtime[k]=temp3[j][k]+sumtime[k];matchliuliang[k]++;}//debug//System.out.println("temp3 "+temp3[j][k]);//System.out.println("sumtime "+Arrays.toString(sumtime)); }}}}//System.out.println("sumtime "+Arrays.toString(sumtime));//System.out.println("matchliuliang \t"+Arrays.toString(matchliuliang));//System.out.println("matchliuliang2 \t"+Arrays.toString(matchliuliang2));int l=0;for(;l<=sumtime.length-1;l++) {if (matchliuliang[l]!=0) {avgtime[l]=sumtime[l]/(matchliuliang[l]);avgtime[l]=avgtime[l]/1000;avgspeed[l]=3.6*distance/avgtime[l];} }//System.out.println("avgtime\t"+Arrays.toString(avgtime));System.out.println("avgspeed\t"+Arrays.toString(avgspeed));//this.chedaoliuliang=chedaoliuliang;//this.avgtime=avgtime;//將兩兩匹配的數據代碼放入數據庫中//System.out.println("this.pipei"+Arrays.deepToString(pipei2));final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//定義SQL server鏈接final String dburl="jdbc:sqlserver://localhost:1433;";try {Class.forName(driver); System.out.println("加載數據庫驅動成功");}//拋出異常 catch(ClassNotFoundException e) { System.out.println("加載數據庫驅動失敗"); }try {Connection conn =DriverManager.getConnection(dburl+"databasename=master","sa","SQLServer"); System.out.println("數據庫連接成功");PreparedStatement pstmt1=conn.prepareStatement("SELECT a,e,f,i,h into table1_all FROM [002].[dbo].[01] where e in (?,?) and f between ? and ?");pstmt1.setString(1,jisuan.kakou1);pstmt1.setString(2,jisuan.kakou2);pstmt1.setString(3,jisuan.time1);pstmt1.setString(4,jisuan.time2);int result1 = pstmt1.executeUpdate();if (result1==0) {System.out.println("執行失敗1\n");}pstmt1=conn.prepareStatement("create table temp1(q int,time1 char(25),time2 char(25),upfx char(25),chepai char(50))");result1 = pstmt1.executeUpdate();if (result1==0) {System.out.println("執行失敗2\n");}int ii=0;//更改pipeifor (;pipei1[ii][0]!=null;ii++) {//更改pipeiString ttime1=pipei1[ii][1];String ttime2=pipei1[ii][5];String tempfx=pipei1[ii][2];String temp=pipei1[ii][0];//更改h//統計在下游卡口time1和time2里多少輛車經過。h是車道//temp是匹配成功的車的車牌號pstmt1=conn.prepareStatement("insert into temp1 values ((select count(*) from table1_all where i=? and h=1 and e = ? and f between ? and ?),?,?,?,?)");pstmt1.setInt(1, jisuan.downfx);pstmt1.setString(2, jisuan.kakou2);pstmt1.setString(3, ttime1);pstmt1.setString(4, ttime2);pstmt1.setString(5, ttime1);pstmt1.setString(6, ttime2);//上游進入方向pstmt1.setString(7, tempfx);pstmt1.setString(8, temp);result1 = pstmt1.executeUpdate();if (result1==0) {System.out.println("執行失敗3\n");}}}catch(SQLException e) {e.printStackTrace();}}//輸入卡口的記錄信息,記錄條數和要求的進口道方向,按方向和車道返回值//進口道方向 1 ↑ 2 ← 3 ↓ 4 →public void chedaoliuliang(String rsx[][],int rsxi,int fx) {int i=0;int j=0;//int j1,j2,j3,j4,j5,j6,j7,j8,j0; String tempchedao;int [] temp= new int [9];//七個車道//一維數組存車牌String [][] chedaochepai = new String [100*100][9];//遍歷數組for (;i<=rsxi-1;i++) {if (rsx[i][2].equals(Integer.toString(fx))) {//賦值車道號tempchedao=rsx[i][3];if (tempchedao.equals("1")) {//賦值車牌chedaochepai[temp[1]][1]=rsx[i][0];temp[1]++;}else if (tempchedao.equals("2")) {//賦值車牌chedaochepai[temp[2]][2]=rsx[i][0];temp[2]++;}else if (tempchedao.equals("3")) {//賦值車牌chedaochepai[temp[3]][3]=rsx[i][0];temp[3]++;}else if (tempchedao.equals("4")) {//賦值車牌chedaochepai[temp[4]][4]=rsx[i][0];temp[4]++;}else if (tempchedao.equals("5")) {//賦值車牌chedaochepai[temp[5]][5]=rsx[i][0];temp[5]++;}else if (tempchedao.equals("6")) {//賦值車牌chedaochepai[temp[6]][6]=rsx[i][0];temp[6]++;}else if (tempchedao.equals("7")) {//賦值車牌chedaochepai[temp[7]][7]=rsx[i][0];temp[7]++;}else if (tempchedao.equals("8")) {//賦值車牌chedaochepai[temp[8]][8]=rsx[i][0];temp[8]++;}else {//賦值車牌chedaochepai[temp[0]][0]=rsx[i][0];temp[0]++;}}else {continue;}}//類變量chedaoliuliang是0123 減一//在 fx做下標數組時全部減一for(;j<=this.chedaoliuliang[fx-1].length-1;j++) {this.chedaoliuliang[fx-1][j]=temp[j];}//System.out.println("chedaochepai"+Arrays.deepToString(chedaochepai));//System.out.println("chedaochepai \t "+Arrays.deepToString(chedaochepai));System.out.println("chedaoliuliang "+fx+" \t"+Arrays.toString(this.chedaoliuliang[fx-1]));}//根據三股車流求區間車速//根據下游交叉口分類//fx為下游交叉口進口道的方向//輸入為每個車在卡口的參數,卡口2 temp2 為下游卡口,fx為上游掉頭經過的下游卡口方向public int [][] qujiantime(String[][] temp1 ,String[][] temp2,int fx) {////定義后一個參數為下游交叉口//設定此目的 在函數調用的時候 正反執行兩遍而不是在放在此函數內,調用一次//debug//static int [] sumtime = new int [8]={0,0,0,0,0,0,0,0};//設置下標 i temp1 j temp2int i=0,j=0;//設置左直右的時間數組//定義7個車道 對應數組第二維的1-7 二維的第0個表示1-7之外的車道(大于7)//第一維記錄每輛車通過kakou1和kakou2的時間int chedaotime[][]=new int[maxitem][9]; //按車道分類//按照下游車道分類for (;j<=temp2.length-1&&temp2[j][0]!=null&&temp2[j][2].equals(Integer.toString(fx));j++) {//循環kakou1(temp1)for (;i<=temp1.length-1&&temp1[i][0]!=null /*&&!temp1[i][3].equals(fx)*/;i++) {//尋找到了匹配車輛//System.out.println("shijian "+Arrays.toString(temp2[0])+"\nshijian"+Arrays.toString(temp1[i]));//關于匹配時間//temp1[j][1] temp2[i][1];//07:00:00 07:05:00//07:03:00//這時候7:03:00要和07:05:00匹配//如果temp1第一個時間小于temp1的賦值//如果temp1第二個時間小于temp1再次賦值,//如果temp1第三個時間大于temp1存在第二列if (temp2[j][0].compareTo(temp1[i][0]) == 0 && /*(temp1[i+1][1]==null) || (*/(temp2[j][1].compareTo(temp1[i][1]) > 0)) {//一號車道if (temp2[j][3].equals("1")) {//debug//System.out.println("gettemp"+Arrays.toString(getpipei(temp1[i],temp2[j])));this.pipei1[this.x1]=getpipei(temp1[i],temp2[j]);this.x1++;//System.out.println("pipei"+Arrays.toString(pipei[j])); chedaotime[j][1]=diffstrtime(temp2[j][1],temp1[i][1]);//System.out.println("xxxxxxxxxxxxxxxxx "+chedaotime[j][3]);//System.out.println("xxxxxxxxxxxxxxxxxtemp1 "+Arrays.deepToString(temp1));//System.out.println("xxxxxxxxxxxxxxxxxtemp2 "+Arrays.deepToString(temp2));//記錄給sumtime,sumtime是static變量,每次執行都保留原來的值//sumtime[1]=chedaotime[j][1]+sumtime[1];//System.out.println("執行了1");//j++; }//二號車道else if (temp2[j][3].equals("2")) {this.pipei2[this.x2]=getpipei(temp1[i],temp2[j]);this.x2++;chedaotime[j][2]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[2]=chedaotime[j][2]+sumtime[2];//System.out.println("執行了2");//j++; }//三號車道else if (temp2[j][3].equals("3")) {this.pipei3[this.x3]=getpipei(temp1[i],temp2[j]);this.x3++;chedaotime[j][3]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[3]=chedaotime[j][3]+sumtime[3];//j++; }//四號車道else if (temp2[j][3].equals("4")) {this.pipei4[this.x4]=getpipei(temp1[i],temp2[j]);this.x4++;chedaotime[j][4]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[4]=chedaotime[j][4]+sumtime[4];//j++; }//五號車道else if (temp2[j][3].equals("5")) {this.pipei5[this.x5]=getpipei(temp1[i],temp2[j]);this.x5++;chedaotime[j][5]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[5]=chedaotime[j][5]+sumtime[5];//j++; }//六號車道else if (temp2[j][3].equals("6")) {this.pipei6[this.x6]=getpipei(temp1[i],temp2[j]);this.x6++;chedaotime[j][6]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[6]=chedaotime[j][6]+sumtime[6];//j++; }//七號車道else if (temp2[j][3].equals("7")) {this.pipei7[this.x7]=getpipei(temp1[i],temp2[j]);this.x7++;chedaotime[j][7]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[7]=chedaotime[j][7]+sumtime[7];//j++; }//八號車道else if (temp2[j][3].equals("8")) {this.pipei8[this.x8]=getpipei(temp1[i],temp2[j]);this.x8++;chedaotime[j][8]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[7]=chedaotime[j][7]+sumtime[7];//j++; }//其他,在8個之外else {chedaotime[j][0]=diffstrtime(temp2[j][1],temp1[i][1]);//sumtime[0]=chedaotime[j][0]+sumtime[0];//System.out.println("執行了else");//j++; }}else if (temp2[j][0].compareTo(temp1[i][0]) == 0 &&temp2[j][1].compareTo(temp1[i][1]) < 0) {break;//先經過卡口2在經過卡口1,篩去 }//未找到(應該是有的如果執行這一步就有問題)else if (temp2[j][0].compareTo(temp1[i][0]) > 0) {System.out.println("車牌不匹配!\n");break;}//未尋找到(車牌號是按升序排序的)else if (temp2[j][0].compareTo(temp1[i][0]) < 0) {System.out.println("未找到(應該是有的如果執行這一步就有問題)");break;}else {//System.out.println("未找到(應該是有的如果執行這一步就有問題)"); }}}//每輛車根據下游車道記錄,記錄完畢//System.out.println(Arrays.deepToString(chedaotime));//運行結果如下//[[0, 0, 70860, 0, 0, 0, 0, 0], //[0, 0, 0, 0, 0, 0, 0, 0], //[0, 0, 0, 0, 0, 0, 0, 0], //。。。。。。。。//[0, 0, 0, 0, 0, 0, 0, 0]]return chedaotime;}//在chepai1和chepai2里尋找相同的車并給類變量數組chepai賦值,liuliang是chepai有效數據的長度//計算流量//考慮到流量是個基本參數,放在類變量里不用函數返回public void liuliang() {int liuliang=0;//定義不重復車牌1和車牌2的數組下標//i chepai1 j chepai2 k chepaiint i=0,j=0,k=0;String temp;//debug//System.out.println(chepai1i+" "+chepai2i);//遍歷經過卡口1不重復的車牌//記錄的條數在數組里減一for(;(i<=chepai1i-1)&&(j<=chepai2i-1);i++) {//賦值臨時變量temp=chepai1[i];//比較chepai1和chepai2的字符串大小(順序)//目的是替換對chepai的遍歷查找//sql中對查找已經asc正序排序if (temp.compareTo(chepai2[j]) == 0) {//debug//System.out.println(temp);this.chepai[k]=temp;//debug//System.out.println("匹配的車輛"+chepai[k]);k++;liuliang++;j++;}else if (temp.compareTo(chepai2[j]) > 0) {//debug//System.out.println(temp+" "+chepai2[j]+" "+i+" "+j);j++;i--;}}//System.out.println(liuliang);this.liuliang=liuliang;}//輸入要查找到車牌,要查找的數組,數組有效長度輸出查找結果的數組//根據chepai數組查找rs1和rs2的位置//返回值為chepai在rs1和rs2中的數組下標//返回值為數組//數組第一個值是元素個數//第一個值下標為1public String[][] find1(String chepai,String[][] rsx,int rsxi) {//定義查詢返回數組 數組大小和find1(存儲下標)對應String [][] singleche = new String [maxitem][4];//設置循環變量//i為rs1的循環變量//j為find1的循環變量int i=0,j=0;//每一個車在卡口的記錄最多有51-1條 (第一個值記錄個數)//int [] find1 = new int[51];//debug//System.out.println("rs1i "+rs1i);//按升序排序的數組//chepai肯定在rs1中間,所以比較的結果是正的//for 循環尋找rs1的下標存在find1里//j<=maxitem-1保證存放每輛車的數組不越界for(;i<=rsxi-1&&j<=maxitem-1;i++) {if (chepai.compareTo(rsx[i][0]) == 0) {//find1[j]=i;//debug//System.out.println("尋找的下標\t"+i+"\t"+Arrays.toString(rsx[i]));//將記錄寫入singleche singleche[j][0]=rsx[i][0];singleche[j][1]=rsx[i][1];singleche[j][2]=rsx[i][2];singleche[j][3]=rsx[i][3];// j++;}else if (chepai.compareTo(rsx[i][0]) > 0) {//debug//System.out.println("執行>0"+i+" "+Arrays.toString(this.rs1[i]));continue;}else if (chepai.compareTo(rsx[i][0]) < 0) {//debug//System.out.println("執行<0"+i+" "+Arrays.toString(this.rs1[i]));break;}}//find1[0]=j-1;//debug//System.out.println(i+" "+j);//debug 對應輸入卡口號輸出每個車的卡口信息System.out.println("singleche "+Arrays.deepToString(singleche));return singleche;//考慮函數的思想,不直接調用類變量,采用參數傳遞/*//同理 //尋找rs2//重置循環變量賦值i=0;j=1;int [] find2 = new int[50];for(;i<=rs2i-1;i++) {if (chepai.compareTo(this.rs2[i][0]) == 0) {find2[j]=i;//debugSystem.out.println("尋找的下標\t"+i+"\t"+Arrays.toString(this.rs2[i]));j++;}else if (chepai.compareTo(this.rs2[i][0]) > 0) {//debug//System.out.println("執行>0"+i+" "+Arrays.toString(this.rs1[i]));continue;}else if (chepai.compareTo(this.rs2[i][0]) < 0) {//debug//System.out.println("執行<0"+i+" "+Arrays.toString(this.rs1[i]));break;}}find2[0]=j-1;*///考慮取出尋找數組 }//計算時間差//返回數值單位為秒//前減后public int diffstrtime(String time1,String time2) {//定義時間差int time;//調用函數將字符串類型轉換成日期類型Date date1=StrToDate(time1);Date date2=StrToDate(time2);//getTime獲取1970-01-01 00:00:00到本時間的時間毫秒數//強制轉換//!!!//int 4 字節 有符號類型的最大值為 2147483647 位//所以最大的時間差為 2147483647/(3600*1000)=396小時time=(int)(date1.getTime()-date2.getTime());//轉化為秒//time=time/1000;return time;}//計算速度public double qujianspeed(double time,double distance) {//定義速度double speed;speed=distance/time;return speed;}//************************//工具函數//字符串轉日期函數 不用staticpublic static Date StrToDate(String str) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");Date date = null;try {date = format.parse(str);} catch (ParseException e) {e.printStackTrace();}return date;}//將日期轉成字符串public static String DateToStr(Date date) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");String time=format.format(date);return time;}//*******************************//從sql中獲取信息;//從其他類獲取sql 函數public void getchepai1(String[] chepai1) {this.chepai1=chepai1;//debug//System.out.println(chepai1[100]); }public void getchepai2(String[] chepai2) {this.chepai2=chepai2;//debug//System.out.println(chepai2[100]); }public void getrs1(String[][] rs1) {this.rs1=rs1;//debug//System.out.println(rs1[100][2]); }public void getrs2(String[][] rs2) {this.rs2=rs2;//debug//System.out.println(rs2[100][2]); }//獲取數量函數public void getchepai1i(int chepai1i) {this.chepai1i=chepai1i;}public void getchepai2i(int chepai2i) {this.chepai2i=chepai2i;}public void getrs1i(int rs1i) {this.rs1i=rs1i;}public void getrs2i(int rs2i) {this.rs2i=rs2i;}//將temp1和temp2合成一起public String[] getpipei(String[] temp1,String[] temp2) {//int i=4;String [] temp3 = new String [8];for (int i=0;i<4;i++) {temp3[i]=temp1[i];}for (int i=0;i<4;i++) {temp3[i+4]=temp2[i];}return temp3;}//類結束//************************ }/*//sql查詢函數和數組存儲函數//查詢卡口public void query(String time1 , String time2 , String kakou1 , String kakou2 ) {//初始化循環變量//int fxi=0;//記錄數組存放的條數int rs1i=0;int rs2i=0;//記錄不重復車輛的條數int chepai1i=0;int chepai2i=0;//執行查詢操作PreparedStatement pstmt1=conn.prepareStatement(sql1);pstmt1.setString(1, kakou1);pstmt1.setString(2, time1);pstmt1.setString(3, time2);//pstmt.setInt(4, fx[fxi]);//獲取結果ResultSet result1 = pstmt1.executeQuery();//逐行(條)讀取數據while(result1.next()){//賦值車牌this.rs1[rs1i][0] = result1.getString(1);//賦值時間this.rs1[rs1i][1] = result1.getString(3);//賦值方向this.rs1[rs1i][2] = result1.getString(4);//賦值車道號this.rs1[rs1i][3] = result1.getString(5);rs1i++;}result1.close();//執行查詢不重復車牌操作PreparedStatement pstmt1x=conn.prepareStatement(sql2);pstmt1x.setString(1, kakou1);pstmt1x.setString(2, time1);pstmt1x.setString(3, time2);ResultSet rschepai = pstmt1x.executeQuery();while(rschepai.next()) {//賦值不重復車牌this.chepai[chepai1i] = rschepai.getString(1);chepai1i++;}rschepai.close();System.out.println("卡口 "+kakou2+" 數量為 "+rs1i);////**************************************************//****************************************************//**************************************************//int rs2i=0;//定義sql語句 distinctString sq2 = "SELECT a,b,c,d FROM " + sheet +" where b = ? and c between ? and ? ";PreparedStatement pstmt2=conn.prepareStatement(sql1);pstmt2.setString(1, kakou2);pstmt2.setString(2, time1);pstmt2.setString(3, time2);//pstmt.setInt(4, fx[fxi]);ResultSet result2 = pstmt2.executeQuery();while(result2.next()){//賦值車牌this.rs2[i][0] = result2.getString(1);//賦值時間this.rs2[i][1] = result2.getString(3);//賦值方向this.rs2[i][2] = result2.getString(4);//賦值車道號this.rs1[i][3] = result1.getString(5);rs2i++; }result2.close();//執行查詢不重復車牌操作PreparedStatement pstmt2x=conn.prepareStatement(sql2);pstmt2x.setString(1, kakou2);pstmt2x.setString(2, time1);pstmt2x.setString(3, time2);ResultSet rschepai2 = pstmt2x.executeQuery();while(rschepai2.next()) {//賦值不重復車牌this.rschepai2[chepai2i] = rschepai2.getString(1);rschepai2i++;}rschepai2.close();System.out.println("卡口 "+kakou2+" 數量為 "+rs2i);}*//*//嘗試將此函數加入到find1中//計算一個車的時間差和速度public void function() {int time,speed;String tempchepai;int[] tempfind1=new int[20];int i=0;jisuan ins=new jisuan();//先取出一個車牌tempchepai=ins.chepai[1];//debug 輸出車牌編號和數據有幾條//System.out.println(tempchepai);tempfind1=ins.find1(tempchepai);//debug//System.out.println("find1 執行完畢");//System.out.println(tempfind1[0]);int tempi=0;//取出rs1的相關數據for (i=1;i<=tempfind1[0];i++) {//tempfind1里存的是chepai在rs1的數組下標tempi=tempfind1[i];//debug//System.out.println("tempchepai"+tempchepai);//System.out.println("rs1[tempi][0]"+rs1[tempi][0]);if (tempchepai.equals(this.rs1[tempi][0])) {System.out.println("車牌為 "+this.rs1[tempi][0]);System.out.println("時間為 "+this.rs1[tempi][1]);System.out.println("方向為 "+this.rs1[tempi][2]);System.out.println("車道為 "+this.rs1[tempi][3]);}else {System.out.println("數據異常");}}}*/
?
轉載于:https://www.cnblogs.com/XUEYEYU/p/10200667.html
總結
以上是生活随笔為你收集整理的车辆匹配和平均车速计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 或许下一分钟是什么歌啊
- 下一篇: 三星堆博物馆老人门票多少钱