第二周作业汇总

功能一:

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace wordcount
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.Write(">type ");
            string textFileName;
            textFileName = Console.ReadLine();
            //读取文件内容
            StreamReader sreader = new StreamReader(@"C:\Users\dell-pc\desktop\test1.txt");
            //输出文件内容
            string aword = sreader.ReadLine();
            Console.WriteLine(aword);
            Console.WriteLine();
            char c = ' ';
            //除去' '后的字符串数组
            string[] s = aword.Split(c);
            //建立哈希表
            Hashtable ha = new Hashtable();
            for (int i = 0; i < s.Length; i++)
            {
                //判断哈希表中是否有文本
                if (ha.ContainsKey(s[i]))
                {
                    ha[s[i]] = (int)ha[s[i]] + 1;
                }
                else
                {
                    ha.Add(s[i], 1);
                }
            }
            string[] arrKey = new string[ha.Count];//存键
            int[] arrValue = new int[ha.Count];//存值
            ha.Keys.CopyTo(arrKey, 0);
            ha.Values.CopyTo(arrValue, 0);
            Console.WriteLine(">wf -s test.txt");
            Console.WriteLine("total " + ha.Count);
            Console.WriteLine();
            Array.Sort(arrValue, arrKey);//排序
            for (int i = arrKey.Length - 1; i >= 0; i--)
            {
                if ((string)arrKey[i] != "")
                {
                     Console.Write(arrKey[i].ToString().PadRight (12)+" ");
                     Console.WriteLine(arrValue[i].ToString());
                }
            }
        }
    }
}

功能一执行:

功能二:

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace wf
{
    class Program
    {

        static void Main(string[] args)
        {

            string textFileName;
            Console.Write(">type ");
            textFileName = Console.ReadLine();
            StreamReader sreader = new StreamReader(@"C:\Users\dell-pc\desktop\test3.txt");
            string afile = sreader.ReadToEnd();
            afile = afile.ToLower();//转换为小写字母
            //将空格跟标点符号定义出来
            char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' };
            //分隔char[c]后产生的数组
            string[] S = afile.Split(c);
            //建立哈希表
            Hashtable ha = new Hashtable();
            for (int i = 0; i < S.Length; i++)
            {
                if (ha.ContainsKey(S[i]))
                {
                    ha[S[i]] = (int)ha[S[i]] + 1;
                }
                else
                {
                    ha.Add(S[i], 1);
                }
            }
            string[] arrKey = new string[ha.Count];//存键
            int[] arrValue = new int[ha.Count];//存值
            ha.Keys.CopyTo(arrKey, 0);
            ha.Values.CopyTo(arrValue, 0);
            Console.WriteLine();
            Console.WriteLine(">wf -s test.txt");
            Console.WriteLine("total " + ha.Count);
            Console.WriteLine();
            Array.Sort(arrValue, arrKey);//排序
            int n = 0;
            for (int i = arrKey.Length - 1; i >= 0; i--)
            {
                if ((string)arrKey[i] != "")
                {
                    if (n<10)
                    {
                        //输出前10位多的单词,右对齐
                        Console.Write(arrKey[i].ToString().PadRight (12));
                        Console.WriteLine(arrValue[i].ToString());
                        n=n+1;
                    }
                }
            }
        }
    }
}

功能二执行:

本周PSP:

链接: https://git.coding.net/yuanyue2017102885/wordcount_1and2.git

 

posted on 2017-09-18 22:48  袁玥  阅读(178)  评论(0编辑  收藏  举报

导航