第二次作业重交

1、Gitee链接:https://gitee.com/meimeimeishijian/codes/n8op3y7hslmu5aijxzref71

2、解题思路:刚看到这个作业的时候,对于程序设计感觉相对困难,不知道应该从哪里下手。在纠结了很久之后,通过在网上大量的查阅资料和请教同学之后,大致概况任务,可以分为:统计程序设计源文件的字符数、单词数和总行数,以及将结果输出到指定文件中。

3、程序设计:在本次程序设计过程中,我主要分为了三个类,即基本功能的实现类,统计字符数、单词数和行数的类以及打印类。

4、代码说明:

基本功能的实现

 public string Operator(string[] sParameter, string sFilename)
        {
            this.sParameter = sParameter;
            this.sFilename = sFilename;

            string retrun_str = "";

            foreach (string s in sParameter)
            {
                if (s == "-x")
                {
                    string resultFile = "";
                    OpenFileDialog fd = new OpenFileDialog();
                    fd.InitialDirectory = "D:\\Patch";
                    fd.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";
                    fd.FilterIndex = 2;
                    fd.RestoreDirectory = true;
                    if (fd.ShowDialog() == DialogResult.OK)
                    {
                        resultFile = fd.FileName;
                        //Console.WriteLine("文件名:{0}", resultFile);
                        SuperCount(resultFile);
                        BaseCount(resultFile);
                        retrun_str = DisplayAll();
                    }
                    break;
                }
                // 遍历文件
                else if (s == "-s")
                {
                    try
                    {
                        string[] arrPaths = sFilename.Split('\\');
                        int pathsLength = arrPaths.Length;
                        string path = "";

                        // 获取输入路径
                        for (int i = 0; i < pathsLength - 1; i++)
                        {
                            arrPaths[i] = arrPaths[i] + '\\';

                            path += arrPaths[i];
                        }

                        // 获取通配符
                        string filename = arrPaths[pathsLength - 1];

                        //  获取符合条件的文件名
                        string[] files = Directory.GetFiles(path, filename);

                        foreach (string file in files)
                        {
                            //Console.WriteLine("文件名:{0}", file);
                            SuperCount(file);
                            BaseCount(file);
                            retrun_str = Display();
                        }
                        break;
                    }
                    catch (IOException ex)
                    {
                        //Console.WriteLine(ex.Message);
                        return "";
                    }
                }
                // 高级选项
                else if (s == "-a")
                {
                    //Console.WriteLine("文件名:{0}", sFilename);
                    SuperCount(sFilename);
                    BaseCount(sFilename);
                    retrun_str = Display();
                    break;
                }
                //  基本功能
                else if (s == "-c" || s == "-w" || s == "-l")
                {
                    //Console.WriteLine("文件名:{0}", sFilename);
                    BaseCount(sFilename);
                    retrun_str = Display();
                    break;
                }
                else
                {
                    //Console.WriteLine("参数 {0} 不存在", s);
                    break;
                }
            }
            Console.WriteLine("{0}", retrun_str);
            return retrun_str;
        }

统计字符数、单词数及行数

  // 统计基本信息:字符数 单词数 行数
        private void BaseCount(string filename)
        {
            try
            {
                // 打开文件
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                StreamReader sr = new StreamReader(file);
                int nChar;
                int charcount = 0;
                int wordcount = 0;
                int linecount = 0;
                //定义一个字符数组
                char[] symbol = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"', '\t', '{', '}', '(', ')', '+' ,'-',
                  '*', '='};
                while ((nChar = sr.Read()) != -1)
                {
                    charcount++;     // 统计字符数

                    foreach (char c in symbol)
                    {
                        if (nChar == (int)c)
                        {
                            wordcount++; // 统计单词数
                        }
                    }
                    if (nChar == '\n')
                    {
                        linecount++; // 统计行数
                    }
                }
                iCharcount = charcount;
                iWordcount = wordcount + 1;
                iLinecount = linecount + 1;
                sr.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }

打印信息

 private string Display()
        {
            string return_str = "";

            foreach (string s in sParameter)
            {
                if (s == "-c")
                {
                    //Console.WriteLine("字 符 数:{0}", iCharcount);
                    return_str += "字符数:" + iCharcount.ToString();
                }
                else if (s == "-w")
                {
                    //Console.WriteLine("单 词 数:{0}", iWordcount);
                    return_str += "单词数:" + iWordcount.ToString();
                }
                else if (s == "-l")
                {
                    //Console.WriteLine("总 行 数:{0}", iLinecount);
                    return_str += "总行数:" + iLinecount.ToString();
                }
               
            }
            //Console.WriteLine();
            return return_str;
        }
        private string DisplayAll()
        {
            string return_str = "";
            foreach (string s in sParameter)
            {
                //Console.WriteLine("字 符 数:{0}", iCharcount);
                //Console.WriteLine("单 词 数:{0}", iWordcount);
                //Console.WriteLine("总 行 数:{0}", iLinecount);
               
                return_str += "字符数:" + iCharcount.ToString();
                return_str += "单词数:" + iWordcount.ToString();
                return_str += "总行数:" + iLinecount.ToString();
              
            }
            //Console.WriteLine();
            return return_str;
        }
    }

5、测试设计

6、总结

经过此次作业,我收获了很多,对于软件测试代码的编写进行了更加详细充足的认识和学习,也遇到了很多困难,虽然最后大致完成了任务,但是我还是有很多不足和不懂之处需要我继续学习,不断进步。

 

posted on 2018-09-29 22:44  没没没没时间  阅读(130)  评论(0编辑  收藏  举报

导航