软件测试第三次作业

201631062113
代码地址:https://gitee.com/ttxxt/wc
本次作业的链接地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

一、互审代码情况

   我们参考C# 语言的规范,邹欣老师的讲义“现代软件工程讲义 3 代码规范与代码复审”等初步制定了代码规范。在互审代码的过程中发现的问题也主要是在代码规范方面,比如命名不严谨、空格换行不规范,尤其是很多地方没有相应的注释或者不恰当等。

功能代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace wc
{
public class File
{
private string iFile;//输入文件
private List parameter;//输入指令
private int charcount;//字符数
private int wordcount;//单词数
private int linecount;//行数
private int commentLine;//注释行
private int blankLine;//空行
private int codeLine;//代码行
public void Check(List parameter, string iFile)//检查用户输入是否正确
{
this.parameter = parameter;
this.iFile = iFile;
foreach (string a in parameter)
{
if (a != "-c" && a != "-w" && a != "-l" && a != "-o" && a != "-s" && a != "-a" && a != "-e")
{
Console.WriteLine("操作{0}不存在!", a);
}
}
}
public void Statistic(string iFile)//统计数据
{
//检查文件格式是否正确
string extension = Path.GetExtension(iFile).ToLower();
if (extension != ".c")
{
Console.WriteLine("输入文件格式错误");
return;
}
int nchar;
charcount = 0;
wordcount = 0;
linecount = 0;
blankLine = 0;
blankLine = 0;
codeLine = 0;
char[] symbol = { ' ', ',' };

        FileStream File = new FileStream(iFile, FileMode.Open, FileAccess.Read);
        StreamReader file = new StreamReader(File);
        try
        {
            //读取文件
            FileStream fs = new FileStream(iFile, FileMode.Open);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string line = sr.ReadLine();
            while (line != null)
            {
                //注释行
                if (line.Substring(0, 2) == "//" || line.Substring(0, 2) == "/*" || line.Substring(0, 2) == "*/" || line.Substring(0, 1) == "*")
                {
                    commentLine++;
                }
                //空行
                else if (line == "" || line.Substring(0, 1) == "{" || line.Substring(0, 1) == "}")
                {
                    blankLine++;
                }
                //代码行
                else
                {
                    codeLine++;
                }
            }
            
        }
        catch
        {
            Console.WriteLine("打开文件失败");
           

        }
        while ((nchar = file.Read()) != -1)
        {
            charcount++;//记录字符数
            foreach (char c in symbol)
            {
                if (nchar == (int)c)
                {
                    wordcount++;//记录单词数
                }
                if (nchar == '\n')
                {
                    linecount++;//记录行数
                }
                
            }

        }

    }
    
    public void Output(string iFile)//输出统计结果
    {
        bool C = false;
        bool W = false;
        bool L = false;
        bool A = false;
        foreach (string f in parameter)//检查指令
        {
            if (f == "-c")
            {
                C = true;
            }
            if (f == "-w")
            {
                W = true;
            }
            if (f == "-l")
            {
                L = true;
            }
            if (f == "-a")
            {
                A = true;
            }
        }
        //输出数据
        if (C)
        {
            Console.WriteLine("{0},字符数:{1}", iFile, charcount);
        }
        if (W)
        {
            Console.WriteLine("{0},单词数:{1}", iFile, wordcount);
        }
        if (L)
        {
            Console.WriteLine("{0},行数:{1}", iFile, linecount);
        }
        if (A)
        {
            Console.WriteLine("{0},行数:{1}", iFile, linecount);
        }
    }
    public void Writefile(List parameter)//写入文件
    {
        //连接文件
        FileStream fs = new FileStream("result.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter oFile = new StreamWriter(fs);
        bool C = false;
        bool W = false;
        bool L = false;
        bool A = false;
        foreach (string f in parameter)//检查指令
        {
            if (f == "-o")
            {
                foreach (string c in parameter)
                {
                    if (c == "-c")
                    {
                        C = true;
                    }
                    if (c == "-w")
                    {
                        W = true;
                    }
                    if (c == "-l")
                    {
                        L = true;
                    }
                }
            }
        }
        //写入文件
        if (C)
        {
            oFile.WriteLine("{0},字符数:{1}", iFile, charcount);
        }
        if (W)
        {
            oFile.WriteLine("{0},单词数:{1}", iFile, wordcount);
        }
        if (L)
        {
            oFile.WriteLine("{0},行数:{1}", iFile, linecount);
        }
        if (A)
        {
            oFile.WriteLine("{0},注释行:{1},空行:{2},代码行:{3}", iFile, commentLine,blankLine,codeLine);
        }
        oFile.Flush();
        oFile.Close();
        fs.Close();
    }
    static void Main(string[] args)
    {
        File file = new File();
        string iFile = null;
        List parameter = new List();
        string[] inputs = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);//读取用户输入的指令
        for (int i = 0; i <= inputs.Length - 1; i++)
        {
            if (i == inputs.Length - 1)
            {
                iFile = string.Concat(inputs[i]);
            }
            else
            {
                parameter.Add(string.Concat(inputs[i]));
            }
        }
        file.Check(parameter, iFile);
        file.Statistic(iFile);
        
        file.Output(iFile);
        file.Writefile(parameter);
    }
}

}

你可能感兴趣的:(软件测试第三次作业)