Question: write a program to process the C/C ++ source program and remove the comments from the source program.
Input: C/C ++ file name
Output: processed program source file
The program pseudocode is as follows:
C1, C2: Char
Tag: int
A: Read a character and save it to C1.
If tag = 0 // The read character is not a comment
If C1 = '/' // it may be a comment mark
Read one character into C2
If C2 = '*' // starts with/**/comment
Tag = 2
Else if C2 = '/' // start with // Annotation
Tag = 1
Else // not annotation mark
Save C1 and C2
Else // read characters as code
Save C1
Else if tag = 1 // read // comments
If C1 = '/N' // end annotation mark
Tag = 0
Else // read/***/annotation tag = 2
If C1 = '*' // it may end with a comment
Read a character and assign it to C2
If C2 = '/' // Yes/**/end of the comment
Tag = 0
Else // The comment is not complete
Nothing
Goto
The program list is as follows:
// Scan the source file and delete the comments <br/> # include <stdio. h> <br/> void scan (char * filename) <br/>{< br/> int tag = 0; // 0: The read character is code; 1: the read characters are // comments; 2: The read characters are/**/comments <br/> char temp1, temp2; <br/> file * In, * out; <br/> In = fopen (filename, "R"); <br/> If (in = NULL) <br/>{< br/> printf ("cannot open the input file! "); <Br/>}< br/> out = fopen (" out. C "," W "); <br/> If (out = NULL) <br/>{< br/> printf (" cannot open the output file! "); <Br/>}< br/> temp1 = fgetc (in); <br/> while (temp1! = EOF) <br/>{< br/> printf ("% C", temp1); <br/> If (0 = tag) <br/>{< br/> If ('/' = temp1) <br/>{< br/> temp2 = fgetc (in ); <br/> If ('*' = temp2) <br/>{< br/> tag = 2; <br/>}< br/> else if ('/' = temp2) <br/>{< br/> tag = 1; <br/>}< br/> else <br/>{< br/> fputc (temp1, out); <br/> fputc (temp2, out ); <br/>}< br/> else <br/> {<br/> fputc (temp1, out ); <br/>}< br/> else if (1 = tag) <br/>{< br/> If ('/N' = temp1) <br/>{< br/> tag = 0; <br/>}< br/> else <br/> {<br/> If ('*' = temp1) <br/>{< br/> temp2 = fgetc (in); <br/> If ('/' = temp2) <br/>{< br/> tag = 0; <br/>}< br/> temp1 = fgetc (in ); <br/>}< br/> fclose (in); <br/> fclose (out); <br/>}< br/> int main () <br/>{< br/> char name [256]; <br/> printf ("Please input the filename:"); <br/> gets (name ); <br/> scan (name); <br/> return 0; <br/>}< br/>