1. common.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Friday_A
{
class common
{
}
/// <summary>
/// 儲存資料資訊
/// </summary>
class BusInfo
public string strLineName; //車次名稱
public string strLineMemo; //車次備注
public string strLines; //行車路線
/// 儲存要輸出的資訊
class nonstopLine
public string strLineName;
public string strlineCount; //行車站數
public string strLines;
class BusLineName
/// <summary>
/// 計算站點在數組中的索引
/// </summary>
/// <param name="lines">數組</param>
/// <param name="value">站點</param>
/// <returns></returns>
public static int IndexOfArray(string[] lines, string value)
{
for (int i = 0; i < lines.Length;i++ )
{
if (lines[i] == value)
{
return i;
}
}
return -1;
}
}
-----------------------------------------------------------------------------------------
2. GetData.cs
using System.Data;
using System.Data.OleDb;
using System.Collections;
class GetData
public static ArrayList GetLines()
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=busline.mdb";
string sql = "select * from 公交資訊表";
OleDbCommand comm = new OleDbCommand(sql,conn);
conn.Open();
OleDbDataReader oddr = comm.ExecuteReader();
ArrayList al = new ArrayList(); //存儲公交資訊
while(oddr.Read())
BusInfo bi = new BusInfo();
bi.strLineName = oddr["車次"].ToString();
bi.strLineMemo = oddr["車次備注"].ToString();
bi.strLines = oddr["行車路線"].ToString();
al.Add(bi);
conn.Close();
return al;
-----------------------------------------------------------------------------------------------
3. Form1.cs
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading; //引用線程命名空間
public partial class Form1 : Form
public Form1()
InitializeComponent();
ArrayList busLine = new ArrayList(); //存放車次資訊
bool loadComplete = false; //标志線程加載是否結束
private void Form1_Load(object sender, EventArgs e)
Thread t = new Thread(new ThreadStart(GetLoadData));//定義一個線程,用來加載資料
t.Start();//打開線程
private void button1_Click(object sender, EventArgs e)
string strStart = textBox1.Text;
string strEnd = textBox2.Text;
if(strStart.Trim().Length==0)
MessageBox.Show("哥們兒,從哪上車啊?","乘務員說:",MessageBoxButtons.OK,MessageBoxIcon.Information);
textBox1.Text = "";
textBox1.Focus();
return;
if (strEnd.Trim().Length == 0)
MessageBox.Show("哥們兒,從哪下車啊?", "乘務員說:", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox2.Text = "";
textBox2.Focus();
if (!loadComplete)
MessageBox.Show("各位乘客,前方車多人多,車輛行駛緩慢,請諒解!","乘務員說:",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
ArrayList al = NonstopLines(strStart, strEnd);
if(al.Count>0)
richTextBox1.Text = "從 " + strStart + " 開往 " + strEnd + "的車輛\n\n";
myCompare mc = new myCompare(); //排序器執行個體化
al.Sort(mc);//使用排序器
for(int i=0;i<al.Count;i++)
nonstopLine nl = (nonstopLine)al[i];
richTextBox1.Text=richTextBox1.Text+"車次:"+nl.strLineName+"\n";
richTextBox1.Text=richTextBox1.Text+"站數:"+nl.strlineCount+"\n";
richTextBox1.Text=richTextBox1.Text+"線路:"+nl.strLines+"\n\n";
class myCompare:IComparer //定義一個排序器
#region IComparer 成員
public int Compare(object x, object y)
nonstopLine n1 = (nonstopLine)x;
nonstopLine n2 = (nonstopLine)y;
int a = Convert.ToInt16(n1.strlineCount);
int b = Convert.ToInt16(n2.strlineCount);
if (a > b)
return 1;//a>b 傳回1:正序排列;傳回-1倒序排列
else if(a<b)
return -1;
else
return 0;
#endregion
private void GetLoadData()
busLine = GetData.GetLines();//加載資料
loadComplete = true; //加載結束,标志為true
private ArrayList NonstopLines(string strStart, string strEnd)
ArrayList al = new ArrayList();
for (int i = 0; i < busLine.Count;i++ ) //循環查找每輛直達車
BusInfo c = (BusInfo)busLine[i]; //将數組重新定義
string[] arrLines = c.strLines.Split("-".ToCharArray());//将行車路線拆分,放進數組
int iUpLine = BusLineName.IndexOfArray(arrLines,strStart); //公共汽車上下行判斷
int iDownLine = BusLineName.IndexOfArray(arrLines, strEnd);
if (iUpLine >= 0 && iDownLine >= 0)
nonstopLine nl = new nonstopLine(); //執行個體化nonstopLine類
nl.strLineName = c.strLineName; //将從資料庫讀出的車次資訊賦給要輸出的車次資訊
nl.strlineCount = Convert.ToString(Math.Abs(iUpLine-iDownLine));//計算行車站數
string strLines = ""; //定義一個字元串變量,用來儲存行車路線站點
if (iUpLine > iDownLine)
{
for (int j = iUpLine; j >= iDownLine; j--)
{
strLines += arrLines[j] + "-";
}
}
else
for (int j = iUpLine; j <= iDownLine;j++ )
strLines = strLines.Substring(0, strLines.Length - 1); //去掉字元串最後面的"-"
nl.strLines = strLines;
al.Add(nl);