注释转换器

简介

将C++注释转换成C语言注释

代码块

input.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*/

output.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*/

convert.h

#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__

convert.c

#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;
}
}

test.c

#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;
}

还未更新完

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值