첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 단어 배열 구하기 - GetWordArray(url)

        /// <summary>
        /// 단어 배열 구하기
        /// </summary>
        /// <param name="url">URL 입니다.</param>
        /// <returns>단어 배열</returns>
        private static string[] GetWordArray(string url)
        {
            Console.WriteLine($"다음 사이트에서 가져옵니다 : {url}");

            string content = new WebClient().DownloadString(url);

            return content.Split
            (
                new char[] { ' ', '\u000A', ',', '.', ';', ':', '-', '_', '/' },
                StringSplitOptions.RemoveEmptyEntries
            );
        }

        #endregion
        #region 가장 긴 단어 출력하기 - PrintLongestWord(wordArray)

        /// <summary>
        /// 가장 긴 단어 출력하기
        /// </summary>
        /// <param name="wordArray">단어 배열</param>
        private static void PrintLongestWord(string[] wordArray)
        {
            string longestWord =
            (
                from    word in wordArray
                orderby word.Length descending
                select  word
            ).First();

            Console.WriteLine($"첫번째 작업, 가장 긴 단어 : {longestWord}");
        }

        #endregion
        #region 가장 일반적인 단어 출력하기 - PrintMostCommonWord(wordArray)

        /// <summary>
        /// 가장 일반적인 단어 출력하기
        /// </summary>
        /// <param name="wordArray">단어 배열</param>
        private static void PrintMostCommonWord(string[] wordArray)
        {
            IEnumerable<string> enumerable1 = from  word in wordArray
                                              where word.Length > 6
                                              group word by word into wordGroup
                                              orderby wordGroup.Count() descending
                                              select wordGroup.Key;

            IEnumerable<string> enumerable2 = enumerable1.Take(10);

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("두번째 작업, 가장 일반적인 단어 : ");

            foreach(string word in enumerable2)
            {
                stringBuilder.AppendLine("    " + word);
            }

            Console.WriteLine(stringBuilder.ToString());
        }

        #endregion
        #region 단어 수 출력하기 - PrintWordCount(wordArray, term)

        /// <summary>
        /// 단어 수 출력하기
        /// </summary>
        /// <param name="wordArray">단어 배열</param>
        /// <param name="term">용어</param>
        private static void PrintWordCount(string[] wordArray, string term)
        {
            IEnumerable<string> wordEnumerable = from   word in wordArray
                                                 where  word.ToUpper().Contains(term.ToUpper())
                                                 select word;

            Console.WriteLine($@"세번째 작업, ""{term}"" 단어 수 : {wordEnumerable.Count()}");
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            string[] wordArray = GetWordArray(@"http://www.gutenberg.org/files/54700/54700-0.txt");

            Parallel.Invoke
            (
                () =>
                {
                    Console.WriteLine("첫번째 작업을 시작합니다...");

                    PrintLongestWord(wordArray);
                },
                () =>
                {
                    Console.WriteLine("두번째 작업을 시작합니다...");

                    PrintMostCommonWord(wordArray);
                },
                () =>
                {
                    Console.WriteLine("세번째 작업을 시작합니다...");

                    PrintWordCount(wordArray, "sleep");
                }
            );

            Console.WriteLine("Parallel.Invoke 실행을 종료했습니다.");

            Console.WriteLine("종료하기 위해서 아무 키나 눌러주시기 바랍니다.");

            Console.ReadKey(false);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요