using System;
using System.Collections.Generic;
using System.Text;namespace QuickSort
{
    
    class Program
    {
   
        static void Main(string[] args)
        {
            int[] array ={1,4,10,6,7,5,2,5};   //<<1>>
           
            QuickSort(ref array,0,7);   //<<1>>
            foreach(int i in array)
            {
                Console.WriteLine("{0}",i);   //<<8>>
            }
        }
        public static void QuickSort(ref int[] arr, int startpos, int endpos)
        {
            int i = startpos, j = endpos;   //<<5>>
            int temp;   //<<5>>
            int tj = endpos;   //<<5>>
            dat da = new dat(0, 0);   //<<5>>
            Stack<dat> stk = new Stack<dat>();   //<<5>>            temp = arr[i];   //<<5>>
            while (i < j)
            {
                while (i < j && arr[j] >= temp) --j;   //<<7>>
                arr[i] = arr[j];   //<<7>>
                while (i < j && arr[i] < temp) ++i;   //<<7>>
                arr[j] = arr[i];   //<<7>>
            }
            arr[i] = temp;   //<<5>>
            if (startpos < i - 1) QuickSort(ref arr,startpos,i-1);   //<<5>>
            if (endpos > i + 1) QuickSort(ref arr, i+1, endpos);   //<<5>>        }
                  /*      if (tj > j + 1)   
                    {
                        stk.Push(new dat(j + 1, tj));   //<<>>
                        i = startpos;   //<<>>
                        j = j - 1;   //<<>>
                        tj = j;   //<<>>
                    }
                    else 
                    if(startpos==j-1)
                    { 
                        da = stk.Pop();   //<<>>
                        i = da.i;   //<<>>
                        j = da.j;   //<<>>
                    };   //<<>>
                } while (j > startpos && stk.Count != 0);   //<<>>*/
    }
    public struct dat
    {
        public int i;
        public int j;        public dat(int a, int b)
        {
            i = a;
            j = b;
        }
    }
}这是处理后的结果。看完编程珠玑,自己也写个C#版的行计数器,嘿嘿。

解决方案 »

  1.   

    写的垃圾,也没什么注释,没好意思贴出来!-_-!!!
    实现过程是读取cs源程序,然后修改成一个新程序,也就是在每段代码后插入一个计数器(count[i++])..
    在程序结束前把count[]存起来。然后再把count里的值取出来,在插入源代码using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    using System.Threading;
    namespace Monitor
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                MarkList.Add("for");
                MarkList.Add("break");
                MarkList.Add("//");
                MarkList.Add("continue");
                MarkList.Add("return");
            }
            List<string> MarkList = new List<string>();
            private void openSourceFileToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (DialogResult.OK == openFileDialog1.ShowDialog())
                {
                   fileName= openFileDialog1.FileName;
                   StreamReader SR = new StreamReader(fileName);
                    rtb_srcfile.Text = SR.ReadToEnd();
                    srcfile = rtb_srcfile.Text;
                    SR.Close();
                }
            }
            string srcfile = "";
            string modifyFile = @"D:\modifyFile.cs";
            string fileName = "";
            string usingStatement = "using System.IO;";
            string defineVar = "   static int[] Counts = new int[1000];"+
                                 '\n' + "static string str_MonitorRst=\"\";" +
                                 "        static string path=@\"" +"d:\\MonitorTemp.txt\";";
            string  MonitorTempPath=@"d:\MonitorTemp.txt";
          
            string doCount;
            string SaveArrPro = //'\n' + "static void SaveArr()" + '\n' +
                                //"{" +
                                  " for (int j = 0; Counts[j]!=0 ; j++)\n" +
                                     "{\n" +
                                        "str_MonitorRst+=Counts[j].ToString()+" + "',';\n" +
                                     "}\n" +
                                        " StreamWriter sw = File.CreateText(path);\n" +
                                         "sw.Write(str_MonitorRst);" + '\n' + "sw.Close();\n";
                                         
            string  str_sourceFile;
            private void runToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (rtb_srcfile.Text == "")
                    return;            str_sourceFile = rtb_srcfile.Text;
                str_sourceFile= str_sourceFile.Insert(0,usingStatement);//插入using System.IO;
                int startIndex= TradSearch("class", str_sourceFile, true);           InserCount(ref str_sourceFile,startIndex,true,new string[0]);
               int ci= TradSearch("class", str_sourceFile, true);
               if (ci != -1)
               {
                   str_sourceFile = str_sourceFile.Insert(ci+1, defineVar);   //在class定义后插入全局变量
               }
               int mi= TradSearch("Main", str_sourceFile, false);
               if (mi != -1)
               {
                  int si=TradSearch("Main", str_sourceFile, true);              //在main里查找return
                  int ri= SubStringBetween(str_sourceFile,"return",si,mi);     //如果有return的话插在return前面
                  if (ri != -1)
                  {
                      str_sourceFile = str_sourceFile.Insert(ri - 1, SaveArrPro);
                  }
                  else { str_sourceFile = str_sourceFile.Insert(mi - 1, SaveArrPro); }
               }
               
               StreamWriter sw = File.CreateText(modifyFile);
               sw.WriteLine(str_sourceFile);
               sw.Close();
                doCompiler(modifyFile);
                string[] CountArr = new string[] { };
                CountArr = File.ReadAllText(MonitorTempPath).Split(',');
                InserCount(ref srcfile,startIndex,false,CountArr);            rtb_srcfile.Text = "";
                rtb_srcfile.Text= srcfile;            
            }        
            int SubStringBefore(string s, string sub, int startIndex)
            {
                int subn = sub.Length - 1;
                for (int j = startIndex; s[j] != '\n'&&j>subn-1; j--)
                {
                    int i = subn;
                    for (; i > -1; i--)
                    {
                        if (s[j - ( subn- i)] != sub[i]) break;
                    }
                    if (i == -1) return j;
                }
                return -1;
            }
            int  SubStringBetween(string s, string sub, int startIndex, int endIndex)
            {
                int subn = sub.Length;
                int sn = endIndex;
                for (int j = startIndex; j <= sn - subn; j++)
                {
                    int i = 0;
                    for (; i < subn; i++)
                    {
                        if (s[j + i] != sub[i]) break;
                    }
                    if (i == subn) return j;
                }
                return -1;
            }
            int SubStringAfter(string s, string sub, int startIndex)
            {
                int subn = sub.Length;
                int sn = s.Length;
                for (int j = startIndex;j <=sn- subn; j++)
                {
                    int i = 0;
                    for (; i <subn; i++)
                    {
                        if (s[j + i] != sub[i]) break;
                    }
                    if (i == subn) return j;
                }
                return -1;
            }
            bool checkin(string src,int startIndex)
            {            foreach (string sub in MarkList)
                {
                    if (SubStringBefore(src, sub, startIndex) != -1)
                        return true;
                }
                return false;        }
            /// <summary>
            /// 查找语句块,"{"表示开始,"}"表示结束
            /// </summary>
            /// <param name="inMark">要查找的语句块关键字</param>
            /// <param name="sourceFile">被搜索的字符</param>
            /// <param name="startorend">true为开始,false为结束</param>
            /// <returns>返回开始或结束位置</returns>
            int TradSearch(string inMark, string sourceFile, bool startorend)
            {
                if (str_sourceFile != "")
                {
                    int im = sourceFile.IndexOf(inMark);
                    if (im > -1)
                    {
                        int i = sourceFile.IndexOf("{", im);
                        if (startorend)
                        {
                            return i;
                        }
                        else
                        {
                            int seek = 1;
                            int endTrad = -1;
                            for (int h = i + 1; h < sourceFile.Length; h++)
                            {
                                if (sourceFile[h] == '{')
                                {
                                    seek++;
                                }
                                else if (sourceFile[h] == '}')
                                {
                                    seek--;
                                }
                                if (seek == 0)
                                {
                                    endTrad = h;
                                    break;
                                }                        }
                            return endTrad;
                        }
                    }
                }
                return -1;
            }
            int IsInUnExu(string src,int index)
            {
                int st = TradSearch("struct", src, true);    
                int et = TradSearch("struct", src, false);
                if (index > st && index < et)
                {
                    
                    return et;
                }
                else
                    return -1;
            }
            string InserCount(ref string src, int startIndex, bool doCountOrResult,string[] countArr)
            {
                int i = 0;
                int index = 0;
               
            /*  int ispostil = SubStringAfter(src,"/*",0);
                int iepostil=-1;
                if (ispostil != -1)
                {
                    iepostil=SubStringAfter(src, "*", ispostil);
                }*/
                while (true&&startIndex<src.Length)
                {
                    index = src.IndexOf(';', startIndex+1);
                    if (index == -1) break;               /* if (ispostil>startIndex&&startIndex< iepostil)//没有考虑有多段注释的情况。解决方案:在循环外用list<point()>记录多个段注释的起始位置,struct同样
                    {
                        startIndex = iepostil;
                        continue;
                    }*/
                    int iexu=IsInUnExu(src, index);  //效率低!
                    if (-1!=iexu)
                    {
                        startIndex = iexu;
                        continue;
                    }
                  /*  if (istruct!=-1&&startIndex>istruct)
                    {
                        startIndex = TradSearch("struct", str_sourceFile, false); //没有考虑多个struct结构
                        continue;
                    }*/
                    if (checkin(src,index))
                    {
                        startIndex = SubStringAfter(src,"\n",index);
                        continue;
                    }
               
                    if (doCountOrResult)
                       {
                        
                           doCount = "   Counts[" + i.ToString() + "]++;";
                       }
                       else
                       {
                           if(i<countArr.Length)
                           doCount = "   //<<"+countArr[i]+">>";
                       }
                       src = src.Insert(index + 1, doCount);
                       startIndex = index + doCount.Length + 1;
                       i++;
                }
                return src;
            }
           void doCompiler(string filename)
            {
                  //代码太长
            }
            
     
        }
    }
      

  2.   

     void doCompiler(string filename)
            {
                string input = "csc /out:Monitor.exe " +  filename ;
                Process doCsc = new Process();
                doCsc.StartInfo.FileName="cmd.exe";
                doCsc.StartInfo.UseShellExecute = false;
                doCsc.StartInfo.RedirectStandardInput = true;
                doCsc.StartInfo.RedirectStandardOutput = true;
                doCsc.StartInfo.RedirectStandardError = true;
                doCsc.StartInfo.WorkingDirectory=@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";
                doCsc.StartInfo.CreateNoWindow = true;
                doCsc.Start();
                doCsc.StandardInput.WriteLine(input);
                doCsc.StandardInput.WriteLine("Monitor.exe");
                doCsc.StandardInput.WriteLine("exit");
                doCsc.StandardOutput.ReadToEnd();        //
                doCsc.WaitForExit();                    //这段代码浪费我了一个晚上的时间啊,啊 啊啊啊  啊啊
              //  MessageBox.Show(doCsc.ExitCode.ToString());            
            }