第一次作业 源代码计算器 目标2

using System;
using System.IO;

namespace CountLines
{
    //  统计单个文件信息  
    public class CountFileInfo
    {
        private bool m_ignoreCommentary;               //  是否忽略注释  
        private StreamReader m_reader;                  //  文件读流  
        private int m_charNum = 0;                          //  字符数  
        private int m_lineNum = 0;                           //   行数  
        private string m_fileName;                           //   文件名  

        //  私有方法  
        private void m_countALine(string aLine)
        {
            //  如果忽略注释并且在aLine中出现了"//"  
            if (m_ignoreCommentary && aLine.Contains(@"//"))
            {
                for (int i = 0; i != aLine.Length; ++i)
                {
                    if (aLine[i] == '/' && i < aLine.Length - 1 && aLine[i + 1] == '/')
                        return;
                    else
                        ++m_charNum;
                }
            }
            else
            {
                foreach (char c in aLine)
                {
                    //  跳过空格  
                    if (c != ' ')
                    {
                        ++m_charNum;
                    }
                }
            }
        }

        //  公有方法  
        public CountFileInfo(bool ignoreCommentary = true)
        {
            m_ignoreCommentary = ignoreCommentary;
        }

        public void Initialize(StreamReader reader, string fileName)
        {
            m_reader = reader;
            m_charNum = 0;
            m_lineNum = 0;
            m_fileName = fileName;
        }

        public void Count()
        {
            string buff = m_reader.ReadLine();
            while (buff != null)
            {
                //  忽略空行  
                if (buff.Length != 0)
                {
                    ++m_lineNum;
                    m_countALine(buff);
                }
                buff = m_reader.ReadLine();
            }
        }

        public int CharNum
        {
            get
            {
                return m_charNum;
            }
        }

        public int LineNum
        {
            get
            {
                return m_lineNum;
            }
        }

        public string FileName
        {
            get
            {
                return m_fileName;
            }
        }
    }

    //  统计所有文件以及目录信息  
    public class CountGlobalInfo
    {
        private uint m_fileNum;                  //  文件总数  
        private uint m_charNum;                //  总字符数  
        private uint m_lineNum;                 //   总行数  

        public CountGlobalInfo()
        {
            m_fileNum = 0;
            m_charNum = 0;
            m_lineNum = 0;
        }

        public void AddAFile(CountFileInfo countFileInfo)
        {
            ++m_fileNum;
            m_charNum += (uint)countFileInfo.CharNum;
            m_lineNum += (uint)countFileInfo.LineNum;
        }

        public uint FileNum
        {
            get
            {
                return m_fileNum;
            }
        }

        public uint CharNum
        {
            get
            {
                return m_charNum;
            }
        }

        public uint LineNum
        {
            get
            {
                return m_lineNum;
            }
        }
    }

    public class WriteResult
    {
        private string m_indentBuff;
        private StreamWriter m_writer;

        public WriteResult(string targetDirectory)
        {
            m_writer = new StreamWriter("统计结果.txt", false);
        }

        private void m_processIndent(int indentLevel)
        {
            m_indentBuff = null;
            for (int i = 0; i != indentLevel; ++i)
            {
                m_indentBuff += "    ";
            }
        }

        public void go(CountFileInfo countFileInfo, int indentLevel)
        {
            m_processIndent(indentLevel);
            m_writer.WriteLine(m_indentBuff + "文件: " + countFileInfo.FileName);
            m_writer.WriteLine(m_indentBuff + "行: " + countFileInfo.LineNum.ToString());
            m_writer.WriteLine(m_indentBuff + "字符:" + countFileInfo.CharNum.ToString());
            m_writer.WriteLine("");
        }

        public void go(CountGlobalInfo countGolbalInfo)
        {
            m_writer.WriteLine("文件总数: " + countGolbalInfo.FileNum.ToString());
            m_writer.WriteLine("全部行数: " + countGolbalInfo.LineNum.ToString());
            m_writer.WriteLine("全部字符数:" + countGolbalInfo.CharNum.ToString());
        }

        public void Save()
        {
            m_writer.Close();
        }
    }

    //  遍历以根目录为根的目录树  
    public class Travel
    {
        private DirectoryInfo m_rootDirectory;    //  根目录  
        private string m_Extension;                  //  文件名后缀,即文件类型  
        CountFileInfo m_countFileInfo;             //  统计文件类的实例  
        CountGlobalInfo m_countGlobalInfo;    //    统计全局信息类的实例  
        WriteResult m_writeResult;                  //  写统计结构类的实例  
        int m_indentLevel = 0;                        //   缩进数  

        //  公有方法  
        public Travel(string rootDirectory, string extension = ".cs", bool ignoreCommentary = true)
        {
            m_rootDirectory = new DirectoryInfo(rootDirectory);
            if (m_rootDirectory == null)
            {
                throw new System.Exception("无效的当前目录名!");
            }
            m_countFileInfo = new CountFileInfo(ignoreCommentary);
            m_countGlobalInfo = new CountGlobalInfo();
            m_writeResult = new WriteResult(rootDirectory);
            m_Extension = extension;
        }

        public void go(DirectoryInfo currentDirectory)
        {
            //  统计当前目录下的所有文件  
            foreach (FileInfo file in currentDirectory.GetFiles())
            {
                if (file.Extension == m_Extension)
                {
                    //  统计指定的文件类型  
                    StreamReader reader = file.OpenText();
                    m_countFileInfo.Initialize(reader, file.Name);
                    m_countFileInfo.Count();
                    m_countGlobalInfo.AddAFile(m_countFileInfo);
                    m_writeResult.go(m_countFileInfo, m_indentLevel);
                    reader.Close();
                }
            }
            ++m_indentLevel;
            //  获取当前目录下的所有子目录  
            DirectoryInfo[] subDirectories = currentDirectory.GetDirectories();
            foreach (DirectoryInfo directory in subDirectories)
            {
                go(directory);
            }
            --m_indentLevel;
            if (m_indentLevel == 0)
            {
                //  打印最终全局统计结果.  
                m_writeResult.go(m_countGlobalInfo);
                m_writeResult.Save();
            }
        }

        public DirectoryInfo RootDirectoryInfo
        {
            get
            {
                return m_rootDirectory;
            }
        }
    }

    public class program
    {
        public static void Main()
        {
            string directory;
            Console.WriteLine(
                "请输入你要统计的目录:\n");
            directory = Console.ReadLine();
            Console.WriteLine(
                "\n忽略所有注释,并将在本程序所在文件夹生成一个存放统计结果的文件\n" +
                "\"统计结果.txt\".\n\n单击回车键开始统计.");
            Console.ReadKey();
            Console.WriteLine("\n开始统计,请耐心等候...");
            Travel travel = new Travel(@directory);
            travel.go(travel.RootDirectoryInfo);
            Console.WriteLine("\n你所输入的目录为" + directory+"\n");
            Console.WriteLine("统计完毕,已将统计结果写入\"统计结果.txt\"文件中.");
            Console.WriteLine("单击回车退出程序.");
            Console.ReadKey();
        }
    }
}

wKioL1UihYaCz1DPAAFzyUxlLFI088.jpg

wKioL1UihYzwJnuxAAGIu6GJAyc387.jpg


你可能感兴趣的:(统计,private,public,信息)