Individual projects wc.exe (C ++)

GitHub:https://github.com/SInXW/wc.exe

One, Title Description

1. Title Description

1. To achieve a simple and complete software tools (statistical characteristics of the source program).
2. The unit test, regression test, performance test, the use of associated tools in the course of achieving the above program.
3. Practice Personal Software Process (PSP), and gradually record their own time spent in each part of software engineering.

 

2.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:
mode handles user needs to:

wc.exe [parameter] [file_name]

 

The basic list of features:

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:
    -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: lines of code instead of the Bank, and the Bank includes comments. An interesting example is that some programmers will add a comment behind a single character:

    } // NOTE
In this case, the line belongs to a comment line.

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

 

Second, problem-solving ideas

 

Detecting user inputs an instruction, and then performs a corresponding operation according to the instruction

Statistics character operation: in order to read character, character statistics

Statistics term operation: continuous read English characters, if the number of words plus a break

Count the number of rows operation: In case the number of rows plus a newline

 

Third, the key code

 

Statistics character operation: in order to read character, character statistics

// character counts 
int countCharacter (FILE * File) {
     int Character = 0 , chNum = 0 ;
     the while (! Feof (File)) 
    { 
        Character = fgetc (File);
         // newline excluding 
        IF (Character == ' \ n- ' ) {
             Continue ; 
        } 
        chNum ++ ; 
    } 
    IF (chNum == 0 ) {
         return  0 ; 
    } 
    return - chNum; 
}

 

Count the number of rows operation: In case the number of rows plus a newline

// number of statistics rows 
int countLine (FILE * File) {
     int lineNum = 0 , Word = 0 ;
     int Flag = 0 ; // identifies whether the file is empty, empty file 0 line 
    IF (File =! NULL) {
         the while (! feof (File)) 
        { 
            Word = fgetc (File);
             // in case of line breaks 
            IF (Word == ' \ n- ' ) { 
                lineNum ++ ; 
            } 
        }         
        return lineNum;      
    }  
}

 

Statistics term operation: continuous read English characters, if the number of words plus a break

//统计词数
int countWord(FILE* file) {
        int character = 0, word = 0, isWord = 0;
        while (!feof(file))
        {
            character = fgetc(file);
            if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))
            {
                if (isWord == 0) {
                    isWord = 1;
                }        
            }
            else 
            {
                if (isWord == 1) {
                    word++;
                    isWord = 0;
                }             
            }
        }
        return word;      
}

 

extensions:

Way: by using a structure _finddata_t (introduced by #include <io.h>), acquiring file information

Ideas: For each file to determine whether the folders, if the folder is recursive in this operation; otherwise perform basic operations (statistical lines, words, characters) for the file;

// recursive processing files under 
/ * 
_finddata_t described structure 
struct _finddata_t { 
    unsigned Attrib; 
    time_t time_create; 
    time_t time_access; 
    time_t time_write; 
    _fsize_t size; 
    char name [_MAX_FNAME]; 
 }; 
 unsigned atrrib: a storage location of the file attributes. File attribute is bit representation, there are some of the following: 
 _A_ARCH (archive), _A_HIDDEN (hidden), _ A_NORMAL (normal), _ A_RDONLY (read-only), 
 _A_SUBDIR (folder), _ A_SYSTEM (system) 

 char name [_MAX_FNAME]: file name. Here is a constant _MAX_FNAME macro, it is first 
 defined in the document represented by the maximum length of the file name. 



 The main function for _finddata_t structure: 

long _findfirst (char * filespec, FileInfo struct _finddata_t *); 
     Return Value: If the search is successful, it will return a handle to a long type of lookup only use. This handle will be used in _findnext function. Failed return -1. 
     Parameters:
        filespec: string marked files, supports wildcards. 
        fileinfo: this is used to store a pointer file structure information. 

int _findnext (long handle, struct _finddata_t * fileinfo); 
     Return Value: returns 0 if successful, -1 otherwise. 
     Parameters: 
        handle: i.e. returned by the function _findfirst back handle. 
        fileinfo: pointer file structure information. After the file is found, the function of this information into the file structure. 

int _findclose (long handle); 
     Return Value: returns 0 on success, -1 failure. 
     Parameters: 
     handle: _findfirst function returns a handle to come back. 
* / 
Void is showAll, ( char * path) {   
    _finddata_t filedir; 
    Long handle;
     char the dir [MaxPath]; 
    strcpy (the dir, path); 
    // add path '/' 
    strcat (the dir, " /*.*");

    //临时保存作用
    char pathTemp[MAXPATH];
    char nameTemp[MAXPATH];
    FILE* file;
    char* abPath;

    if ((handle = _findfirst(dir, &fileDir)) != -1l) {
        while (_findnext(handle, &fileDir) == 0) {          
                //是文件夹:fileDir.attrib == _A_SUBDIR
                if (fileDir.attrib == _A_SUBDIR && strcmp(fileDir.name, ".") != 0 && strcmp(fileDir.name, "..") != 0) {
                    strcpy (the dir, path); 
                    // strcat (the dir, "/"); 
                    strcat (the dir, fileDir.name);
                     // recursive folder 
                    is showAll, (the dir); 
                } 
                the else {
                     // file 
                    IF (strcmp (filedir. name, " . " )! = 0 && strcmp (fileDir.name, " .. " )! = 0 ) {
                         // is c file 
                        IF (CFile (fileDir.name) == the CFILE) { 
                            strcat (path, " / ");
                            strcpy(pathTemp, path);
                            strcpy(nameTemp, fileDir.name);
                            abPath = strcat(pathTemp, nameTemp);
                            printf("\n*****************************************\n");
                            printf("\t%s%s\n", path, fileDir.name);
                            file = fopen(abPath, "r");                                          
                            printf("\n\t行数:%d 行\n", countLine(file));
                            File = the fopen (abPath, " R & lt " ); 
                            the printf ( " \ n-\ T characters:% d a \ n- " , countCharacter (File)); 
                            File = the fopen (abPath, " R & lt " ); 
                            the printf ( " \ n- \ t word count:% d months \ the n- " , countWord (File)); 
                            printf ( " \ the n-*************************** ************** \ n- " );                            
                        } 
                    }            
                }
            }      
    }          
    _findclose(handle);
}

 

 

Fourth, the test

 

(1) command is not input

 

(2) non-C files

 

(3) empty file

 

(4) a single character file

 

(5) single-word file

 

(6) single-line file

 

(7) Typical Source File

 

Lower (8) --- recursive processing extensions directory eligible files

 

Five, PSP table

 

 

VI Project Summary

 

1. I did not feel the consequences brought about good design in the programming process. When the extended recursive processing functions, because they did not take into account the subsequent expansion of problems do basic functions, and therefore do extensions, they need to return to modify the underlying function.

 For example, a start will be a variety of situations in the various basic functions in direct output function, but when a recursive process, non-C and do not need to file for output, you need to put out some cases output function.

2. The plan also reflects the lack of a time to start considering the entire task and overestimate their own ability and the huge difference between the actual time-consuming.

Guess you like

Origin www.cnblogs.com/InXX/p/12551356.html