(1)Gitee项目地址:我没有注册上码云
(2)描述解题思路:
- 为每个模块设计测试用例(综合应用等价划分、边界值、逻辑覆盖等方法)
- 编写测试代码(尽量使用单元测试工具)
- 执行单元测试确保所有单元均通过测试
(3)程序设计实现过程:我设计了统计单词数、字符数和行数三个方法,统一在类wc里。
(4)代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace WordCount
{
class wc
{
static void Main(string[] args)
{
string fileName = args[1];
int charnumber = 0, linenumber = 0, wordnumber = 0;
charnumber = CharCount(fileName);
linenumber = LineCount(fileName);
wordnumber = WordCount(fileName);
switch (args[0])
{
case "-c":
Console.WriteLine(fileName + ",字符数:" + charnumber);
break;
case "-w":
Console.WriteLine(fileName + ",单词数:" + wordnumber);
break;
case "-l":
Console.WriteLine(fileName + ",行数:" + linenumber);
break;
default:
Console.WriteLine("命令错误");
break;
}
Console.ReadKey();
}
//统计单词数
public static int WordCount(string fileName)
{
int wNum = 1;
char[] divide = { ' ', ',' };
int wchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((wchar = sr.Read()) != -1)
{
foreach (char c in divide)
{
if (wchar == (int)c)
{
wNum++;
}
}
}
return wNum;
}
//统计字符数
public static int CharCount(string fileName)
{
int cNum = 0;
string nchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((nchar = sr.ReadLine()) != null)
{
cNum += nchar.Replace(" ", "").Length;
}
sr.Close();
return cNum;
}
//统计行数
public static int LineCount(string fileName)
{
int lNum = 0;
string nchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((nchar = sr.ReadLine()) != null)
{
lNum++;
}
sr.Close();
return lNum;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace WordCount
{
class wc
{
static void Main(string[] args)
{
string fileName = args[1];
int charnumber = 0, linenumber = 0, wordnumber = 0;
charnumber = CharCount(fileName);
linenumber = LineCount(fileName);
wordnumber = WordCount(fileName);
switch (args[0])
{
case "-c":
Console.WriteLine(fileName + ",字符数:" + charnumber);
break;
case "-w":
Console.WriteLine(fileName + ",单词数:" + wordnumber);
break;
case "-l":
Console.WriteLine(fileName + ",行数:" + linenumber);
break;
default:
Console.WriteLine("命令错误");
break;
}
Console.ReadKey();
}
//统计单词数
public static int WordCount(string fileName)
{
int wNum = 1;
char[] divide = { ' ', ',' };
int wchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((wchar = sr.Read()) != -1)
{
foreach (char c in divide)
{
if (wchar == (int)c)
{
wNum++;
}
}
}
return wNum;
}
//统计字符数
public static int CharCount(string fileName)
{
int cNum = 0;
string nchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((nchar = sr.ReadLine()) != null)
{
cNum += nchar.Replace(" ", "").Length;
}
sr.Close();
return cNum;
}
//统计行数
public static int LineCount(string fileName)
{
int lNum = 0;
string nchar;
FileStream file = new FileStream(fileName, FileMode.Open);
StreamReader sr = new StreamReader(file);
while ((nchar = sr.ReadLine()) != null)
{
lNum++;
}
sr.Close();
return lNum;
}
}
}
(5)运行结果: