C Language Project-comment conversion (c--c++)

Source: Internet
Author: User

Title Requirements:

To convert all C-style annotations to C + + style annotations, as shown in, you need to convert the input.c file to the output.c file as shown in the comment translator.

Here is the test result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/81/F2/wKioL1dGTGSh7TG4AAB6kejxscw601.png "title=" Test Results " alt= "Wkiol1dgtgsh7tg4aab6kejxscw601.png"/>

First of all, we will analyze this topic, in doing this project, we need to introduce the concept of finite state machine. Let's start by understanding what a finite state machine is.

The FSM of finite state machine is a common method in software, and he divides the complicated control logic into a finite stable state, which is processed in each state.

Let's draw a state diagram:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/81/F2/wKioL1dGTfShqABLAABKmzJS4fA849.png "title=" state decomposition " alt= "Wkiol1dgtfshqablaabkmzjs4fa849.png"/> So we split the project into these states.

Nul_state, C_state, Cpp_state, emd_state

Now let's implement this project.

"CommentConvert.h"

 #ifndef   __comment_convert_h__#define  __comment_ convert_h__#include<stdio.h> #include <stdlib.h> #define  INPUTFILENAME  "input.c" #define  OUTPUTFILENAME  "Output.c" enum state{nul_state,c_state,cpp_state,emd_state};void  Commentconvert (File *pread,file *pwrite); void do_nul_state (File *pread,file *pwrite); Void do_c_state (File *pread,file *pwrite); Void do_cpp_state (FILE *pRead,FILE * Pwrite); #endif  //__comment_convert_h__ 

< Span style= "MARGIN:0PX;PADDING:0PX;LINE-HEIGHT:NORMAL;FONT-SIZE:24PX;" >

#include "CommentConvert.h" Enum state state;void commentconvert (file *pread,file * Pwrite) {while (state!=emd_state) {switch (state) {case nul_state:do_nul_state (pread,pwrite); break;case c _state:do_c_state (Pread,pwrite); break;case cpp_state:do_cpp_state (pread,pwrite); Break;case EMD_STATE : Break;}}} Void do_nul_state (file *pread,file *pwrite) {int first=0;int second=0;first =  fgetc (pRead), switch (first) {case  '/': {second=fgetc (PRead), if (second== ' * ') {FPUTC ('/', pwrite); FPUTC ('/ ', pwrite); state=c_state;} Else if (second== '/') {FPUTC (first,pwrite); FPUTC (second,pwrite); state=cpp_state;} ELSE{FPUTC (First,pwrite); FPUTC (Second,pwrite);}} BREAK;CASE&NBSP;EOF:STATE=EMD_STATE;BREAK;DEFAULT:FPUTC (first,pwrite); break;}} Void do_c_state (File *pread,file *pwrite) {int first=0;int second=0;int third=0; First = fgetc (pRead), switch (first) {case  ' * ': &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp {second=fgetc (pRead); switch (second) {case  '/': Third=fgetc (PRead), if (third!= ' \ n ') {FPUTC (' \ n ', pwrite);// FPUTC ('/', pwrite);//FPUTC ('/', pwrite); state=cpp_state;}     if (third== '/') {ungetc (third,pread); state=nul_state;break;} ELSE{FPUTC (third,pwrite); state=nul_state;break;} Case ' * ': third=fgetc (PRead), FPUTC (First,pwrite), if (third== '/') {state=nul_state;} BREAK;DEFAULT:FPUTC (First,pwrite); FPUTC (second,pwrite); break;} Break;case ' \ n ': FPUTC (First,pwrite); FPUTC ('/', pwrite); FPUTC ('/', pwrite);break;case eof:state=emd_state; BREAK;DEFAULT:FPUTC (first,pwrite); break;}}} Void do_cpp_state (file *pread,file *pwrite) {int first=0;first = fgetc (pRead); Switch (first) {case  ' \ n ': {FPUTC (first,pwrite);//FPUTC ('/', pwrite);//FPUTC ('/', pwrite); state=nul_state;} BREAK;CASE&NBSP;EOF:STATE=EMD_STATE;BREAK;&NBSP;&NBSP;&NBSP;&NBSP;DEFAULT:FPUTC (first,pWrite); break;}}

"TEST.c"

#include "CommentConvert.h" int main () {FILE *pread = NULL; FILE *pwrite = null;printf ("Comment Conversions Start:");p Read=fopen (InputFileName, "R"), if (NULL ==pread) {perror ("Open file for read\n"); Exit (exit_failure);} Pwrite=fopen (OutputFileName, "w"), if (NULL ==pwrite) {fclose (pRead);p error ("Open file for write\n"); Exit (exit_failure );} Commentconvert (pread,pwrite);p rintf ("Note conversion Complete:"); return 0;}

This completes the annotation Transformation project.


This article is from the "10917138" blog, please be sure to keep this source http://10927138.blog.51cto.com/10917138/1783330

C Language Project-comment conversion (c--c++)

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.