The second small project in software engineering -- wc, the second small wc in software engineering
Github source code and engineering file address:Https://github.com/HuChengLing/wc
Basic requirements: the basic function of wc is to collect statistics on the number of characters, words, and lines in a file.
Main functions:The number of characters, words, and lines in the file, and comments.
Design Philosophy: Read a line of Characters in the file to count the number of rows, and then use the character array to count the number of characters and the number of words respectively.
Program code:
# Include <stdlib. h> # include <stdio. h ># include <string> void signalcount (char * Filename); void main () {int c = 0, w = 0, l = 0; int signline = 0; FILE * fp; char fileName [80]; char ch [1024]; // bool inword = false; // In a word, inward equals to ture printf ("please input fileName: "); gets_s (fileName); fp = fopen (fileName," r "); if (fp = NULL) {printf ("can't open file % s \ n", fileName); getchar (); exit (1) ;}while (! Feof (fp) {fgets (ch, 1024, fp); l ++; int length = strlen (ch); for (int I = 0; I <length; I ++) {// if it is a space or not, execute I ++ in the while clause to determine whether the next space appears or end if (ch [I]! = '') {W ++; // if (ch [I]> '0' & ch [I] <'9' | ch [I]> 'A' & ch [I], 'Z' | ch [I]> 'A' & ch [I] <'Z') & ch [I]! = '') // C ++; while (ch [I]! = ''& Ch [I]! = '\ 0 ') {if (ch [I]> '0' & ch [I] <'9' | ch [I]> 'A' & ch [I], 'Z' | ch [I]> 'A' & ch [I] <'Z') & ch [I]! = ''& Ch [I]! = '\ N') c ++; I ++ ;}}} printf ("Total: % d rows % d Words % d characters", l, w, c); signalcount (fileName); fclose (fp); system ("pause");} void signalcount (char * filename) {int len = 0, I = 0, signline = 0; FILE * fp; char str [1000]; fp = fopen (filename, "r"); if (fp = NULL) {printf ("add content to the file \ n"); exit (-1);} while (! Feof (fp) {fgets (str, sizeof (str), fp); len = strlen (str); for (I = 0; I <len; I ++) {if (str [I] = '/' & str [I + 1] = '/') | (str [I] = '/' & str [I + 1] = '*') {signline ++; break ;}}} fclose (fp); printf ("comment row % d row \ n", signline );}
Although I wrote some code myself, I also borrowed a lot of online code. In addition, there are still bugs that cannot read Chinese characters that need to be improved.