1,一个长度为1000的字符串aibol_str, 由a-j共计10个字母随即组成。请用C#编程写主要函数
public string GenerateAibolStr(){}来实现。
2,请使用上述试题中的aibol_str字符串,完成如下函数。
///<summary>
///获取aibol_str中匹配的字符串个数
///</summary>
///<param name="s">需要匹配的字符串个数</param>
///<returns>aibol_str中包含的字符串s的数量</returns>
public int GetCount(string s){}
3,现有一个如下定义的XML文件<Dir name="root">
      <Dir Name="project">
   <File Name="test.sln"/>
<File Name="test.aspx.cs"/>
</Dir>
<File Name="makefile"/>
</Dir>
请使用递归算法,遍历所有的文件,并显示文件名及该文件对应的目录名称。

解决方案 »

  1.   

    1.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleCSharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(GenerateAibolStr());
            }
            public static string GenerateAibolStr()
            {
                Random rnd = new Random();
                StringBuilder sb=new StringBuilder();
                while (sb.Length < 1001)
                {
                    sb.Append((char)(rnd.Next(10) + 'a'));
                }
                return sb.ToString();
            }
        }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleCSharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                string ss = GenerateAibolStr();
                Console.WriteLine(ss);
                int count = GetCount("a", ss);
                Console.WriteLine(count);
            }        public static string GenerateAibolStr()
            {
                Random rnd = new Random();
                StringBuilder sb = new StringBuilder();
                while (sb.Length < 1001)
                {
                    sb.Append((char)(rnd.Next(10) + 'a'));
                }
                return sb.ToString();
            }
            /// <summary> 
            ///获取aibol_str中匹配的字符串个数 
            /// </summary> 
            /// <param name="s">需要匹配的字符串 </param> 
            /// <returns>aibol_str中包含的字符串s的数量 </returns> 
            public static int GetCount(string s, string str) //str为原串
            {
                int count = 0;
                while (str.IndexOf(s) != -1)
                {
                    count++;
                    str = str.Substring(str.IndexOf(s) + 1, str.Length - str.IndexOf(s) - 1);
                }
                return count;
            }    }
    }
      

  3.   

    2:string aibol_str;
            private void Form1_Load(object sender, EventArgs e)
            {
                aibol_str = GenerateAibolStr();
                int s = GetCount("abc");
                Console.WriteLine("有{0}个匹配数", s.ToString());
            }
            public string GenerateAibolStr()
            {
                Random rnd = new Random();
                StringBuilder sb = new StringBuilder();
                while (sb.Length < 1001)
                {
                    sb.Append((char)(rnd.Next(10) + 'a'));
                }
                return sb.ToString();
            }
            /// <summary> 
            ///获取aibol_str中匹配的字符串个数 
            /// </summary> 
            /// <param name="s">需要匹配的字符串个数 </param> 
            /// <returns>aibol_str中包含的字符串s的数量 </returns> 
            public int GetCount(string s)
            {
                int count = 0;
                int start = 0;
                while (aibol_str.IndexOf(s, start) != -1)
                {
                    count++;
                    start = aibol_str.IndexOf(s, start)+1;
                }
                return count;
            }