您的位置:首页 > 编程语言 > C语言/C++

[注释转化]C语言注释转换为C++语言注释

2017-07-29 20:08 337 查看
CommentCovnert.h

#ifndef __COMMENT_CONVERT_H__
#define __COMMENT_CONVERT_H__

#include <stdio.h>
#include <stdlib.h>

enum STATE
{
END_STATE,
NUL_STATE,
C_STATE,
CPP_STATE
};

void DoNulState(FILE* pfin, FILE* pfout, enum STATE* s);
void DoCState(FILE* pfin, FILE* pfout, enum STATE* s);
void DoCppState(FILE* pfin, FILE* pfout, enum STATE* s);

#endif //__COMMENT_CONVERT_H__
CommentConvert.c
#include "CommentConvert.h"

void DoNulState(FILE* pfin, FILE* pfout, enum STATE* s)
{
int first = 0;
int second = 0;
first = fgetc(pfin);
switch (first)
{
case '/':
{
second = fgetc(pfin);
switch (second)
{
case '/':
fputc(first, pfout);
fputc(second, pfout);
*s = CPP_STATE;
break;
case '*':
fputc(first, pfout);
fputc('/', pfout);
*s = C_STATE;
break;
default:
fputc(first, pfout);
fputc(second, pfout);
break;
}
}
break;
case EOF:
*s = END_STATE;
break;
default:
fputc(first, pfout);
*s = NUL_STATE;
break;
}
}

void DoCState(FILE* pfin, FILE* pfout, enum STATE* s)
{
int first = 0;
int second = 0;
int third = 0;
first = fgetc(pfin);
switch (first)
{
case '*':
{
second = fgetc(pfin);
switch (second)
{
case '/':
{
third = fgetc(pfin);
if (third == '\n')
{
fputc(third, pfout);
}
else
{
ungetc(third, pfin);
fputc('\n', pfout);
}
*s = NUL_STATE;
}
break;
case '*':
{
ungetc(second, pfin);
fputc(first, pfout);
}
break;
default:
{
fputc(first, pfout);
fputc(second, pfout);
}
break;
}
break;
case EOF:
*s = END_STATE;
break;
case '\n':
fputc(first, pfout);
fputc('/', pfout);
fputc('/', pfout);
break;
default:
fputc(first, pfout);
}
}
}

void DoCppState(FILE* pfin, FILE* pfout, enum STATE* s)
{
int first = 0;
int second = 0;
first = fgetc(pfin);
switch (first)
{
case '\n':
fputc(first, pfout);
*s = NUL_STATE;
break;
case EOF:
*s = END_STATE;
break;
default:
fputc(first, pfout);
break;
}

}
test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"

//注释转化
test()
{
enum STATE state = NUL_STATE;
FILE* fin = NULL;
FILE* fout = NULL;
fin = fopen("input.c", "r");
if (fin == NULL)
{
perror("fopen input");
exit(EXIT_FAILURE);
}
fout = fopen("output.c", "w");
if (fout == NULL)
{
perror("fopen output");
exit(EXIT_FAILURE);
}
while (state != END_STATE)
{
switch (state)
{
case NUL_STATE:
DoNulState(fin, fout, &state);
break;
case C_STATE:
DoCState(fin, fout, &state);
break;
case CPP_STATE:
DoCppState(fin, fout, &state);
break;
default:
break;
}
}
fclose(fin);
fclose(fout);
}
int main()
{
test();
system("pause:");
return 0;
}


input.c

// 1.一般情况

int num = 0;

/* 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.连续注释问题

/*a*//*b*/

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

/***/

// 7.C++注释问题

// /*xxxxxxxxxxxx*/

output.c

// 1.一般情况

int num = 0;

// 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.连续注释问题

//a

//b

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

//*

// 7.C++注释问题

// /*xxxxxxxxxxxx*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: