一、Gitee地址:
二、解题思路:
1、该程序要求在控制台输入参数后运行,所以编程时将输入的参数存入args[]中进行逐个判断。
2、作业要求对指定文本内的字符数、单词总数和行数进行统计,且可指定输出至文件,以及一些其它扩展内容。
3、将文本内容读取进String中,对该字符串进行检测则可得出结果。
4、统计单词个数时,与停用词表中的单词逐个比较,若相同则不计数。
5、注释行、空行、代码行的判断通过正则表达式判断。
三、程序的设计实现过程:
public static String getC(String fileName) // 计算指定文件中字符的个数
public static String getW(String fileName) // 计算指定文件中单词的个数
public static String getl(String fileName) //计算指定文件中的总的行数
public static String geta(String fileName) //返回具体的代码行数, 注释行数和空的行数
public static void writeFile(String fileName) // 将结果写入指定的文件中
四、代码说明:
//计算指定文件中字符的个数,
public staticString getC(String fileName) {int count = 0;
String s;try{
BufferedReader in= new BufferedReader(newFileReader(fileName));//FileReader in = new FileReader(fileName);
while ((s = in.readLine()) != null)
count+=s.length();
in.close();
}catch(FileNotFoundException e1) {
System.out.println("找不到文件!!!");
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}return fileName + ", 字符数为:" +count;
}
//计算指定文件中单词的个数
public staticString getW(String fileName) {int count = 0;
String s;try{
BufferedReader in= new BufferedReader(newFileReader(fileName));while ((s = in.readLine()) != null) {if (!s.isEmpty()) {
String ss[]= s.trim().split(" |,| ");
count+=ss.length;
}
}
in.close();
}catch(FileNotFoundException e1) {
System.out.println("找不到文件!!!");
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}return fileName + ", 单词数为:" +count;
}
//计算指定文件中的总的行数
public staticString getl(String fileName) {int count = 0;try{
BufferedReader in= new BufferedReader(newFileReader(fileName));while ((in.readLine()) != null)++count;
in.close();
}catch(FileNotFoundException e1) {
System.out.println("找不到文件!!!");
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}return fileName + ", 总的行数为:" +count;
}
//返回具体的代码行数, 注释行数和空的行数
public staticString geta(String fileName) {int count = 0, space = 0, h = 0;
String s;try{
BufferedReader in= new BufferedReader(newFileReader(fileName));while ((s = in.readLine()) != null) {if (s.isEmpty() || s.trim().length() <= 1)++space;else{int t = 0;while (t < s.length() && s.charAt(t) == ' ')++t;if ((t + 1 < s.length() && s.charAt(t) == '/' && s.charAt(t + 1) == '/')|| (t + 2 < s.length() && s.charAt(t + 1) == '/' && s.charAt(t + 2) == '/'))++h;else
++count;
}
}
in.close();
}catch(FileNotFoundException e1) {
System.out.println("找不到文件!!!");
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}return fileName + ", 代码行/空行/注释行:" + count + "/" + space + "/" +h;
}
//将结果写入指定的文件中
public static voidwriteFile(String fileName) {try{
FileWriter w= newFileWriter(fileName);int i = 0;for (; i < arrayList.size(); i++) {
w.write(arrayList.get(i));
w.write("\r\n");
}
w.close();
}catch(IOException e) {
System.out.println("文件写入错误");
e.printStackTrace();
}
}
//主函数
public static voidmain(String[] args) {if (args.length > 1) {int last = args.length - 1;
String name= args[last];
String outputName = "";
String tmp;
int flag = 0;//判断是否需要将信息输出到一个文件中
for (int i = 0; i < last; i++)if (args[i].equals("-o")) {
flag= 1;
name= args[last - 2];
outputName=args[last];break;
}//读取命令行中传入的参数
for (int i = 0; i < last; i++)if (args[i].equals("-c")) {
tmp= newString(getC(name));
System.out.println(tmp);
arrayList.add(tmp);
}else if (args[i].equals("-w")) {
tmp= newString(getW(name));
System.out.println(tmp);
arrayList.add(tmp);
}else if (args[i].equals("-l")) {
tmp= newString(getl(name));
System.out.println(tmp);
arrayList.add(tmp);
}else if (args[i].equals("-a")) {
tmp= newString(geta(name));
System.out.println(tmp);
arrayList.add(tmp);
}//将结果输出到一个文件中
if (flag == 1) {
writeFile(outputName);
System.out.println();
System.out.println("将信息写入" + outputName + "文件成功!!!");
}
}elseSystem.out.println("请先输入参数!!!");
}
五、测试用例设计:
测试字符个数:: wc.exe -c test.txt -o result.txt
测试单词个数: wc.exe -w test.txt -o result.txt
测试总行数: wc.exe -l test.txt -o result.txt
测试代码行数, 注释行数和空的行数: wc.exe -a test.txt -o result.txt
所有功能测试: wc.exe -c -w -l -a test.txt -o result.txt
六、总结:
在编码实现的过程中遇到了很多问题,好在网上有很多参考代码,然后自己根据参考代码写了出来。通过这次作业学会了Git的使用。
参考文献链接