Presentation is loading. Please wait.

Presentation is loading. Please wait.

軟體實驗: C-Lab5 虞台文. Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains.

Similar presentations


Presentation on theme: "軟體實驗: C-Lab5 虞台文. Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains."— Presentation transcript:

1 軟體實驗: C-Lab5 虞台文

2 Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains.

3 What is a word? A word is a sequence of alphabets terminated by a blank, a period, a comma, the newline character, or the EOF character. : space : tab \t : newline \n : EOF blank The definition is incomplete here.

4 Tai-Wen Yue35Taipei, Taiwan Example I am young. My data is as follows. Name 36c 123456789012345678901234567890123456789 8w AgeOffice 19c3w 32c5w : space : tab \t : newline \n : EOF blank There are 3 lines, 87 characters, and 16 words in the article.

5 Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains. A word is a sequence of alphabets terminated by a blank, a period, a comma, the newline character, or the EOF character. You can be assured that the input file contains nothing other than alphanumeric characters and the punctuation marks mentioned above.

6 軟體實驗: C-Lab5 Algorithm

7 Type of Character Encountered ’ ’, ’\t’ : blank ’,’, ’;’ ’.’, ’!’, ’:’ : punctuation ’\n’ : newline EOF : end of file Those not described below : normal 0 1 2 3 4

8 Type of Character Encountered ’ ’, ’\t’ : blank ’,’, ’;’ ’.’, ’!’, ’:’ : punctuation ’\n’ : newline EOF : end of file Those not described below : normal 0 1 2 3 4 #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4

9 Helper Function int CharType(char c) Returns CT_NORMAL, CT_BLANK, CT_PUNCTUATION, CT_NEWLINE, or CT_EOF according to the value of c described above.

10 States I am young. My data is as follows. Newline Start In Word Not In Word In Word Not In Word Newline Start Newline Start Newline Start In Word Not In Word Not In Word CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL

11 States Finish CT_EOF #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; Newline Start Newline Start In Word Not In Word Not In Word CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE

12 The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0;

13 The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. }

14 The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } Done by Parse(int type);


Download ppt "軟體實驗: C-Lab5 虞台文. Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains."

Similar presentations


Ads by Google