国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#統(tǒng)計(jì)英文文本中的單詞數(shù)并排序

2019-11-17 03:16:01
字體:
供稿:網(wǎng)友

C#統(tǒng)計(jì)英文文本中的單詞數(shù)并排序

思路如下:1.使用的Hashtable(高效)集合,記錄每個單詞出現(xiàn)的次數(shù)2.采用ArrayList對Hashtable中的Keys按字母序排列3.排序使用插入排序(穩(wěn)定)
public void StatisticsWords(string path)        {            if (!File.Exists(path))            {                Console.WriteLine("文件不存在!");                return;            }            Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase);            StreamReader sr = new StreamReader(path, System.Text.Encoding.UTF8);            string line = sr.ReadLine();            string[] wordArr = null;            int num = 0;            while (line.Length > 0)            {                //   MatchCollection mc =  Regex.Matches(line, @"/b[a-z]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);                //foreach (Match m in mc)                //{                //    if (ht.ContainsKey(m.Value))                //    {                //        num = Convert.ToInt32(ht[m.Value]) + 1;                //        ht[m.Value] = num;                //    }                //    else                //    {                //        ht.Add(m.Value, 1);                //    }                //}                //line = sr.ReadLine();                wordArr = line.Split(' ');                foreach (string s in wordArr)                {                    if (s.Length == 0)                        continue;                    //去除標(biāo)點(diǎn)                    line = Regex.Replace(line, @"[/p{P}*]", "", RegexOptions.Compiled);                    //將單詞加入哈希表                    if (ht.ContainsKey(s))                    {                        num = Convert.ToInt32(ht[s]) + 1;                        ht[s] = num;                    }                    else                    {                        ht.Add(s, 1);                    }                }                line = sr.ReadLine();            }            ArrayList keysList = new ArrayList(ht.Keys);            //對Hashtable中的Keys按字母序排列            keysList.Sort();            //按次數(shù)進(jìn)行插入排序【穩(wěn)定排序】,所以相同次數(shù)的單詞依舊是字母序            string tmp = String.Empty;            int valueTmp = 0;            for (int i = 1; i < keysList.Count; i++)            {                tmp = keysList[i].ToString();                valueTmp = (int)ht[keysList[i]];//次數(shù)                int j = i;                while (j > 0 && valueTmp > (int)ht[keysList[j - 1]])                {                    keysList[j] = keysList[j - 1];                    j--;                }                keysList[j] = tmp;//j=0            }            //打印出來            foreach (object item in keysList)            {                Console.WriteLine((string)item + ":" + (string)ht[item]);            }        }

  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 林芝县| 正阳县| 米易县| 古浪县| 农安县| 会理县| 密云县| 沈丘县| 桃江县| 资兴市| 信丰县| 徐州市| 大宁县| 兰坪| 板桥市| 合阳县| 武川县| 小金县| 读书| 全椒县| 永川市| 会泽县| 武川县| 昆山市| 广德县| 都安| 东至县| 洞头县| 锡林浩特市| 定结县| 乡城县| 乌鲁木齐县| 瑞安市| 宣化县| 乡城县| 辽源市| 松滋市| 石家庄市| 桑植县| 贺兰县| 德安县|