Small project-Simple annotation Conversion

Source: Internet
Author: User

The StdAfx.h file implements a variety of predefined.
#ifndef _stdafx_h #define _stdafx_h#define UL unsigned long#endif

CONSTD write the corresponding implementation method.
#include <stdio.h> #include <stdlib.h> #include "StdAfx.h" extern "C" void converter (FILE *input,file *output    ); Represents the use of a typedef enum STATE_ENUM//enumeration type in the context of C, storing a similar amount of {no_comment_state,//Unknown case c_comment_state,//C language annotation situation cpp_comment_s TATE,//c++ annotation case str_state,//The case where comments are omitted in the string end_state//occurrence of EOF end}state_enum;typedef struct State_machine{file *input File FILE *outputfile; UL Ulstate;} State_machine; State_machine g_state;////////////////////////////////////////////void eventpro (char ch); void EventProAtNo (char ch) ; void Eventproatc (char ch); void Eventproatcpp (char ch); void Eventproatstr (char ch);///////////////////////////////// void Converter (FILE *input,file *output) {char ch;if ((NULL = = input) | | (NULL = = output)) {perror ("Input or output error."); Exit (-1);} G_state.inputfile = Input;g_state.outputfile = Output;g_state.ulstate = No_comment_state;while (g_state.ulstate! = END_ STATE) {ch = fgetc (g_state.inputfile); Eventpro (ch);}} void Eventpro (char ch) {switch (g_state.uLstate) {case No_comment_state:eventproatno (CH); break;case C_COMMENT_STATE:EVENTPROATC (CH); Break;case CPP_COMMENT_ State:eventproatcpp (CH); break;case str_state:eventproatstr (ch); break;default:break;}} void Eventproatno (char ch) {char nextch;switch (CH) {case '/': Nextch = fgetc (g_state.inputfile); if (Nextch = = '/')//cpp/*{ FPUTC ('/', g_state.outputfile); FPUTC (' * ', g_state.outputfile); g_state.ulstate = C_comment_state;} else if (Nextch = = ' * ')//C{FPUTC ('/', g_state.outputfile); FPUTC (' * ', g_state.outputfile); g_state.ulstate = Cpp_comment _state;} ELSE{FPUTC ('/', g_state.outputfile); FPUTC (nextch,g_state.outputfile);} Break;case EOF:g_state.ulstate = END_STATE;DEFAULT:FPUTC (ch,g_state.outputfile); break;}} void Eventproatc (char ch) {char nextch;switch (CH) {case ' \ n ': FPUTC (' * ', g_state.outputfile); FPUTC ('/', g_ State.outputfile); Fputc (' \ n ', g_state.outputfile); g_state.ulstate = No_comment_state;break;case EOF:fputc (' * ', g_ State.outputfile); FPUTC ('/', g_state.outputfile); g_state.ulstate = End_state;break;case '/': NEXtch = fgetc (g_state.inputfile); if (('/' = = Nextch) | | (' * ' = = Nextch)) {FPUTC (', g_state.outputfile); FPUTC (", g_state.outputfile);} Break;case ' * ': Nextch = fgetc (G_state.inputfile), if ('/' = = Nextch) {FPUTC (', g_state.outputfile); FPUTC (', g_ State.outputfile);} BREAK;DEFAULT:FPUTC (ch,g_state.outputfile); break;}} void Eventproatcpp (char ch) {char nextch;switch (CH) {case ' * ': Nextch = fgetc (g_state.inputfile); if ('/' = = Nextch) {FPUTC ( ' * ', g_state.outputfile); FPUTC ('/', g_state.outputfile); g_state.ulstate = No_comment_state;} ELSE{FPUTC (' * ', g_state.outputfile); FPUTC (nextch,g_state.outputfile);} Break;case '/': Nextch = fgetc (g_state.inputfile); if (('/' = = Nextch) | | (' * ' = = Nextch)) {FPUTC (', g_state.outputfile); FPUTC (", g_state.outputfile);} BREAK;DEFAULT:FPUTC (ch,g_state.outputfile); break;}} void Eventproatstr (char ch) {}

  



The main.c file implements annotation conversions through the main function.

#include <stdio.h>#include<stdlib.h>extern "C" voidConverter (FILE *input,file *output);intMain () {FILE*input; FILE*output; Input= fopen ("Input.txt","R"); Output= fopen ("Output.txt","W"); if(NULL = =input) {Perror ("input error."); Exit (-1); } if(NULL = =output) {Perror ("output error."); Exit (-1); }///////////////////////////////////////////////Converter (input,output);/////////////////////////////////////////////// if(fclose (input)! =0) {perror ("fclose input error."); Exit (-1); } if(fclose (output)! =0) {perror ("fclose output error."); Exit (-1); } return 0;}

This allows for a simple annotation conversion. The following is a comment-converted implementation.

In the Input.txt:

Each zone consists of several memory blocks

Each zone consists of a number of memory blocks,//each block is 4,096 bytes

int i = 0;*/

*//*int i = 0;

/**/int i = 0;
-------------------------------------------------------------
/* int i = 0;
*//*
*/

/* int i = 0;
*/int j = 0;

/*
Each zone consists of a number of memory blocks, each of which is 4,096 bytes
The No. 0 integer of each block points to the next area
So it's a single-linked list structure.
So each zone has only 4,092 bytes to hold the real data.
*/

/* int i = 0;*//*int j = 0;
*/

/*
*//*
*/int i = 0;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// 5

"Abcdefghijklmn~~~~!!!!!!!!"

The output in Output.txt is:

/* Each zone consists of several memory blocks */

/* Each zone consists of several memory blocks, each of which is 4,096 bytes per block */

/*int i = 0; */

/* int i = 0; */

/* int i = 0; */
-------------------------------------------------------------
/* int i = 0;
*//*
*/

/* int i = 0;
*/int j = 0;

/*
Each zone consists of a number of memory blocks, each of which is 4,096 bytes
The No. 0 integer of each block points to the next area
So it's a single-linked list structure.
So each zone has only 4,092 bytes to hold the real data.
*/

/* int i = 0;*//*int j = 0;
*/

/*
*//*
*/int i = 0;

/* 5*/

"Abcdefghijklmn~~~~!!!!!!!!"

Small project-Simple annotation 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.