java实现wc.exe

Github地址:https://github.com/ztz1998/wc/tree/master

项目相关要求

实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:

基本功能列表:

wc.exe -c   file.c     //返回文件 file.c 的字符数(实现)

wc.exe -w  file.c    //返回文件 file.c 的词的数目 (实现)

wc.exe -l    file.c      //返回文件 file.c 的行数(实现)

扩展功能:
wc.exe   -s   递归处理目录下符合条件的文件。(未实现)
wc.exe   -a   返回更复杂的数据(代码行 / 空行 / 注释行)。(未实现)

PSP2.1表格

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 30  40

· Estimate

· 估计这个任务需要多少时间

 30  40

Development

开发

 300  500

· Analysis

· 需求分析 (包括学习新技术)

 60  100

· Design Spec

· 生成设计文档

 20  0

· Design Review

· 设计复审 (和同事审核设计文档)

30  60

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 10  20

· Design

· 具体设计

 30  40

· Coding

· 具体编码

 30 50 

· Code Review

· 代码复审

 0  0

· Test

· 测试(自我测试,修改代码,提交修改)

 30  30

Reporting

报告

 120  150

· Test Report

· 测试报告

10  20

· Size Measurement

· 计算工作量

 20 20

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 10  10

合计

   730  1080

解题思路

  大二时学java时候,最后一个作业的第一步就是读取文件,也在网上搜索了WordCounter代码,两项结合,思路就基本出来了。结合开发语言写出实现函数所用的方法,通过网上查阅资料开始编写代码,修改代码。

设计

代码分为两个,一个是菜单(主代码,启动程序),一个是运算(逻辑函数代码),通过对应的命令来控制功能,最后输出。

变量:chars, words, lines.   基本功能的表示

方法:calculate                               基本功能的实现方法。

          switch(arr[0])                    对功能进行分类并打印输出结果。

代码

菜单(主代码,启动程序)

package my10;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
while (true){
System.out.println("$$$$$$$$$$$$$$$$$$$");
System.out.println("1: 字符数、词数、行数");
System.out.println(" 输入命令:");
Scanner input=new Scanner(System.in);
String m=input.nextLine();
String arr[]=m.split("\\s");

try{
switch(arr[0]){
case"1":Counter.counter(); break;
}
}
catch (FileNotFoundException e) {
System.out.println("\n找不到文件");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}

运算(逻辑函数代码)

package my10;

import java.io.*;
import java.util.Scanner;

public class Counter {
public static void counter( ) throws Exception {
Scanner input = new Scanner(System.in);
System.out.println(" 输入路径:");
String path = input.next();
File file = new File(path);
FileReader reader = new FileReader(file);

int countChar = 0;
int countword = 0;
int countline = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr);
while(br.read()!=-1)
{
String s = br.readLine();
countChar += s.length();
countword += s.split(" ").length;
countline++;
}
System.out.println("字符数 "+countChar);
System.out.println("词数"+countword );
System.out.println("行数 "+countline);
}
}

运行结果

遇到的困难及解决方法

1.对java不太熟悉,需要查找资料,并且最后做出来的功能也只有基础功能。

2.起初用switch(arr[0]),出现报错

Cannot switch on a value of type String for source level below 1.7.
Only convertible int values or enum constants are permitted

百度后才知道只有1.7以上才支持String,而我则是1.6

3.代码打完后运行时出现 Could not find the main class,不清楚什么原因,百度后也没解决,最后换了一个编译器才成功

总结

开学的第一个作业,不算很难,但任然做的很辛苦,还得上网查找,找同学帮助,暴露了对java的不熟悉,希望能再慢慢学习,进步。

也是第一次写博客。

猜你喜欢

转载自www.cnblogs.com/ztz1998/p/9648392.html