Personal items (WordCount)

GitHub:https://github.com/yozyyyqls/WordCounter

1.0 PSP table

PSP2.1

Personal Software Process Stages

Estimated time consuming (minutes)

The actual time-consuming (minutes)

Planning

plan

 90

 70

· Estimate

• Estimate how much time this task requires

5

10

Development

Develop

 600

 960

· Analysis

· Needs analysis (including learning new technologies)

 300

 400

· Design Spec

Generate design documents

 30

 30

· Design Review

· Design Review (and his colleagues reviewed the design documents)

 10

 6

· Coding Standard

· Code specifications (development of appropriate norms for the current development)

 60

 120

· Design

· Specific design

 60

 120

· Coding

· Specific coding

 600

 720

· Code Review

· Code Review

 30

 60

· Test

· Test (self-test, modify the code, submit modifications)

 45

 60

Reporting

report

 20

 15

· Test Report

· testing report

 30

 20

· Size Measurement

· Computing workload

 5

 10

· Postmortem & Process Improvement Plan

· Hindsight, and propose process improvement plan

 10

 10

total

 

1895

 2611

 

 

1.1 WC project requirements

wc.exe is a common tool, it can count the number of characters of text files, words, and lines. The project asked to write a command line program, to mimic the function of the existing wc.exe, and be expanded, given the number of characters in a programming language source files, words, and lines.

Implement a statistical program, the number of characters it can correct statistics program file, the number of words, lines, and also has other extended functions, and can handle multiple files quickly.

Specific functional requirements:

Program mode processing user requirements are as follows:

wc.exe [parameter] [file_name]

 

Basic functions list (realized):

wc.exe -c file.c // returns the number of characters in the file file.c

The number of wc.exe -w file.c // Returns the file of the word file.c  

wc.exe -l file.c // returns the number of rows of file file.c

 

Extensions (realized):

    -s recursive processing files that meet the conditions of the directory.

    -a return more complex data (line / space lines / comment lines).

Blank line: Bank format all control characters or spaces, if included code, no more than a displayable character, such as "{."

Line: Bank codes comprises more than one character.

Comment lines: The Bank is not a line of code, and the Bank includes comments. An interesting example is that some programmers will add a comment behind a single character:

    } // comment

In this case, the line belongs to the comment line.

[File_name]: file or directory name, you can handle general wildcard.

 

Advanced features (realized):

 -x parameter. This parameter is used alone. If you have the command line parameters, the program will display a graphical interface, the user can select a single file through the interface, the program will display the file number of characters, lines, etc. All statistics.

Demand For example:

wc.exe -s -a * .c

 

The number of lines of code returns the current directory and all subdirectories * .c files, the number of blank lines, comments, number of rows.

 

1.2 description of problem-solving ideas

  • Topics involving file operations, certainly require an operator IO stream.
  • Basic Functions:
    • Calculate the number of characters and the number of words directly using regular expressions to match;
    • Line counting the number of the transport is calculated, and finally add a.
  • Expanded Functions:
    • Comment line calculated by line reading, and then used to match a regular;
    • Calculating row read press blank line, end to end blank characters per line is removed, the remaining number of characters is compared with a blank line is 0 or 1;
    • Calculating line, does not satisfy the above two conditions, if a row is read, the line behavior.
  • Advanced Features section:
    • Use javafx

1.3 design and implementation process

Package project directory structure:

 

 

Each module dependencies:

1.4 Key Code Description

  • Basic Functions of the core code

public enum CountImpl implements Count {
    OP_C{
        @Override
        public int op(byte[] fileByte) {
            if(fileByte==null) return 0;    //文件为空
            String fileStr = new String(fileByte);
            String pattern = "\\S";
            return FileUtil.searchStr(pattern, fileStr);
        }
    },


    OP_W{
        @Override
        public int op(byte[] fileByte){
            if(fileByte==null) return 0;
            String fileStr = new String(fileByte);
            String REGEX = "[a-zA-Z]+\\W";
            return FileUtil.searchStr(REGEX, fileStr);
        }
    },


    OP_L{
        @Override
        public int op(byte[] fileByte){
            if(fileByte==null) return 0;
            String REGEX = "\\n";
            FileStr String= New new String (fileByte);
             return FileUtil.searchStr (REGEX, fileStr) + 1'd; // last line usually without the transport 
        } 
    }; 
}

 

  • Expanded Functions of the core code

/ ** 
 * batch processing document 
 * @param processing mode selected submodel 
 * @param filePath file absolute path 
 * @throws IOException file does not exist
  * / 
public  static  void batchProCount (submodel String, String filePath) throws IOException { 
    File File = new new File (filePath);
     int NUM = 0 ;
     IF (file.isFile ()) { // if the file 
        byte [] = fileByte FileUtil.fileToByte (filePath); 
        NUM = CountImpl.valueOf (submodel) .OP (fileByte); / / which function is used depending on the mode of automatically distinguish
        System.out.println ( "File Path:" + file.getAbsolutePath ()); 
        System.out.println ( "Calculation Results:" NUM + + "\ n-" ); 
    } the else  IF (file.isDirectory ()) { // If the directory 
            file [] = the fileList File.listFiles ();
             IF (the fileList == null ) { // If there is no file in the directory 
                System.out.println ( "file does not exist" );
                 return ; 
            } 
            for ( value File: the fileList) { 
                batchProCount (submodel, value.getAbsolutePath ()); // recursive process 
            }  
        }
} 




/ ** 
 * calculated number of lines of code file, comment number of rows, the number of blank lines 
 * @param filePath file path 
 * @return num [0] is the number of lines of code [1] is the number of comment lines num, num [2] is number of blank lines 
 * @throws IOException file does not exist
  * / 
public  static  int [] Procount (String filePath) throws IOException {
     int [] NUM = new new  int [. 3 ]; 
    file file = new new file (filePath);
     IF (! file. eXISTS ()) { 
        System.out.println ( "file does not exist" );
         return NUM; // file exists
     }
    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
    int codeLineNum=0;
    int noteLineNum=0;
    int blankLineNum=0;
    String fileLine;
    while ((fileLine=bufferedReader.readLine())!=null){
        fileLine = fileLine.trim();
        //计算注释行
        if(fileLine.startsWith("/") || fileLine.startsWith("*")){
            noteLineNum++;
        }
        //计算空行
        else if(fileLine.trim().isEmpty() || fileLine.trim().length()==1){
            blankLineNum++;
        }
        //计算代码行
        else {
            codeLineNum++;
        }
    }
    bufferedReader.close();
    num[0] = codeLineNum;
    num[1] = noteLineNum;
    num[2] = blankLineNum;
    return num;
}

 

  • Advanced features part of the core code

@FXML
public void open(ActionEvent actionEvent) throws IOException {
    File file = OpenUtil.chooseFile();
    long start = System.currentTimeMillis();
    if(file==null){
        warn.setText("你没有打开任何文件噢~");
        return;
    }else{
        path.setText(file.getAbsolutePath());
        int[] num;


        num = WordCount.wc("-c", file.getAbsolutePath());
        charNum.setText(String.valueOf(num[0]));
        num = WordCount.wc("-w", file.getAbsolutePath());
        wordNum.setText(String.valueOf(num[0]));
        num = WordCount.wc("-l", file.getAbsolutePath());
        lineNum.setText(String.valueOf(num[0]));


        num = ProWordCount.proCount(file.getAbsolutePath());
        codeNum.setText(String.valueOf(num[0]));
        noteNum.setText(String.valueOf(num[1]));
        blankNum.setText(String.valueOf(num[2]));
    }
    long runTime = TimeUtil.getRunTime(start);
    runtime.setText(runTime +"ms");
}

 

1.5 WC test project

Test file:

Empty file

Only one character file

Only a word file

One-line file

A typical source file

 

Empty file test

 

 

 

 

 

 

 

 

Single-character file test

 

 

Single word file test

 

 

Single-line file test

 

 

A typical source file test

package com.demo;
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class WordCounter {
    public static void main(String[] args) throws IOException {
        /*
        Scanner scan = new Scanner(System.in);
        String model = "";
        String filePath = "";
        WordCounter.wc(model, filePath);
         */
        System.out.println(wc("-l", "C:\\Users\\15191\\Desktop\\imageTest\\testBrunch\\test\\C_language_Class2_1_utf8.txt"));
}

 

 

 

 

 

 

 

 

 

 

 

 

 

GUI program testing

 

 

 

 

2.1 Project Summary

  1. When actually writing code takes more time to planning the relationship between each module, but did not do a good job;
  2. We spent more time in the learning process javafx in;
  3. In the project packaged into .exe process also encountered a lot of small problems;
  4. The need to strengthen the programming exercises.

Guess you like

Origin www.cnblogs.com/yozyyyqls/p/12554092.html