软件测试第二次作业
gitee项目地址:https://gitee.com/zingwu/WordCount
解题思路
因为最近在学习java的相关知识,所以看到题目就决定用java来写。初步思考了之后发现要完成这个题目需要用到大量I/O相关的知识,然后准备每一个功能写一个类,然后在主类之中调用相关方法来实现这个程序。在谷歌上看了一些类似的代码,就开始动手了。
程序设计实现过程
我实现了以下功能:
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的单词总数
wc.exe -l file.c //返回文件 file.c 的总行数
wc.exe -a file.c //返回更复杂的数据(代码行 / 空行 / 注释行)
wc.exe -o outputFile.txt //将结果输出到指定文件output.txt
我给每一个实现了的功能建了一个类用来写具体实现的方法,然后在主类中调用就行了,想着还是挺简单的,具体实施下还是遇到了不少的问题:
1.最大的问题就是因为马虎,我没仔细看要求,没有用命令行参数来执行指令,就不能一次执行多个命令,还好在提交之前发现及时做了修改。
2.在我初期测试项目时发现,当我复制文件路径进入程序时,eclipse会报错显示我的路径不正确,因为路径前面多了一个问号,我百思不得其解,只能对输入的路径再次处理去掉前面的问号(我在程序注释里进行了说明),最后才发现原来是我复制粘贴时系统会自动在我路径前面加入一个符号,导致报错...
对字符,单词等的判定我想到的是用正则表达式来进行判断。
代码说明
具体代码请参考gitee,这里只列出函数相关的代码。
WC.java(主函数部分)
public static void main(String[] args) throws IOException {
if (args.length == 0) { // 当命令行为空时,显示指示说明
System.out.println("指令说明:");
System.out.println("wc.exe -c file.c //返回文件 file.c 的字符数");
System.out.println("wc.exe -w file.c //返回文件 file.c 的单词总数");
System.out.println("wc.exe -l file.c //返回文件 file.c 的总行数");
System.out.println("wc.exe -a file.c //返回更复杂的数据(代码行 / 空行 / 注释行)");
System.out.println("wc.exe -o outputFile.txt //将结果输出到指定文件outputFile.txt");
} else {
boolean hasOutput = false; // 定义布尔变量hasOutput判断是否有输出文件
String outputName = null; // 定义字符串outputName表示输出文件
String inputName = null; // 定义字符串inputName表示输入文件
for (int i = 0; i < args.length; i++) // 判断是否需要输出到output.txt
if (args[i].equals("-o")) {
hasOutput = true; // 需要输出
inputName = args[i - 1];
/*
* inputName = inputName.replace('?', ' '); inputName = inputName.trim();
* 传统输入路径会自动加入一个问号导致路径不正确,我解决不了,只能把问号去掉
* PS:已解决:是我复制文件路径时会自动加上一个line符号(我也不知道为什么),不过解决了
*/
outputName = args[i + 1];
break;
}
if(inputName==null) {
inputName =args[args.length-1]; //不需要输出到output.txt
}
//调用各功能对应的方法
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-c")) {
charCount.countChar(inputName);
if (hasOutput) {
outputResult.Result(outputName);
}
} else if (args[i].equals("-w")) {
wordCount.countWord(inputName);
if (hasOutput) {
outputResult.Result(outputName);
}
} else if (args[i].equals("-l")) {
lineCount.countLine(inputName);
if (hasOutput) {
outputResult.Result(outputName);
}
} else if (args[i].equals("-a")) {
complexCount.countComplex(inputName);
if (hasOutput) {
outputResult.Result(outputName);
}
}
}
}
}
}
wordCount.java(计算单词数)
static int countword = 0; // 定义变量countword用来记录单词数
public static void countWord(String path) throws IOException {
String REGEX = "\\b\\w+\\b"; // 判定为单词的正则表达式条件
String text; // 定义变量text用来存储文本内容
BufferedReader br = new BufferedReader(new FileReader(path));
Pattern p = Pattern.compile(REGEX);
while ((text = br.readLine()) != null) {
Matcher m = p.matcher(text);
while (m.find()) {
countword++;
}
}
System.out.println("单词数=" + countword);
br.close();
}
charCount.java(计算字符数)
static int countchar = 0; // 定义变量charcount用来记录字符数
public static void countChar(String path) throws IOException {
String REGEX = "."; // 判定为字符的正则表达式条件
String text; // 定义变量text用来存储文本内容
BufferedReader br = new BufferedReader(new FileReader(path));
Pattern p = Pattern.compile(REGEX);
while ((text = br.readLine()) != null) {
Matcher m = p.matcher(text);
while (m.find()) {
countchar++;
}
}
System.out.println("字符数=" + countchar);
br.close();
}
lineCount.java(计算行数)
static int countline = 0; // 定义变量countline用来记录行数
public static void countLine(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
while (br.readLine() != null) {
countline++;
}
System.out.println("行数=" + countline);
br.close();
}
complexCount.java(计算代码行,空行,注释行)
static int blankcount = 0;// 定义blankcount用来记录空行
static int commentcount = 0;// 定义commentcount用来记录注释行
static int codecount = 0;// 定义codecount用来记录代码行
public static void countComplex(String path) throws IOException {
int count = 0; // 定义变量count用来记录总行数
boolean temp = false;
String tp = null;
BufferedReader br = new BufferedReader(new FileReader(path));
while ((tp = br.readLine()) != null) {
count++;
if (tp.length() == 0) {
blankcount++;// 计算空行数
}
tp = tp.trim();
if (tp.startsWith("/*") && !tp.endsWith("*/")) {
commentcount++;
temp = true;
} else if (temp) {
commentcount++;
if (tp.endsWith("*/")) {
temp = false;
}
} else if (tp.startsWith("//")) {
commentcount++;
} else if (tp.startsWith("/*") && tp.endsWith("*/")) {
commentcount++;// 计算注释行
}
codecount = count - commentcount - blankcount;// 计算代码行
}
System.out.println("空行=:" + blankcount);
System.out.println("代码行=" + codecount);
System.out.println("注释行=" + commentcount);
br.close();
}
outputResult.java(将结果输出到文件)
public static void Result(String path) throws IOException {
File file = new File(path);
if (!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
PrintStream stdout=System.out;
PrintStream printStream = new PrintStream(fileOutputStream);
System.setOut(printStream);
if (wordCount.countword != 0) {
System.out.println("单词数=" + wordCount.countword);
}
if (charCount.countchar != 0) {
System.out.println("字符数=" + charCount.countchar);
}
if (lineCount.countline != 0) {
System.out.println("行数=" + lineCount.countline);
}
if (complexCount.codecount != 0) {
System.out.println("代码行=" + complexCount.codecount);
System.out.println("注释行=" + complexCount.commentcount);
System.out.println("空行=" + complexCount.blankcount);
}
System.setOut(stdout);
}
测试设计过程
先进行基本功能测试:
1.wc.exe -c D:\test\helloworld.c
运行结果为:
2.wc.exe -w D:\test\helloworld.c
运行结果为:
3.wc.exe -l D:\test\helloworld.c
运行结果为:
4.wc.exe -a D:\test\helloworld.c
运行结果为:
5.wc.exe -c D:\test\helloworld.c -o output.txt
运行结果为:
生成的output.txt:
6.wc.exe -w D:\test\helloworld.c -o output.txt
运行结果为:
生成的output.txt:
7.wc.exe -l D:\test\helloworld.c -o output.txt
运行结果为:
生成的output.txt:
8.wc.exe -a D:\test\helloworld.c -o output.txt
运行结果为:
生成的output.txt:
......
组合指令我也测试过了,基本能满足用户需求。