标签:
StdAfx.h文件实现各种预定义.
#ifndef _STDAFX_H #define _STDAFX_H #define UL unsigned long #endif
constd编写相应的实现方法.
#include<stdio.h> #include<stdlib.h> #include"StdAfx.h" extern "C" void converter(FILE *input,FILE *output); //表示以C的环境访问 typedef enum STATE_ENUM //枚举类型的使用,存放相似的量 { NO_COMMENT_STATE, //未知情况 C_COMMENT_STATE, //C语言注释情况 CPP_COMMENT_STATE, //C++注释情况 STR_STATE, //字符串中出现注释可忽略的情况 END_STATE //出现EOF结束的情况 }STATE_ENUM; typedef struct STATE_MACHINE { FILE *inputfile; 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) { }
main.c文件通过主函数实现注释转换.
#include<stdio.h> #include<stdlib.h> extern "C" void converter(FILE *input,FILE *output); int main() { 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; }
如此便实现了简单的注释转换。以下是注释转换后的实现效果图。
Input.txt中为:
//每个区由若干个内存块组成
//每个区由若干个内存块组成,//每个块是4096个字节
//int i = 0;*/
//*//*int i = 0;
// /**/int i = 0;
-------------------------------------------------------------
/* int i = 0;
*//*
*/
/* int i = 0;
//*/int j = 0;
/*
//每个区由若干个内存块组成,每个块是4096个字节
//每个块的第0个整数指向下个区
//所以是单链表结构
//所以每个区只有4092个字节存放真正的数据
*/
/* int i = 0;*//*int j = 0;
*/
/*
*//*
*/int i =0;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 5
"abcdefghijklmn~~~~!!!!!!!!"
Output.txt中输出为:
/*每个区由若干个内存块组成*/
/*每个区由若干个内存块组成, 每个块是4096个字节*/
/*int i = 0; */
/* int i = 0; */
/* int i = 0; */
-------------------------------------------------------------
/* int i = 0;
*//*
*/
/* int i = 0;
*/int j = 0;
/*
每个区由若干个内存块组成,每个块是4096个字节
每个块的第0个整数指向下个区
所以是单链表结构
所以每个区只有4092个字节存放真正的数据
*/
/* int i = 0;*//*int j = 0;
*/
/*
*//*
*/int i =0;
/* 5*/
"abcdefghijklmn~~~~!!!!!!!!"
标签:
原文地址:http://www.cnblogs.com/waiting-for/p/4230897.html