软件工程firstblood
https://github.com/happyeven/WC
项目要求
wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:
程序处理用户需求的模式为:
wc.exe [parameter] [file_name]
- 基本功能列表(已完成):
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的词的数目
wc.exe -l file.c //返回文件 file.c 的行数
关键代码or设计说明
1.单词、行数、字符的计数
public class Counter {
private long character;
private long word;
private long line;
private String text;
private long countChar() {
return text.length();
}
private long countWord() {
boolean blank = true;
long word = 0;
for (char c : text.toCharArray()) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
if (blank){
blank = false;
word++;
}
}else if (c == '-' && !blank){
continue;
}else {
blank = true;
}
}
return word;
}
private long countLine() {
long line = 0;
int index = -1;
while((index=text.indexOf("\n", index)) > -1) {
index++;
line++;
}
return line;
}
public Counter(String text) {
this.text = text;
}
public void count() {
character = countChar();
word = countWord();
line = countLine();
}
public long getWord() {
return word;
}
public long getLine() {
return line;
}
public long getCharacter() {
return character;
}
}
2文件的读取并转换成string类型
import java.io.*;
public class ReadFileToString {
public static String readFileToString(String fileName) {
File file = new File(fileName);
Long length = file.length();
byte[] bytes = new byte[length.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(bytes);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new String(bytes);
}
}
3主类,输出需要的结果
public class WC {
public static void main(String[] args) {
String path = args[args.length-1];
// get the file
if (args.length <= 0){
System.err.println("please input the argument");
return;
}
String text = ReadFileToString.readFileToString(path);
// count
Counter counter = new Counter(text);
counter.count();
// output result
System.out.print(path + ": ");
for (int i = 0; i < args.length-1; i++) {
switch (args[i]) {
case "-c" :System.out.print("Character: " + counter.getCharacter() + " "); break;
case "-w" : System.out.print("word: " + counter.getCharacter() + " "); break;
case "-l" : System.out.print("line: " + counter.getCharacter() + " "); break;
default: break;
}
}
}
}
PSP
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | ||
| · Estimate | · 估计这个任务需要多少时间 | 10 | 5 |
| Development | 开发 | ||
| · Analysis | · 需求分析 (包括学习新技术) | 120 | 130 |
| · Design Spec | · 生成设计文档 | 60 | 35 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 40 | 30 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 20 | 30 |
| · Design | · 具体设计 | 60 | 60 |
| · Coding | · 具体编码 | 360 | 480 |
| · Code Review | · 代码复审 | 30 | 30 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 60 | 60 |
| Reporting | 报告 | 20 | 20 |
| · Test Report | · 测试报告 | 60 | 10 |
| · Size Measurement | · 计算工作量 | 30 | 30 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 20 |
| 合计 | 920 | 940 |

浙公网安备 33010602011771号