github项目地址:
https://github.com/changrui520/homework
作业要求:
可执行程序命名为:wc.exe。
该程序处理用户需求的模式为:wc.exe [parameter] [input_file_name]
存储统计结果的文件默认为result.txt,放在与wc.exe相同的目录下。
输入:wc.exe -c file.c
读取file.c,统计字符数,输出字符数并写入到result.txt中。
输入:wc.exe -w file.c
读取file.c,统计单词数,输出单词数并写入到result.txt中。
输入:wc.exe -l file.c
读取file.c,统计行数,输出行数并写入到result.txt中。
| PSP2.1 | PSP阶段 | 预估耗时(分钟) | 实际耗时(分钟) |
| · Planning | · 计划 | 15 | 10 |
| · Estimate | · 估计这个任务需要多少时间 | 30 | 20 |
| · Development | · 开发 | 60 | 40 |
| · Analysis | · 需求分析 (包括学习新技术) | 5 | 5 |
| · Design Spec | · 生成设计文档 | 5 | 5 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 10 | 5 |
| · Coding | · 代码规范 (为目前的开发制定合适的规范) | 10 | 5 |
| · Code Review | · 具体设计 | 15 | 20 |
| · Test | · 具体编码 | 60 | 40 |
| · Reporting | · 代码复审 | 10 | 5 |
| · Test Report | · 报告 | 10 | 5 |
| · Size Measurement | · 测试报告 | 30 | 20 |
| · Postmortem & Process | · 计算工作量 | 15 | 10 |
| · Improvement Plan | · 事后总结, 并提出过程改进计划 | 10 | 5 |
| · 合计 | 270 | 250 |
思路:
使用IO流中的字符流实现。
Main类:读取用户录入的内容,根据内容调用utils类中的相应方法。
Utils类:负责具体处理。charNum方法:统计字符数,wordNum方法:统计单词数,lineNum方法:统计行数。
开发环境及工具:
win10+idea+jdk9.0.1+git+mavem
关键代码:
Main:
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String currentCommand = null;
String[] commands = null;
while (true) {
try {
//判断命令是否为空
if ((currentCommand = br.readLine()) != null) {
commands = currentCommand.split(" ");
//判断是否为3个词
if (commands.length == 3) {
//判断第一个词是否为wc.exe
if ("wc.exe".equals(commands[0])) {
//判断第三个词是否以.c结尾
if (!commands[2].endsWith(".c")) {
System.out.println("必须以.c结尾");
continue;
}
//判断第二个词
switch (commands[1]) {
case "-c":
System.out.println(charNum(commands[2]));
break;
case "-w":
System.out.println(wordNum(commands[2]));
break;
case "-l":
System.out.println(lineNum(commands[2]));
break;
default:
System.out.println("错误,请重新输入");
break;
}
} else {
System.out.println("错误,请重新输入");
continue;
}
} else {
System.out.println("错误,请重新输入");
continue;
}
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("读取文件失败");
}
}
}
}
Utils:
public class Utils {
public static int charNum(String fileName) throws IOException {
Integer num = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt"));
int c;
//统计字符数
while((c=br.read())!=-1){
if((char)c!=‘\r‘&&(char)c!=‘\n‘){
num++;
}
}
//写出结果
bw.write(fileName+","+"字符数"+":");
bw.write(num.toString());
//关流
br.close();
bw.close();
return num;
}
public static int wordNum(String fileName)throws IOException{
Integer num=0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt"));
String currentLine=null;
//储存分割后的数组
String[] words=null;
while((currentLine=br.readLine())!=null){
currentLine=currentLine.replaceAll("[^_a-zA-Z]"," ");
words=currentLine.split(" ");
for(String word:words){
//去掉空格,和空字符
if(!word.equals(" ")&&!word.equals("")){
num++;
}
}
}
bw.write(fileName+","+"单词数"+":");
bw.write(num.toString());
br.close();
bw.close();
return num;
}
public static int lineNum(String fileName)throws IOException{
Integer num=0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt"));
while((br.readLine())!=null){
num++;
}
bw.write(fileName+","+"行数"+":");
bw.write(num.toString());
br.close();
bw.close();
return num;
}
}
共计10个测试用例:
1.wc.exe -c file.c
结果:


2.wc.exe -w file.c
结果:


3.wc.exe -l file.c
结果:


4.a.exe -c file.c
结果:

5.wc.exe -x file.c
结果:

6.wc.exe
结果:

7.wc.exe -c file.c file.c
结果:

8.wc.exe -c a.c
结果:

9.空命令
结果:

10..wc.exe -c file.java
结果:

全部达到预期要求
作业心得:
通过这次wc作业的开发,我对软件工程这门专业有了更深入的了解和感悟,软件的开发不仅仅是简单地编写代码,还要注意设计的规范化,合理化,代码的安全性,程序的健壮性。软件上线之前必须经过单元测试。想要让用户觉得自己的程序好用,必须要考虑周到。
原文:https://www.cnblogs.com/a154627/p/9693045.html