日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言每日練習之統計文本單詞數及高頻詞_C 語言

作者:德宏大魔王 ? 更新時間: 2022-07-14 編程語言

作業1:統計出txt文本里面的單詞數,并找出頻率出現最高的單詞是哪個?

運行結果:

上代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //文件打開
            //string file = System.IO.File.ReadAllLines(@"");
            int count = 0;
            string tmp = "";
            //初始化次數
            string words = "hihi hello,hihi,hello,hihi?";
            var new_i = words.Split(new char[] { ' ', ',', '.', '?' },StringSplitOptions.RemoveEmptyEntries);
            Console.Write("總的單詞數量:{0}\n", new_i.Length);
            for (int i = 0; i < new_i.Length; i++)
            {
                //查詢每個單詞出現的次數
                var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key;
                int key_count = query.Count();
                if (key_count > count) {

                    count = key_count;
                    tmp = new_i[i];
                }
               
            }

            Console.Write("頻率出現最高的單詞是:{0}\n", tmp);
            Console.Write("次數為:{0}\n", count);
            
            Console.ReadKey();
            
        }
    }
}

基礎代碼運行成功!通過外部打開!

創建11.txt

通過外部打開:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //文件打開
            //string[] file_A = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\11.txt");
            string file_A = System.IO.File.ReadAllText(@"C:\Users\Administrator\Desktop\11.txt");
            //Console.Write(file_A);
            int count = 0;
            string tmp = "";
            //初始化次數
            
            var new_i = file_A.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries);
            Console.Write("總的單詞數量:{0}\n", new_i.Length);
            for (int i = 0; i < new_i.Length; i++)
            {
                //查詢每個單詞出現的次數
                var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key;
                int key_count = query.Count();
                if (key_count > count) {

                    count = key_count;
                    tmp = new_i[i];
                }
               
            }

            Console.Write("頻率出現最高的單詞是:{0}\n", tmp);
            Console.Write("次數為:{0}\n", count);
            
            Console.ReadKey();
            
        }
    }
}

運行截圖:

原文鏈接:https://blog.csdn.net/qq_35230125/article/details/124828900

欄目分類
最近更新