天天看点

公交线路查询(WinForm)

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);