WC项目(JAVA实现)

一、Github地址:https://github.com/gypf/wc

二、PSP表格

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划2031
· Estimate· 估计这个任务需要多少时间2025
Development开发960900
· Analysis· 需求分析 5050
· Design Spec· 生成设计文档3033
· Design Review· 设计复审 2020
· Coding Standard· 代码规范1515
· Design· 具体设计8060
· Coding· 具体编码750800
· Code Review· 代码复审3030
· Test· 测试(自我测试,修改代码,提交修改)180180
Reporting报告6080
· Test Report· 测试报告6070
· Size Measurement· 计算工作量2026
· Postmortem & Process Improvement Plan· 事后总结, 并提出过程改进计划3040
合计 23252335

三、解题思路以及感想、收获

看到题目我首先想到的是这次项目的功能与之前的JAVA课设有这相似的功能,因此我选择用JAVA来实现。首先要一个功能一个功能的实现,然后再整合起来。

1.功能分析

(1)统计字符数(计算文本文档中内容的长度)(实现)

(2)统计单词数(利用空格数)(实现)

(3)统计行数(利用‘\n’的数目)(实现)

(4)统计空行数、注释行数、代码行数(利用正则表达式)(实现)

(5)GUI界面(实现)

(6)支持通配符(未实现)

(7)递归处理目录下符合条件的文件(未实现)

2.遇到难题

(1)

 

当指令输入错误时如何能够再次输入指令。这个问题之前也遇到过并解决,但没有加深记忆,导致这一次慢慢摸索。

(2)空行的正则表达式,由于对正则表达式的不熟料,设计过程中卡在这里很久

 

 

 

 

(3)对注释行的判断,一开始我选择的是使用正则表达式,但是试了好多次都没找到合适的表达式,最后学习了同学的代码决定使用他的方法

 

(4)在主程序中启动图形界面的问题,一开始在输入指令wc.exe -x的指令后并不能马上启动图形界面,要再进行一次输入,失败多次、经过长久的思考后发现是while循环的问题,最后决定添加一个控制变量i来控制,这样才解决了问题

 

 

 

 

 3.感想与收获

正如老师在项目要求中写的一样,Practice makes perfect. 没有捷径可走,就是练习、练习再练习!经过这次的简单个人项目之后,发现我的编程能力真的是十分低,大学两年的学习还是和刚入门一样,这样下去毕业可能都成问题。因此我今后要努力锻炼我的编程能力,多练多思考,提高水平。

四、设计实现过程

项目主要有六个类:

1.WC(主类)

2.CCount(实现字符统计)

3.WCount(实现单词统计)

4.LCount(实现行数统计)

5.Extra(实现扩展功能,对空行数、注释行数、代码行数的统计)

6.WCJFrame(实现GUI界面)

 五、测试运行

1.空文件的测试

(1)字符统计

(2)单词统计

 

 

 

(3)行数统计

 

 

 

(4)空行数、注释行数、代码行数的统计

 

 

 

 

2.只有一个字符的文件的测试

(1)字符统计

(2)单词统计

 

 

 

(3)行数统计

 

 

 

(4)空行数、注释行数、代码行数的统计

 

 

 

3.只有一个词的文件的测试

 

 

 

(1)字符统计

 

 

 

(2)单词统计

 

 

 

(3)行数统计

 

 

 

(4)空行数、注释行数、代码行数的统计

 

 

 

4.只有一行的文件

 

 

 

(1)字符统计

 

 

 

(2)单词统计

(3)行数统计

(4)空行数、注释行数、代码行数的统计

5.典型文件的测试

(1)字符统计

 

 

 

 

 

(2)单词统计

 

 

 

(3)行数统计

(4)空行数、注释行数、代码行数的统计

 

(6)GUI图形界面测试

(1)空文档

 

 

 

(2)一字符文档

 

 

 

(3)一词文档

 

 

 

(4)一行文档

 

 

 

(5)典型文档

 

 

 

六、代码

WC.java

 1 package wc;
 2 
 3 import java.util.Scanner;
 4 
 5 public class WC {
 6 
 7     public static void main(String[] args) {
 8         System.out.println("*********************************************");
 9         System.out.println("               欢迎使用wc统计工具");
10         System.out.println("指令说明:");
11         System.out.println("wc.exe -c  //返回文件的字符数");
12         System.out.println("wc.exe -w  //返回文件的单词数");
13         System.out.println("wc.exe -l  //返回文件的行数");
14         System.out.println("wc.exe -a  //返回.c文件的空行数、代码行数、注释行数");
15         System.out.println("wc.exe -x  //启动图形界面");
16         System.out.println("退出            //退出wc工具");
17         System.out.println("*********************************************");    
18         Scanner scan=new Scanner(System.in);
19         String order=null;
20         int i=1;
21         while(i==1&&(order=scan.nextLine())!="") {
22         switch(order) {
23         case "wc.exe -c":System.out.println("请输入文件路径:");
24                          new Ccount();break;
25         case "wc.exe -w":System.out.println("请输入文件路径:");
26                          new Wcount();break;
27         case "wc.exe -l":System.out.println("请输入文件路径:");
28                          new Lcount();break;
29         case "wc.exe -a":System.out.println("请输入文件路径:");
30                           new Extra();break;
31         case "wc.exe -x":i=0;break;                 
32         default:System.out.println("指令输入错误,请重新输入指令:");
33                 i=1;
34         }
35         
36     }
37         scan.close();
38         if(i==0)
39         new WCJFrame();
40         
41 }}

CCount.java

 1 package wc;
 2 
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.util.Scanner;
 6 
 7 public class Ccount {
 8     Scanner c=new Scanner(System.in);{
 9     String Pathname=null;
10     while((Pathname=c.nextLine())!="") {
11     File file=new File(Pathname);{
12     try {
13         FileReader fr=new FileReader(file);
14         char temp[]=new char[4096];
15         int len= fr.read(temp);//把文件内容读入到数组中并返回数组长度
16         if(len==-1) len=len+1;
17         System.out.println("文件的字符数为"+len);
18         fr.close();
19         }catch(Exception e) {
20             System.out.println("路径不正确,请重新输入路径:");
21         }
22     
23                                  }
24     
25     }
26 
27 }}

WCount.java

 1 package wc;
 2 
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.util.Scanner;
 6 
 7 public class Wcount {
 8     Scanner w=new Scanner(System.in);
 9     String Pathname=null;{
10     while((Pathname=w.nextLine())!="") {
11     File file=new File(Pathname);{
12     try {
13         FileReader fr=new FileReader(file);
14         char temp[]=new char[4096];
15         int num=0;
16         int len= fr.read(temp);//把文件内容读入到数组中并返回数组长度
17         for(int i=0;i<len;i++) {
18             if(temp[i]==' ')
19                 num++;
20             }
21         if(len==-1) num=-1;
22         System.out.println("文本的单词数为"+(num+1));
23         fr.close();
24     }catch(Exception e) {
25         System.out.print("文件路径输入错误,请重新输入:");
26     }
27 }}}}

LCount.java

 1 package wc;
 2 import java.io.BufferedReader;
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.util.Scanner;
 6 public class Lcount {
 7     Scanner l=new Scanner(System.in);
 8     String Pathname=null;{
 9     while((Pathname=l.nextLine())!="") {
10     File file=new File(Pathname);{
11         try {
12         FileReader fr=new FileReader(file);
13         BufferedReader bufr=new BufferedReader(fr);
14         int i=0;
15         while((bufr.readLine())!=null) {
16             i++;
17         }
18         System.out.println("文本行数为"+i);
19         fr.close();
20         bufr.close();
21         }catch(Exception e) {
22             System.out.println("文件路径输入错误,请重新输入:");
23         }
24     }
25 }}}

Extra.java

 1 package wc;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.util.Scanner;
 7 
 8 public class Extra {
 9     Scanner e=new Scanner(System.in);
10     String Pathname=null;
11     String kregex="(\\s)*[{]?";{
12     while((Pathname=e.nextLine())!="") {
13         File file=new File(Pathname);{
14         try {
15             String s=null;
16             int k=0,z=0,d=0;
17             FileReader fr=new FileReader(file);
18             BufferedReader bufr=new BufferedReader(fr);
19             while((s=bufr.readLine())!=null) {
20                 if(s.contains("//"))
21                     z++;
22                 else if(s.matches(kregex))
23                     k++;
24                 else d++;
25                 }                                                               
26             System.out.println("文本的空行数为:"+k);
27             System.out.println("文本的注释行数为:"+z);
28             System.out.println("文本的代码行数为:"+d);
29             fr.close();
30             bufr.close();
31             }
32         catch(Exception e) {
33             e.printStackTrace();
34             System.out.println("文件路径输入错误,请重新输入:");
35                            }
36                                       }
37                                         }
38                         
39                     }
40 }

WCJFrame.java

 1 package wc;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 import java.io.BufferedReader;
 7 import java.io.File;
 8 import java.io.FileReader;
 9 public class WCJFrame extends JFrame implements ActionListener{
10     JTextArea jt1=new JTextArea();
11     public WCJFrame() {
12     this.setBounds(100, 100, 600, 600);    
13     this.setVisible(true);
14     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15     this.setLayout(new GridLayout(2,1));
16     
17     JPanel jp1=new JPanel();
18     JPanel jp2=new JPanel();
19     this.add(jp1);
20     this.add(jp2);
21     
22     JButton jb1=new JButton("选择需要统计的文件");
23     jb1.addActionListener(this);
24     jp1.add(jb1);
25     
26     
27     jt1.setSize(300, 300);
28     jp2.add(jt1);
29 
30 }
31 
32     public void actionPerformed(ActionEvent arg0) {
33         JFileChooser file=new JFileChooser();
34         file.setDialogTitle("请选择文件:......");
35         file.showOpenDialog(null);
36         file.setVisible(true);
37         String path=file.getSelectedFile().getAbsolutePath();//得到用户选择文件的路径
38         File file2=new File(path);{
39             try {
40                 FileReader fr=new FileReader(file2);
41                 BufferedReader bufr=new BufferedReader(fr);
42         //统计字符数        
43             char temp[]=new char[4096];
44                 int len= fr.read(temp);//把文件内容读入到数组中并返回数组长度
45                 if(len==-1) len=len+1;
46                 jt1.setText("文本文档的字符数为"+len);
47                 
48         //统计单词数
49                 int wnum=0;
50                 for(int i=0;i<len;i++) 
51                     if(temp[i]==' ')
52                         wnum++;
53                 jt1.setText(jt1.getText()+"\n"+"文本文档的单词数为"+(wnum+1));
54             
55         //统计行数
56                 int a=0;
57                 for(int i=0;i<len;i++)
58                     if(temp[i]=='\n')
59                     a++;
60                 jt1.setText(jt1.getText()+"\n"+"文本文档的行数为"+(a+1));
61         fr.close();
62         bufr.close();
63         
64         //统计空行、注释行、代码行
65         String kregex="(\\s)*[{]?";
66         String s=null;
67         int k=0,z=0,d=0;
68         FileReader fr2=new FileReader(file2);
69         BufferedReader bufr2=new BufferedReader(fr2);
70         while((s=bufr2.readLine())!=null) {
71                                         if(s.contains("//"))
72                                             z++;
73                                         else if(s.matches(kregex))
74                                             k++;
75                                         else d++;
76                                         }
77                 jt1.setText(jt1.getText()+"\n"+"文本文档的空行数为"+k+"\n"+"文本文档的注释行数为"+z
78                         +"\n"+"文本文档的代码行数为"+d);
79         fr2.close();
80         bufr2.close();}
81     
82     catch(Exception e) {
83         e.printStackTrace();
84     }}}}

转载于:https://www.cnblogs.com/zha11/p/11587503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值