C Language Small Item-comment conversion

Source: Internet
Author: User
Tags fread

The purpose of this small project is to convert C-style annotations in. c files into C + + style annotations.

c Annotation Style:/**/
C + + annotation style://

First we analyze the states that will appear, and the transition relationships between the various states:
First, we've labeled some states here, and there are 4 states we know of here,
The first kind is end_state.
The second kind is c_state.
The third Kind is cpp_state.
The fourth kind is our usual null_state.

Here, the comment conversion operation is the file, so I'm going to take the file operation.

First we want to open the Input.c file, read the file inside the file, and then process it, otherwise the user is directly prompted to have an error.
Next, we also open the Output.c file and write to the code that will process it.

Next, we will analyze the various scenarios that may be encountered:
Input.c

//1. General  /* int i = 0; */  //2. Line break problem  /* int i = 0; */ int  j = 0 ; /* int i = 0; */ int  j = 0 ; //3. Matching question  /*int i = 0;/*xxxxx*/ //4. Multiline comment Question  /*int i=0;int j = 0;int k = 0;*/ int  k = 0 ; //5. Continuous comment question  /**//**/ //6. Continuous **/problem  /***/ //7.c++ comment question  ///*xxxxxxxxxxxx*/ 

Here we write the method of this small project and we face the same address book before, with three file encapsulation, a test.c file encapsulation test file, a COMMENT_COVERT.C file encapsulation implementation function, a Comment_ The Covert.h file encapsulates the header file.

We can take the enumeration constants here to treat each state.

staticenumSTATE{    NULL_STATE,    C_STATE,    CPP_STATE,    END_STATE};

And then you have to understand that the default state we're going to deal with now is from Null_state, so next we implement the Null_state function, and then we take advantage of the transitions between the various states I've drawn above, and we know that The most we need to judge here is what the first character of the prosecution is, here are three cases: '/', normal character, EOF, so we first have to receive the first character, and then use our finite state machine, when it is '/', we make the next character judgment, whether it is ' * ', if it is, Then we will let the state be c_state, if it is '/' then enter the Cpp_state, next, the other characters, according to the limit to make their own edits.

comment_covert.h
#ifndef __comment_convert_h#define __COMMENT_CONVERT_H#include <stdio.h>#include <stdlib.h>#define READFILENAME "input.c"#define WRITEFILENAME "output.c"Static enumstate{null_state, C_state, Cpp_state, end_state};voidCommentconvert (FILE *fread,FILE*fwrite);voidDo_c_state (FILE *fread,FILE*fwrite);voidDo_cpp_state (FILE *fread,FILE*fwrite);voidDo_null_state (FILE *fread,FILE*fwrite);#endif//!__comment_convert_h
comment_covert.c
#Define _crt_secure_no_warnings 1#include "comment_covert.h"enumstate state = Null_state;voidDo_c_state (file *fread, file *fwrite) {CharFirst =0;CharSecond =0;CharThird =0; First = Fgetc (FRead);Switch(first) { Case' * ': Second = fgetc (FRead);Switch(second) { Case'/': Third = fgetc (FRead);if(Third! =' \ n ') FPUTC (' \ n ', FWrite);if(Third = ='/') {ungetc (third, fRead); state = Null_state; Break;                } FPUTC (third, fWrite); state = Null_state; Break; Case' * ': Third = fgetc (FRead); FPUTC (first, fWrite);if(Third = ='/') {state=null_state; } Break;default: FPUTC (first, fWrite); FPUTC (second, fWrite); Break; } Break; Case' \ n ': FPUTC (first, fWrite); FPUTC ('/', FWrite); FPUTC ('/', FWrite); Break; CaseEof:state = end_state; Break;default: FPUTC (first, fWrite); Break; }}voidDo_cpp_state (file *fread, file *fwrite) {CharFirst =0; First = Fgetc (FRead);Switch(first) { Case ' \ n ': FPUTC (first, fWrite); state = Null_state; Break; CaseEof:state = end_state; Break;default: FPUTC (first, fWrite); Break; }}voidDo_null_state (file *fread, file *fwrite) {CharFirst =0;CharSecond =0; First = Fgetc (FRead);Switch(first) { Case'/': Second = fgetc (FRead);if(Second = =' * ') {FPUTC ('/', FWrite); FPUTC ('/', FWrite);        state = C_state; }Else if(Second = ='/') {FPUTC (first, fWrite);            FPUTC (second, fWrite);        state = Cpp_state; }Else{FPUTC (first, fWrite);        FPUTC (second, fWrite); } Break; CaseEof:state = end_state; Break;default: FPUTC (first, fWrite); Break; }}voidCommentconvert (file *fread, file *fwrite) { while(state!=end_state) {Switch(state) { CaseNull_state:do_null_state (FRead, fWrite); Break; CaseC_state:do_c_state (FRead, fWrite); Break; CaseCpp_state:do_cpp_state (FRead, fWrite); Break;default: Break; }    }}
test.c
#define _CRT_SECURE_NO_WARNINGS 1#include "comment_covert.h"intMain () {FILE*fread= NULL; FILE*fwrite= NULL;printf(* * * * * Note -release conversionstarts * * * * * \ n "); FRead = fopen (Readfilename,"R");if(NULL = = FRead) {Perror ("read of File");Exit(exit_failure); } fWrite = fopen (Writefilename,"W");if(NULL = = FRead)        {fclose (fRead); Perror ("Write of File");Exit(exit_failure); } commentconvert (FRead, fWrite);printf(* * * * * the end of theNote -Release Conversion * * * * * \ n ");    Fclose (FRead); Fclose (FWrite);system("Pause");return 0;}

Finally can complete this small project, the code analysis needs step by step, want to solve the problem, can not be urgent, step by step analysis, even the most arduous one-step debugging.

C Language Small Item-comment conversion

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.