日期:2014-05-18  浏览次数:20946 次

关于C#的公车线路调用的程序,求各位大神帮忙
我假定了10条线路,button1的作用是起点到终点坐一条线直达,button2的作用是需换车的,但是button2有错误,运行不出来。(textBox1是起点,textBox2是终点)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace bus_station
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }
  string[] bus = new string[10];

  private void button1_Click(object sender, EventArgs e)
  {
  string path = " ";
  string aa = " ";
  for (int i = 0; i < bus.Length; i++)
  {
  aa = bus[i];
  if ((aa.IndexOf(textBox1.Text) >= 0) && (aa.IndexOf(textBox2.Text) >= 0))
  {
  string[] b = aa.Split(',');
  path += b[0];
  }
  }
  if (path != " ") textBox3.Text = path;
  else textBox3.Text = "没有合适的线路";

  }


   
  private string tr(string a, string b)
  {
  string ret = " ";
  string[] a1 = a.Split(',');
  string[] b1 = b.Split(',');
  for (int i = 0; i < a1.Length; i++)
  {
  for (int j = 0; j < b1.Length; j++)
  {
  if (a1[i] == b1[j])
  {
  if (ret == " ") ret = a1[i];

  else ret += "," + a1[i];
  }
  }
  }
  return ret;  
  }
   

  private void button2_Click(object sender, EventArgs e)
  {
   
   
  string path = " ";
  string aa = " ", bb = " ",cc=" ";
  for (int i = 0; i <bus.Length; i++)
  {
  aa = bus[i];
  if (aa.IndexOf(textBox1.Text) >= 0)
  {
  for (int j = 0; j < bus.Length; j++)
  if (i != j)
  {
  bb = bus[j];
  if (bb.IndexOf(textBox2.Text) >= 0)
  {
  cc = tr(aa, bb);
  if (cc != " ")
  path += aa.Substring(0, 4) + "→" + cc+"转" + bb.Substring(0, 4) + "→" + textBox2.Text;
  }
  }
  }
  }
  if (path != " ") textBox3.Text = path;
  else textBox3.Text = "没有合适的线路";

  }

  private void Form1_Load(object sender, EventArgs e)
  {
  bus[0] = "#1,10,11,12,13,14,15,16,17,18,19";
  bus[1] = "#2,20,21,22,23,24,25,2