VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之用C#开发的一个通用的地铁换乘查询工具(2)

  • 2019-01-20 21:11 来源:未知
找出到达站经过的路线和始发站所在路线的交叉线路 332 var crossLines = GetAcrossLines(depStation, destStation); 333 //分三种情况:1、始发站不是换乘站而到达站是换乘站;2、始发站是换乘站而到达站不是换乘站;3、始发站和到达站都是换乘站,根据情况展开分析 334 if (!IsTransferStation(depStation) && IsTransferStation(destStation)) 335 { 336 #region 情况1:始发站不是换乘站而到达站是换乘站 337 if (crossLines.Count > 0) //依赖交叉线 338 { 339 var listTrans = new List<Station>(); 340 foreach (var line in crossLines) 341 { 342 //找出每条交叉线换乘到始发站线路的所有换乘站 343 var ss = line.GetTransStations() 344 .Where(x => x.TransferNo.IsSame(afterDepLines.First().No)) 345 .ToList(); 346 listTrans.AddRange(ss); 347 } 348 var depIx = GetIndexOnLine(depStation, afterDepLines.First()); 349 var tranStas = 350 listTrans.Select( 351 s => new { sta = s, ix = GetIndexOnLine(s.Name, afterDepLines.First()) }) 352 .ToList(); 353 foreach (var sta in tranStas) 354 { 355 var destIx = GetIndexOnLine(destStation, sta.sta.LineNo); 356 var tranIx = GetIndexOnLine(sta.sta.Name, sta.sta.LineNo); 357 var coss1 = 358 afterDepLines.First() 359 .Stations.Between(depIx, sta.ix) 360 .Select(x => x.Name) 361 .ToList(); 362 var coss2 = 363 GetLine(sta.sta.LineNo) 364 .Stations.Between(destIx, tranIx) 365 .Select(x => x.Name) 366 .ToList(); 367 var rang1 = coss1.Count - 1; 368 var rang2 = coss2.Count - 1; 369 var h = rang1 + rang2; //站点数 370 if (depIx > sta.ix) coss1.Reverse(); 371 if (destIx < tranIx) coss2.Reverse(); 372 373 var rs = msg_transOnce.FormatTo(afterDepLines.First().ToString(), depStation, rang1, 374 sta.sta.Name, 375 GetLine(sta.sta.LineNo).ToString(), rang2, destStation, 376 coss1.ToJoinString(), coss2.Where(x => x != sta.sta.Name).ToJoinString(), h); 377 result.Add(new QueryResult() 378 { 379 Description = rs, 380 Range = h, 381 TransferTimes = 1, 382 TransferStations = new List<string>() { sta.sta.Name } 383 }); 384 } 385 } 386 //查找其他换乘可能 387 var depSta = GetStation(depStation); 388 var trlinesDep = afterDepLines.First().TransferLines.Select(GetLine).ToList(); 389 foreach (var depline in afterDestLines) 390 { 391 var trlineItems = depline.TransferLines.Select(GetLine).ToList(); 392 foreach (var iline in trlineItems) 393 { 394 foreach (var destline in trlinesDep) 395 { 396 var ss = destline.GetAcrossStations(iline); 397 if (!ss.Any()) continue; //3次换乘 398 var slist1 = afterDepLines.First().GetAcrossStations(destline); 399 if (!slist1.Any()) continue; 400 var s1 = slist1.GetClosestStation(depStation); 401 var s1_ix1 = depSta.Index; 402 var s1_ix2 = s1.Index; 403 404 var s1_range = 405 afterDepLines.First() 406 .Stations.Between(s1_ix1, s1_ix2) 407 .Select(x => x.Name) 408 .ToList(); 409 var s1_h = s1_range.Count - 1; 410 if (s1_ix1 > s1_ix2) s1_range.Reverse(); 411 412 var s2 = ss.GetClosestStation(s1.Name); 413 var s2_ix1 = GetIndexOnLine(s1.Name, destline); 414 var s2_ix2 = GetIndexOnLine(s2.Name, destline); 415 var s2_range = destline.Stations.Between(s2_ix1, s2_ix2).Select(x => x.Name).ToList(); 416 var s2_h = s2_range.Count - 1; 417 if (s2_ix1 > s2_ix2) s2_range.Reverse(); 418 419 var slist3 = iline.GetAcrossStations(depline); 420 if (!slist3.Any()) continue; 421 var s3 = slist3.GetClosestStation(s2.Name); 422 var s3_ix1 = s3.Index; 423 var s3_ix2 = GetIndexOnLine(s2.Name, iline); 424 var s3_range = iline.Stations.Between(s3_ix1, s3_ix2).Select(x => x.Name).ToList(); 425 var s3_h = s3_range.Count - 1; 426 if (s3_ix1 < s3_ix2) s3_range.Reverse(); 427 428 var s4_ix1 = GetIndexOnLine(s3.Name, depline); 429 var s4_ix2 = GetIndexOnLine(destStation, depline); 430 var s4_range = depline.Stations.Between(s4_ix1, s4_ix2) 431 .Select(x => x.Name) 432 .ToList(); 433 var s4_h = s4_range.Count - 1; 434 if (s4_ix1 > s4_ix2) s4_range.Reverse(); 435 436 var h = s1_h + s2_h + s3_h + s4_h; 437 if (s2_h == 0 || s3_h == 0 || s4_h == 0 || destline.No.IsSame(iline.No)) continue; 438 var rs = msg_transThreetimes.FormatTo(afterDepLines.First().ToString(), 439 depStation, 440 s1_h, s1.Name, 441 destline.ToString(), s2_h, s2.Name, 442 iline.ToString(), s3_h, s3.Name, 443 depline.ToString(), s4_h, destStation, s1_range.ToJoinString(), 444 s2_range.Where(x => x != s1.Name).ToJoinString(), 445 s3_range.Where(x => x != s2.Name).ToJoinString(), 446 s4_range.Where(x => x != s3.Name).ToJoinString(), h); 447 result.Add(new QueryResult() 448 { 449 Description = rs, 450 Range = h, 451 TransferTimes = 3, 452 TransferStations = 453 new List<string>() 454 { 455 s1.Name, 456 s2.Name, 457 s3.Name 458 } 459 }); 460 } 461 } 462 } 463 #endregion 464 } 465 if (IsTransferStation(depStation) && !IsTransferStation(destStation)) 466 { 467 #region 情况2:始发站是换乘站而到达站不是换乘站 468 var transLines = 469 afterDepLines.Where( 470 x => 471 x.Stations.Exists( 472 y => y.CanTransfer && y.TransferNo.Contains(afterDestLines.First().No))) 473 .ToList(); 474 if (transLines.Any()) 475 { 476 var clist = 477 GetAcrossStations(transLines.First(), afterDestLines.First()) 478 .Select(x => x.Name) 479 .ToList(); 480 var i = GetIndexOnLine(depStation, transLines.First()); 481 var j = GetIndexOnLine(clist.First(), transLines.First()); 482 var k = GetIndexOnLine(destStation, afterDestLines.First()); 483 var l = GetIndexOnLine(clist.First(), afterDestLines.First()); 484 var coss1 = transLines.First().Stations.Between(i, j).Select(x => x.Name).ToList(); 485 var coss2 = afterDestLines.First().Stations.Between(k, l).Select(x => x.Name).ToList(); 486 var rang1 = coss1.Count - 1; 487 var rang2 = coss2.Count - 1; 488 var h = rang1 + rang2; //站点数 489 if (i > j) coss1.Reverse(); 490 if (k < l) coss2.Reverse(); 491 492 var rs = msg_transOnce.FormatTo(transLines.First().ToString(), depStation, rang1, 493 clist.First(), 494 afterDestLines.First().ToString(), rang2, destStation, 495 coss1.ToJoinString(), coss2.Where(x => x != clist.First()).ToJoinString(), h); 496 result.Add(new QueryResult() 497 { 498 Description = rs, 499 Range = h, 500 TransferTimes = 1, 501 TransferStations = new List<string>() { clist.First() } 502 }); 503 } 504 //寻找其他换乘可能 505 var destSta = GetStation(destStation); 506 var trlinesDest = afterDestLines.First().TransferLines.Select(GetLine).ToList(); 507 foreach (var depline in afterDepLines) 508 { 509 var trlineItems = depline.TransferLines.Select(GetLine).ToList(); 510 foreach (var iline in trlineItems) 511 { 512 foreach (var destline in trlinesDest) 513 { 514 var ss = iline.GetAcrossStations(destline); 515 if (!ss.Any()) continue; //3次换乘 516 var slist1 = depline.GetAcrossStations(iline); 517 if (!slist1.Any()) continue; 518 var s1 = slist1.GetClosestStation(depStation); 519 var s1_ix1 = GetIndexOnLine(depStation, depline); 520 var s1_ix2 = s1.Index; 521 var s1_range = 522 depline.Stations.Between(s1_ix1, s1_ix2) 523 .Select(x => x.Name) 524 .ToList(); 525 var s1_h = s1_range.Count - 1; 526 if (s1_ix1 > s1_ix2) s1_range.Reverse(); 527 528 var s2 = ss.GetClosestStation(s1.Name); 529 var s2_ix1 = GetIndexOnLine(s1.Name, iline); 530 var s2_ix2 = GetIndexOnLine(s2.Name, iline); 531 var s2_range = iline.Stations.Between(s2_ix1, s2_ix2).Select(x => x.Name).ToList(); 532 var s2_h = s2_range.Count - 1; 533 if (s2_ix1 > s2_ix2) s2_range.Reverse(); 534 535 var slist3 = destline.GetAcrossStations(afterDestLines.First()); 536 if (!slist3.Any()) continue; 537 var s3 = slist3.GetClosestStation(ss.First().Name); 538 var s3_ix1 = s3.Index; 539 var s3_ix2 = GetIndexOnLine(s2.Name, destline); 540 var s3_range = destline.Stations.Between(s3_ix1, s3_ix2).Select(x => x.Name).ToList(); 541 var s3_h = s3_range.Count - 1; 542 if (s3_ix1 < s3_ix2) s3_range.Reverse(); 543 544 var s4_ix1 = GetIndexOnLine(s3.Name, afterDestLines.First()); 545 var s4_ix2 = destSta.Index; 546 var s4_range = 547 afterDestLines.First() 548 .Stations.Between(s4_ix1, s4_ix2) 549 .Select(x => x.Name) 550 .ToList(); 551 var s4_h = s4_range.Count - 1; 552 if (s4_ix1 > s4_ix2) s4_range.Reverse(); 553 554 var h = s1_h + s2_h + s3_h + s4_h; 555 if (s1_h == 0 || s2_h == 0 || s3_h == 0 || iline.No.IsSame(destline.No)) continue; 556 var rs = msg_transThreetimes.FormatTo(depline.ToString(), depStation, 557 s1_h, s1.Name, 558 iline.ToString(), s2_h, s2.Name, 559 destline.ToString(), s3_h, s3.Name, 560 afterDestLines.First().ToString(), s4_h, destStation, 561 s1_range.ToJoinString(), 562 s2_range.Where(x => x != s1.Name).ToJoinString(), 563 s3_range.Where(x => x != s2.Name).ToJoinString(), 564 s4_range.Where(x => x != s3.Name).ToJoinString(), h); 565 result.Add(new QueryResult() 566 { 567 Description = rs, 568 Range = h, 569 TransferTimes = 3, 570 TransferStations = 571 new List<string>() 572 { 573 s1.Name, 574 s2.Name, 575 s3.Name 576 } 577 }); 578 } 579 } 580 } 581 #endregion 582 } 583 if (IsTransferStation(depStation) && IsTransferStation(destStation)) 584 { 585 #region 情况3:始发站和到达站都是换乘站 586 if (crossLines.Count > 0) //依赖交叉线 587 { 588 var transStations = GetClosestStation(depStation, true); 589 if ( 590 !transStations.Exists( 591 x => 592 crossLines.Exists(y => y.Stations.Exists(z => x.TransferNo.Split(',').Intersect(z.LineNo.Split(',')).Any())))) 593 { 594 transStations = new List<Station>(); 595 afterDepLines.ForEach(x => 596 { 597 var ctrans = x.GetTransStations(); 598 var ctrans2 = 599 ctrans.Where( 600 y => 601 crossLines.Exists( 602 z => z.Stations.Exists(zz => y.TransferNo.Split(',').Intersect(zz.LineNo.Split(',')).Any()))) 603 .ToList(); 604 transStations.AddRange(ctrans2); 605 }); 606 } 607 var transLine = 608 afterDestLines.Where( 609 x => 610 x.Stations.Exists(y => transStations.Exists(z => z.Name.IsSame(y.Name)))) 611 .ToList(); 612 var intersStas = 613 transStations.Where( 614 x => 615 transLine.Exists( 616 y => 617 y.Stations.Exists( 618 z => z.Name.IsSame(x.Name) && x.TransferNo.Split(',').Intersect(z.LineNo.Split(',')).Any()))) 619 .ToList(); 620 foreach (var line in transLine) 621 { 622 //分别获取换乘站在换乘线上的索引 623 foreach (var t in intersStas) 624 { 625 var ix = GetIndexOnLine(destStation, line); //目的站在换乘线上的索引 626 var iix = GetIndexOnLine(t.Name, line); //换乘站在换乘线上的索引 627 if (iix == -1) continue; 628 var ix2 = GetIndexOnLine(depStation, t.LineNo); //始发站在换乘站所在线路上的索引 629 var iix2 = GetIndexOnLine(t.Name, t.LineNo); //换乘站在始发站所在线路上的索引 630 631 var ixRange = line.Stations.Between(ix, iix).Select(x => x.Name).ToList(); 632 var ixRange2 = GetLine(t.LineNo).Stations.Between(ix2, iix2).Select(x => x.Name).ToList(); 633 var ixh = ixRange.Count - 1; 634 var ixh2 = ixRange2.Count - 1; 635 var h = ixh + ixh2; 636 if (ix < iix) ixRange.Reverse(); 637 if (ix2 > iix2) ixRange2.Reverse(); 638 var rs = msg_transOnce.FormatTo(GetLine(t.LineNo).ToString(), depStation, ixh2, t.Name, 639 line.ToString(), ixh, destStation, 640 ixRange2.ToJoinString(), 641 ixRange.Where(x => !x.IsSame(t.Name)).ToJoinString(), h); 642 result.Add(new QueryResult() 643 { 644 Description = rs, 645 Range = h, 646 TransferTimes = 1, 647 TransferStations = new List<string>() { t.Name } 648 }); 649 } 650 } 651 } 652 #endregion 653 } 654 } 655 #endregion 656 } 657 658 //查找其他可能性 659 #region 深度挖掘其他方案 660 //找出经过始发站和到达站的线路中共同穿过的公共线路,寻找换乘方案 661 var connLines = GetCommonLines(depStation, destStation); 662 if (connLines.Count > 0) 663 { 664 var transDep = new List<Station>(); 665 var transDest = new List<Station>(); 666 foreach (var depLine in afterDepLines) 667 { 668 //在经过始发站的线路中的站点中找到可换乘到公共线路的站点 669 var trans = depLine.GetTransStations() 670 .Where(x => x.Name != depStation && x.Name != destStation && connLines.Exists(y => x.TransferNo.Contains(y.No))) 671 .ToList(); 672 transDep.AddRange(trans); 673 } 674 foreach (var destLine in afterDestLines) 675 { 676 //在经过到达站的线路中的站点中找到可换乘到公共线路的站点 677 var trans = destLine.GetTransStations() 678 .Where(x => x.Name != depStation && x.Name != destStation && connLines.Exists(y => x.TransferNo.Contains(y.No))) 679 .ToList(); 680 transDest.AddRange(trans); 681 } 682 foreach (var d1 in transDep) 683 { 684 foreach (var d2 in transDest) 685 { 686 //找出交叉站点,即可换乘同一条公共线路的站点 687 var inters = d1.TransferNo.Split(',').Intersect(d2.TransferNo.Split(',')).ToList(); 688 if (!inters.Any() || d1.Name.IsSame(d2.Name)) continue; 689 var tranLine = GetLine(inters.First()); 690 var depLine = GetLine(d1.LineNo); 691 var destLine = GetLine(d2.LineNo); 692 var ix1 = GetIndexOnLine(depStation, depLine); 693 var ix2 = GetIndexOnLine(d1.Name, depLine); 694 var iix1 = GetIndexOnLine(d1.Name, tranLine); 695 var iix2 = GetIndexOnLine(d2.Name, tranLine); 696 var iiix1 = GetIndexOnLine(d2.Name, destLine); 697 var iiix2 = GetIndexOnLine(destStation, destLine); 698 699 var depRange = depLine.Stations.Between(ix1, ix2).Select(x => x.Name).ToList(); 700 var transRange = tranLine.Stations.Between(iix1, iix2).Select(x => x.Name).ToList(); 701 var destRange = destLine.Stations.Between(iiix1, iiix2).Select(x => x.Name).ToList(); 702 var r1 = depRange.Count - 1; 703 var r2 = transRange.Count - 1; 704 var r3 = destRange.Count - 1; 705 var r = r1 + r2 + r3; 706 if (ix1 > ix2) depRange.Reverse(); 707 if (iix1 > iix2) transRange.Reverse(); 708 if (iiix1 > iiix2) destRange.Reverse(); 709 string rs; 710 if (r1 > 0 && r2 > 0 && r3 > 0) 711 { 712 rs = msg_transTwice.FormatTo(depLine.ToString(), depStation, r1, d1.Name, tranLine.ToString(), r2, 713 d2.Name, destLine.ToString(), r3, destStation, depRange.ToJoinString(), 714 transRange.Where(x => !x.IsSame(d1.Name)).ToJoinString(), 715 destRange.Where(x => !x.IsSame(d1.Name) && !x.IsSame(d2.Name)).ToJoinString(), r); 716 result.Add(new QueryResult() 717 { 718 Description = rs, 719 Range = r, 720 TransferTimes = 2, 721 TransferStations = new List<string>() { d1.Name, d2.Name } 722 }); 723 } 724 else if (r1 > 0 && r2 == 0 && r3 > 0) 725 { 726 rs = msg_transOnce.FormatTo(GetLine(inters.First()).ToString(), depStation, r1, d2.Name, 727 destLine.ToString(), r3, destStation, 728 depRange.ToJoinString(), 729 destRange.Where(x => !x.IsSame(d2.Name)).ToJoinString(), r); 730 result.Add(new QueryResult() 731 { 732 Description = rs, 733 Range = r, 734 TransferTimes = 1, 735 TransferStations = new List<string>() { d2.Name } 736 }); 737 } 738 else 739 { 740 rs = msg_transOnce.FormatTo(depLine.ToString(), depStation, r1, d1.Name, tranLine.ToString(), r2, 741 destStation, depRange.ToJoinString(), 742 transRange.Where(x => !x.IsSame(d1.Name)).ToJoinString(), r); 743 result.Add(new
相关教程