简介
将C++注释转换成C语言注释
代码块
int num = 0;
/*int i = 0;*/
/*int i = 0;*/int j = 0;
/*int i = 0;*/
int j = 0;
/*int i = 0;/*123456*/
/*
asdas
asdasd
asdasd
sadasd
*/int k = 0;
/*dsfsd*//*gdfg*/
/***/
// /*xxxxxxxxxxx*/
int num = 0;
//int i = 0;
//int i = 0;
int j = 0;
//int i = 0;
int j = 0;
//int i = 0;/*123456
//
//asdas
//asdasd
//asdasd
//sadasd
//
int k = 0;
//dsfsd
//gdfg
//*
// /*xxxxxxxxxxx*/
#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
enum STATE
{
NUL_STATE,
C_STATE,
CPP_STATE,
END_STATE,
};
void DoConvertWork(FILE* pfIn,FILE* pfOut);
void DoNulState(FILE* pfIn,FILE* pfOut);
void DoCState(FILE* pfIn,FILE* pfOut);
void DoCppState(FILE* pfIn,FILE* pfOut);
#endif // __COMMENT_CONVERT_H__
#include "convert.h"
#include <stdio.h>
enum STATE state = NUL_STATE;
void DoConvertWork(FILE* pfIn, FILE* pfOut)
{
while (state != END_STATE)
{
switch (state)
{
case NUL_STATE:
DoNulState(pfIn, pfOut);
break;
case C_STATE:
DoCState(pfIn, pfOut);
break;
case CPP_STATE:
DoCppState(pfIn, pfOut);
break;
default :
break;
}
}
}
void DoNulState(FILE* pfIn,FILE* pfOut)
{
int first = fgetc( pfIn );
int second = 0;
switch (first)
{
case EOF:
{
state = END_STATE;
break;
}
case '/':
{
fputc(first, pfOut);
second = fgetc(pfIn);
if ('*' == second){
fputc('/',pfOut);
state = C_STATE;
}
else if ('/' == second){
fputc(second, pfOut);
state = CPP_STATE;
}
}
break;
default:
fputc(first, pfOut);
break;
}
}
void DoCState(FILE* pfIn,FILE* pfOut)
{
int first = fgetc(pfIn);
switch (first)
{
case'*':
{
int second = fgetc(pfIn);
if('/' != second)
{
fputc(first, pfOut);
ungetc(second, pfIn);
//state = C_STATE;
}
else if('/' == second)
{
int third = fgetc(pfIn);
if ('\n' != third){
fputc('\n', pfOut);
ungetc(third, pfIn);
}
else if('\n' == third){
fputc('\n', pfOut);
}
state = NUL_STATE;
}
}
break;
case '\n':
{
fputc('\n', pfOut);
fputc('/', pfOut);
fputc('/', pfOut);
}
break;
case '/':
fputc(first, pfOut);
{
int second = fgetc(pfIn);
if ('*' == second){
fputc(second,pfOut);
}
}
break;
default:
fputc(first, pfOut);
break;
}
}
void DoCppState(FILE* pfIn,FILE* pfOut)
{
int first = fgetc(pfIn);
switch (first)
{
case EOF:
state = END_STATE;
break;
case '\n':
state = NUL_STATE;
fputc(first, pfOut);
break;
default:
fputc(first, pfOut);
break;
}
}
#include "convert.h"
#include <stdlib.h>
void test()
{
FILE* pfIn = fopen("input.c", "r");
FILE* pfOut = fopen("output.c", "w+");
if (NULL == pfIn){
perror("input open error");
exit( EXIT_FAILURE );
}
if (NULL == pfOut)
{
fclose(pfIn);
perror("output open error");
exit( EXIT_FAILURE );
}
DoConvertWork(pfIn, pfOut);
fclose(pfIn);
fclose(pfOut);
}
int main()
{
test();
system("pause");
return 0;
}
还未更新完