个人项目WordCount基础功能
码云地址:https://gitee.com/stedylan/WordCount
1.PSP表格:
|
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
|
Planning |
计划 |
10 |
10 |
|
· Estimate |
· 估计这个任务需要多少时间 |
600 |
590 |
|
Development |
开发 |
500 |
560 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
60 |
80 |
|
· Design Spec |
· 生成设计文档 |
20 |
0 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
0 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 |
20 |
|
· Design |
· 具体设计 |
30 |
10 |
|
· Coding |
· 具体编码 |
300 |
380 |
|
· Code Review |
· 代码复审 |
20 |
10 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
110 |
160 |
|
Reporting |
报告 |
100 |
0 |
|
· Test Report |
· 测试报告 |
30 |
0 |
|
· Size Measurement |
· 计算工作量 |
35 |
0 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
35 |
30 |
|
合计 |
600 |
590 |
2.项目分析:
该项目的要求为WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件result.txt中。-c为统计字符数,-w为统计单词数,-l为统计行数,-o + 文件名为将结果输出到指定的文件中。并要求以命令行的形式对程序进行输入和输出。
解决思路:
在这次项目中,我选择用java完成。首先需要通过流读取文件,然后通过循环一行一行的读取字符串,并对每一行字符串进行统计。当读到文章末尾时,关闭文件。然后打印结果并写入到文件中。最后打包成exe文件并上传至码云。
3.实现过程:
在设计代码时,我将项目分为两个类。一是统计所用的方法类和客户端。

在Count类中有两个方法,本别实现了统计单词数和统计字符数。
代码说明:
在这段代码中,我实现了统计单词数量的功能。该方法中的参数为每一行的字符串。从第一个字符开始遍历,当遇到字母时,然后从当前位置进行二次遍历,知道当前字符不为字母,这时单词数+1。该方法返回统计数。
public static int Words(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {// 遇到字母时遍历直到出现不是字母的字符,此时单词数+1
if (i == str.length() - 1)
break;
else {
int j = 0;
for(j=i;j<str.length();j++)
{
if(!((str.charAt(j) >= 'a' && str.charAt(j) <= 'z')
|| (str.charAt(j) >= 'A' && str.charAt(j) <= 'Z')))
break;
}
i = j;
count++;
}
}
}
return count;
}
关于主函数,其中遇到的问题有如何通过命令行将参数传递,借鉴了该博客https://www.cnblogs.com/xy-hong/p/7197725.html。
在这段代码中,实现了可以输入多条命令来输出多项统计结果。这里我主要运用了集合中的方法,将数组当成集合,判断集合当中是否有这个元素,从而完成选择功能。
if (Arrays.asList(args).contains("-o")) {
fw = new FileWriter(args[args.length - 1], true);
if (Arrays.asList(args).contains("-c")) {
fw.write(args[args.length - 3] + "," + "字符数:" + countChar+"\r\n");
System.out.println(args[args.length - 3] + "," + "字符数:" + countChar);
}
if (Arrays.asList(args).contains("-w")) {
fw.write(args[args.length - 3] + "," + "单词数:" + countWord+"\r\n");
System.out.println(args[args.length - 3] + "," + "单词数:" + countWord);
}
if (Arrays.asList(args).contains("-l")) {
fw.write(args[args.length - 3] + "," + "行数:" + countLine+"\r\n");
System.out.println(args[args.length - 3] + "," + "行数:" + countLine);
}
fw.flush();
} else {
fw = new FileWriter("result.txt", true);
if (Arrays.asList(args).contains("-c")) {
fw.write(args[args.length - 1] + "," + "字符数:" + countChar+"\r\n");
System.out.println(args[args.length - 1] + "," + "字符数:" + countChar);
}
if (Arrays.asList(args).contains("-w")) {
fw.write(args[args.length - 1] + "," + "单词数:" + countWord+"\r\n");
System.out.println(args[args.length - 1] + "," + "单词数:" + countWord);
}
if (Arrays.asList(args).contains("-l")) {
fw.write(args[args.length - 1] + "," + "行数:" + countLine+"\r\n");
System.out.println(args[args.length - 1] + "," + "行数:" + countLine);
}
fw.flush();
}
其中还有遇到的问题是我读取文件时,每行首字母都会丢失。这个问题困扰了我很久。后来借鉴了该博客http://www.cnblogs.com/mybook/archive/2011/12/15/2289362.html,原因是bufferreader.read()方法会将首字母读出,这样用readline方法时便不会读取。按照博客里修改代码,最后解决了问题。
while ((s=br.readLine()) != null) {
countChar += Count.Chars(s);
countWord += Count.Words(s);
countLine++;
}
4.1测试代码:
在测试中,我主要的思路是通过java调用cmd控制台,并预设置多条输入指令,并在java控制台显示出来,这样以达到多次测试的功能。
具体代码(借鉴自https://blog.csdn.net/shenxiaomo1688/article/details/79196625):
通过executeCmd方法打开命令行,并执行相应指令。其中用了两个C源代码、一个java源代码和一个C#源代码作为测试用例。
public class TestClient {
public static String executeCmd(String command) throws IOException {
System.out.println("Execute command : " + command);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null) {
System.out.println(line);
build.append(line);
}
return build.toString();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
executeCmd("G:\\WordCount\\wc.exe -c test.cpp");
executeCmd("G:\\WordCount\\wc.exe -w sort.cpp");
executeCmd("G:\\WordCount\\wc.exe -l test.cpp");
executeCmd("G:\\WordCount\\wc.exe -c sort.cpp -o AutoTest1.txt");
executeCmd("G:\\WordCount\\wc.exe -c -w LoginDao.java -o AutoTest2.txt");
executeCmd("G:\\WordCount\\wc.exe -c -w -l sort.cpp -o AutoTest3.txt");
executeCmd("G:\\WordCount\\wc.exe -c -l test.cpp");
executeCmd("G:\\WordCount\\wc.exe -c -w Program.cs");
executeCmd("G:\\WordCount\\wc.exe -l -w test.cpp");
executeCmd("G:\\WordCount\\wc.exe -c -w -l LoginDao.java");
}
}
4.2测试结果:

参考文献:
https://www.cnblogs.com/xy-hong/p/7197725.html
https://www.cnblogs.com/xy-hong/p/7197725.html
http://www.cnblogs.com/mybook/archive/2011/12/15/2289362.html
---恢复内容结束---
个人项目WordCount基础功能的更多相关文章
- WordCount 基础功能
软测第一次作业 该项目在码云上的地址: https://gitee.com/zhege/WordCount 一,概述 WordCount的基础功能需求分析大致如下:对程序设计语言源文件统计字符数.单词 ...
- 项目开发-->基础功能汇总
祭奠曾经逝去的青春…… 1.基础功能汇总-->身份认证及用户登录模块 2.基础功能汇总-->一键登录功能汇总 3.堆和栈 4.变量
- wc基础功能
第一次作业 项目地址 https://gitee.com/xxlznb/WordCount PSP WordCount 预估耗时(分钟) 实际耗时 计划 20 30 预估任务需要时间 20 30 开发 ...
- WordCount基本功能
WordCount基本功能 码云地址:https://gitee.com/Joker_zou/WordCount.git 一.项目需求 WordCount的需求可以概括为:对程序设计语言源文件统计字符 ...
- 系统分析与设计结对项目——WordCount
结对项目完成WordCount 合作者:201631062507 201631062526(学号) 代码地址:https://gitee.com/WordCountMC/WordCountTeam ...
- 可在广域网部署运行的QQ高仿版 -- GG叽叽V3.0,完善基础功能(源码)
(前段时间封闭式开发完了一个项目,最近才有时间继续更新GG的后续版本,对那些关注GG的朋友来说,真的是很抱歉.)GG的前面几个版本开发了一些比较高级的功能,像视频聊天.远程桌面.文件传送.远程磁盘等, ...
- 谷歌Chrome浏览器开发者工具的基础功能
上一篇我们学习了谷歌Chrome浏览器开发者工具的基础功能,下面介绍的是Chrome开发工具中最有用的面板Sources.Sources面板几乎是最常用到的Chrome功能面板,也是解决一般问题的主要 ...
- Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
6月17日是Apache Karaf作为Apache顶级项目.Karaf是个运行时包,包含了一个OSGi框架(Equinox或Felix).一个命令shell(Felix Gogo)及默认情况下内置的 ...
- VC项目配置基础以及快捷键(收藏)
来自http://blog.csdn.net/phunxm/article/details/5082488 一.IDE基础配置 1.字体 VC6中“Tools→Options→Format→Font” ...
随机推荐
- Angular中ngx-image-cropper图片裁剪的使用
GitHub示例源码地址:https://github.com/luoruiemail/ngx-image-cropper 下载下来之后,执行yarn install安装相关node_modules包 ...
- 关于c调用lua 对‘luaL_newstate()’未定义的引用的问题解决办法
#include <string.h>#include "lua.h"#include "lauxlib.h"#include "lual ...
- vi去掉^M
Windows下编辑的文本文件or程序文件,拷贝到linux下,用vi打开时,有时候会看到每行末尾有个 ^M 的符号. 这里介绍一个可以快速去除^M符号的方法: 用vi编辑器打开该文件,然后输入: : ...
- memcached命令行、Memcached数据导出和导入
1.memcached命令行 telnet 127.0.0.1 11211set key2 0 30 2abSTOREDget key2VALUE key2 0 2abEND 如: set key3 ...
- The import javax.websocket cannot be resolved的解决问题
在eclipse中导入项目的时候出现了这个问题,废了我半天劲,才搞明白,把问题记录下来,方便大家以后遇到这问题好处理.提供参考. 出现的问题截图: 因为我用的是tomcat8, 大体步骤:项目上点右键 ...
- Spring系列七:Spring 自动装配
相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...
- django 聚合统计查询
from django.shortcuts import renderfrom django.http import HttpResponsefrom django.db.models import ...
- docker-配置网桥-自定义网络
容器网络访问原理 桥接宿主机网络 临时生效: # 网桥名称 br_name=br0 # 添加网桥 brctl addbr $br_name # 给网桥设置IP ip addr add 192.168 ...
- spring boot 学习笔记(三)之 配置
一:概述 在Spring boot 中根据业务需求,我们往往会在不同地方配置我们所需的key-value 配置项,配置文件存在不同的地方的场景如下: (1) 默认存在 application.prop ...
- mybatis数组和集合的长度判断及插入
1.在使用foreach的是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下4种情况: 如果传入的是单参数且参数类型是一个List的时候,collect ...