Delete the annotation in C/C ++-Finite State Machine (implemented in C language)

Source: Internet
Author: User

Some time ago, I wrote a code to delete the C/C ++ source.ProgramThe structure of the annotated program in is complicated. Now we need to use finite state machines to solve the problem.CodeIf strings and nesting are not taken into account, you need to make a few modifications to them. The Code is as follows:

 

# Include <stdio. h> <br/> Enum {<br/> code, <br/> comment1, // "/**/" type comment <br/> comment2, // "//" type comment <br/>} g_state = Code; <br/> Enum input {<br/> CH, // other characters <br/> success, // "/*" or "*/" <br/> CH2, // "//" <br/> RT, // "/N" <br/>}; <br/> input alter (char CH, file * file) <br/>{< br/> char temp; <br/> If (CH = '/') <br/>{< br/> temp = fgetc (File ); <br/> If (temp = '/') return CH2; <br/> else if (temp = '*') return else; <br/> else <br />{< Br/> fseek (file,-1, 1); <br/> return ch; <br/>}< br/> else if (CH = '*') <br/>{< br/> temp = fgetc (File ); <br/> If (temp = '/') return response; <br/> else <br/>{< br/> fseek (file,-1, 1 ); <br/> return ch; <br/>}< br/> else if (CH = '/N') <br/> return RT; <br/> else <br/> return ch; <br/>}< br/> int main () <br/>{< br/> file * infile = fopen ("infile. CPP "," R "); <br/> file * OUTFILE = fopen (" OUTFILE. CPP", "W"); <br/> input; <br/> If (infile = NULL) <br/>{< br/> printf ("cannot open infile! "); <Br/> return-1; <br/>}</P> <p> char ch; <br/> CH = fgetc (infile ); <br/> while (Ch! = EOF) <br/>{< br/> input = alter (CH, infile); <br/> switch (g_state) <br/>{< br/> case code: <br/> If (input = CH | input = RT) fputc (CH, OUTFILE ); <br/> else if (input = inputs) g_state = comment1; <br/> else if (input = CH2) g_state = comment2; <br/> else; <br/> break; <br/> case comment1: <br/> If (input = inputs) g_state = Code; <br/> break; <br/> case comment2: <br/> If (input = RT) <br/>{< br/> fputc ('/N', OUTFILE); <br/> g_state = Code; <br/> break; <br/>}< br/> CH = fgetc (infile ); <br/>}</P> <p> printf ("complete! "); <Br/> getchar (); <br/> fclose (infile); <br/> fclose (OUTFILE); <br/> return 0; <br/>}< br/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.