c/c++注释转换函数

来源:互联网 发布:wireshark过滤端口 编辑:程序博客网 时间:2024/06/05 08:10

题目:要求将下列的注释全部都转换成c++中的的注释,也就是//注释

// 1.一般情况
/* int i = 0; */

// 2.换行问题
/* int i = 0; */int j = 0;
/* int i = 0; */
int j = 0;
// 3.匹配问题
/*int i = 0;/*xxxxx*/

// 4.多行注释问题
/*
int i=0;
int j = 0;
int k = 0;
*/int k = 0;

// 5.连续注释问题
/**//**/

// 6.连续的**/问题
/***/

// 7.C++注释问题
// /*xxxxxxxxxxxx*/

结果:

// 1.一般情况
// int i = 0;

// 2.换行问题
// int i = 0;
int j = 0;
// int i = 0;
int j = 0;
// 3.匹配问题
//int i = 0;/*xxxxx

// 4.多行注释问题
//
//int i=0;
//int j = 0;
//int k = 0;
//
int k = 0;

// 5.连续注释问题
//
//

// 6.连续的**/问题
//*

// 7.C++注释问题
// /*xxxxxxxxxxxx*/


代码如下:

(1).头文件

#pragma  once #define  _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include<assert.h>enum State{NUL_STATE,C_STATE,CPP_STATE,END_STATE};void CommentConvert();void DoCommentConvert(FILE *Input, FILE * Output );void DoNulComment(FILE *Input, FILE * Output ,enum State * state);void DoCComment(FILE *Input, FILE * Output ,enum State * state);void DoCppComment(FILE *Input, FILE * Output ,enum State * state);
(2).函数实现:

#include"CommentConverce.h"void CommentConvert(){FILE *pfin = NULL;FILE *pfout = NULL;pfin = fopen("input.c","r");if (NULL == pfin){perror("input.c");exit(EXIT_FAILURE);}pfout = fopen("output.c","w");if (NULL == pfin){perror("output.c");fclose(pfin);exit(EXIT_FAILURE);}DoCommentConvert(pfin, pfout);printf("转换完成!\n");fclose(pfin);fclose(pfout);}void DoCommentConvert(FILE *Input, FILE * Output ){enum State state = NUL_STATE;while (END_STATE != state ){switch (state){case NUL_STATE:DoNulComment(Input, Output, &state);break;case C_STATE:DoCComment(Input, Output, &state);break;case CPP_STATE:DoCppComment(Input, Output, &state);break;default:break;}}}void DoNulComment(FILE *Input, FILE * Output ,enum State * state){int first = 0;first = fgetc(Input);switch (first){case '/':{int second = 0;second = fgetc(Input);switch (second){case '*':*state = C_STATE;fputc('/', Output);fputc('/', Output);break;case '/':*state = CPP_STATE;fputc('/', Output);fputc('/', Output);break;default:fputc(second, Output);break;}}break;case EOF:*state = END_STATE;break;default:fputc(first, Output);break;}}void DoCppComment(FILE *Input, FILE * Output ,enum State * state){int first = 0;first = fgetc(Input);switch (first){case '\n':*state = NUL_STATE;fputc('\n', Output);break;case EOF:*state = END_STATE;default:fputc(first, Output);break;}}void DoCComment(FILE *Input, FILE * Output ,enum State * state){int first = 0;first = fgetc(Input);switch (first){case'\n':fputc(first, Output);fputc('/',Output);fputc('/',Output);break;case '*':{int second = 0;second = fgetc(Input);switch (second){case '/'://fputc('\n' , Output);{int third = 0;third = fgetc(Input);if ('\n' != third){fputc('\n',Output);}ungetc(third,Input);*state = NUL_STATE;break;}case '*':ungetc(second, Input);fputc(first,Output);break;default:fputc(first, Output);fputc(second, Output);break;}}break;  //我擦,少了个break,搞死人呢。default:fputc(first, Output);break;}}
(3).主函数

#include"CommentConverce.h"int main(){CommentConvert();system("pause");return 0;}


结果:






0 0
原创粉丝点击