当前位置:编程学习 > C#/ASP.NET >>

编程经验不足,请高手帮忙将两程序整合

本人编程经验不足,请高手帮忙将两程序整合,一个是后台算法,一个是界面。请大家帮帮忙吧! 新人在此先谢过大家
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[][] lines = new string[5][];
            lines[0] = new string[] { "A", "C", "E", "G", "I", "K", "M", "O" };
            lines[1] = new string[] { "B", "D", "F", "H", "J", "L", "N", "P" };
            lines[2] = new string[] { "A", "C", "E", "L", "Q", "W" };
            lines[3] = new string[] { "B", "E", "F", "J", "Q", "L", "M", "H" };
            lines[4] = new string[] { "E", "C", "L", "H", "J", "M", "O" };

            string start = "A", end = "L", change;
            int startID, endID, changeIDinStartLine, changeIDinEndLine;
            int startLineID, endLineID;
            int stopsCount = 0;
            int solutionsCount = 0;

            Console.WriteLine("We've got " + lines.Length + " line(s) in total:");
            for (int i = 0; i < lines.Length; i++)
            {
                Console.Write("line[" + i + "]:");
                for (int j = 0; j < lines[i].Length; j++)
                    Console.Write(lines[i][j] + " ");
                Console.WriteLine();
            }

            Console.WriteLine("\n\nNow you are going FROM [" + start + "] TO [" + end + "]\n\n");

            for (startLineID = 0; startLineID < lines.Length; startLineID++)
            {
                //依次测试每条线路,判断起点是否在其中
                startID = belongTo(start, lines[startLineID]);
                if (startID >= 0)//一条包含起点的线路
                {
                    //判断终点是否也在此线路中
                    endID = belongTo(end, lines[startLineID]);
                    if (endID >= 0)//终点也在此线路中
                    {
                        stopsCount = Math.Abs(startID - endID);
                        Console.WriteLine("Solution(" + (++solutionsCount) + "): start from [" + start + "]   in line[" + startLineID + "]   end at [" + end + "]\n" + stopsCount + " stop(s)\n");
                        continue;//跳过剩余操作,直接回到最外层循环,判断起点是否在余下的线路中
                    }
                    //终点和起点不在一条线路中时,需要测试终点是否在其他线路
                    for (endLineID = 0; endLineID < lines.Length; endLineID++)
                    {
                        //依次测试每条线路,判断终点是否在其中
                        endID = belongTo(end, lines[endLineID]);
                        if (endID >= 0)
                            //起点线路中,除了起点之外的其他站点依次进行测试,判断是否在终点线路中
                            for (changeIDinStartLine = 0; changeIDinStartLine < lines[startLineID].Length; changeIDinStartLine++)
                            {
                                if (changeIDinStartLine != startID)
                                {
                                    change = lines[startLineID][changeIDinStartLine];
                                    changeIDinEndLine = belongTo(change, lines[endLineID]);

                                    if (changeIDinEndLine >= 0)
                                    {
                                        stopsCount = Math.Abs(startID - changeIDinStartLine) + Math.Abs(endID - changeIDinEndLine);
                                        Console.WriteLine("Solution(" + (++solutionsCount) + "): start from [" + start + "]   in line[" + startLineID + "]   change at [" + change + "]   to line[" + endLineID + "]   end at [" + end + "]\n" + stopsCount + " stop(s)\n");
                                    }
                                }
                            }
                    }
                }
            }

            Console.WriteLine("\n(" + solutionsCount + ") solution(s)");

        }

        //判断站点是否属于某线路的方法:是,返回站点在线路中的数组下标;否,返回-1
        static int belongTo(string target, string[] line)
        {
            for (int i = 0; i < line.Length; i++)
                if (line[i] == target)
                    return i;
            return -1;
        }

    }

}
这个是后台算法


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

namespace 公交查询系统__2._1_
{
    public partial class Form1 : Form
    {
        static string[] arrd = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q" }, arrh ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q" };
         
     
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.richTextBox1.Text = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);

            this.richTextBox1.Text = doit(this.comboBox1.GetItemText(this.comboBox1.SelectedItem), this.comboBox2.GetItemText(this.comboBox2.SelectedItem));
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.richTextBox1.Text = this.comboBox2.GetItemText(this.comboBox2.SelectedItem);

            this.richTextBox1.Text = doit(this.comboBox1.GetItemText(this.comboBox1.SelectedItem), this.comboBox2.GetItemText(this.comboBox2.SelectedItem));
        }
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private string doit(string start, string end) { return "result"; } 
    }
}
这个是界面
谢谢大家了!!
--------------------编程问答-------------------- 帮顶一下!! --------------------编程问答-------------------- 顶顶。 --------------------编程问答-------------------- 你这是在做什么?要做什么?要整合什么?看不懂你在干什么. --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 是不是有病啊 --------------------编程问答-------------------- 一个控制台程序一个WINFORM程序..
汗.... --------------------编程问答-------------------- 把  static   void   Main(string[]   args) 做个第二个程序里的函数不就行了么?
--------------------编程问答-------------------- 不太清楚你要取用控制台的哪个变量?还是要取用string[] args ,还是取用。。。?
你就直接改成模板类算了,全设成公共函数,然后自己调用。
class   Programs'这里改个名字,以防冲突 

    public static   void   Mains(string[]   args) 
    { 
                        string[][]   lines   =   new   string[5][]; 
                        lines[0]   =   new   string[]   {   "A",   "C",   "E",   "G",   "I",   "K",   "M",   "O"   }; 
                        lines[1]   =   new   string[]   {   "B",   "D",   "F",   "H",   "J",   "L",   "N",   "P"   }; 
                        lines[2]   =   new   string[]   {   "A",   "C",   "E",   "L",   "Q",   "W"   }; 
                        lines[3]   =   new   string[]   {   "B",   "E",   "F",   "J",   "Q",   "L",   "M",   "H"   }; 
                        lines[4]   =   new   string[]   {   "E",   "C",   "L",   "H",   "J",   "M",   "O"   }; 

                        string   start   =   "A",   end   =   "L",   change; 
                        int   startID,   endID,   changeIDinStartLine,   changeIDinEndLine; 
                        int   startLineID,   endLineID; 
                        int   stopsCount   =   0; 
                        int   solutionsCount   =   0; 

                        Console.WriteLine("We've   got   "   +   lines.Length   +   "   line(s)   in   total:"); 
                        for   (int   i   =   0;   i   <   lines.Length;   i++) 
                        { 
                                Console.Write("line["   +   i   +   "]:"); 
                                for   (int   j   =   0;   j   <   lines[i].Length;   j++) 
                                        Console.Write(lines[i][j]   +   "   "); 
                                Console.WriteLine(); 
                        } 

                        Console.WriteLine("\n\nNow   you   are   going   FROM   ["   +   start   +   "]   TO   ["   +   end   +   "]\n\n"); 

                        for   (startLineID   =   0;   startLineID   <   lines.Length;   startLineID++) 
                        { 
                                //依次测试每条线路,判断起点是否在其中 
                                startID   =   belongTo(start,   lines[startLineID]); 
                                if   (startID   > =   0)//一条包含起点的线路 
                                { 
                                        //判断终点是否也在此线路中 
                                        endID   =   belongTo(end,   lines[startLineID]); 
                                        if   (endID   > =   0)//终点也在此线路中 
                                        { 
                                                stopsCount   =   Math.Abs(startID   -   endID); 
                                                Console.WriteLine("Solution("   +   (++solutionsCount)   +   "):   start   from   ["   +   start   +   "]       in   line["   +   startLineID   +   "]       end   at   ["   +   end   +   "]\n"   +   stopsCount   +   "   stop(s)\n"); 
                                                continue;//跳过剩余操作,直接回到最外层循环,判断起点是否在余下的线路中 
                                        } 
                                        //终点和起点不在一条线路中时,需要测试终点是否在其他线路 
                                        for   (endLineID   =   0;   endLineID   <   lines.Length;   endLineID++) 
                                        { 
                                                //依次测试每条线路,判断终点是否在其中 
                                                endID   =   belongTo(end,   lines[endLineID]); 
                                                if   (endID   > =   0) 
                                                        //起点线路中,除了起点之外的其他站点依次进行测试,判断是否在终点线路中 
                                                        for   (changeIDinStartLine   =   0;   changeIDinStartLine   <   lines[startLineID].Length;   changeIDinStartLine++) 
                                                        { 
                                                                if   (changeIDinStartLine   !=   startID) 
                                                                { 
                                                                        change   =   lines[startLineID][changeIDinStartLine]; 
                                                                        changeIDinEndLine   =   belongTo(change,   lines[endLineID]); 

                                                                        if   (changeIDinEndLine   > =   0) 
                                                                        { 
                                                                                stopsCount   =   Math.Abs(startID   -   changeIDinStartLine)   +   Math.Abs(endID   -   changeIDinEndLine); 
                                                                                Console.WriteLine("Solution("   +   (++solutionsCount)   +   "):   start   from   ["   +   start   +   "]       in   line["   +   startLineID   +   "]       change   at   ["   +   change   +   "]       to   line["   +   endLineID   +   "]       end   at   ["   +   end   +   "]\n"   +   stopsCount   +   "   stop(s)\n"); 
                                                                        } 
                                                                } 
                                                        } 
                                        } 
                                } 
                        } 

                        Console.WriteLine("\n("   +   solutionsCount   +   ")   solution(s)"); 

                } 

                //判断站点是否属于某线路的方法:是,返回站点在线路中的数组下标;否,返回-1 
                public static   int   belongTo(string   target,   string[]   line) 
                { 
                        for   (int   i   =   0;   i   <   line.Length;   i++) 
                                if   (line[i]   ==   target) 
                                        return   i; 
                        return   -1; 
                } 

        } 


补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,