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

【C语言项目】注释转换

2016-05-30 17:17 381 查看
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef enum ConvertState
{
SUCCESS,
FILE_ERROR,
NO_MATCH,
}ConvertState;

typedef enum State
{
C_BEGIN,
C_END,
CPP_BEGIN,
CPP_END,

}State;

ConvertState Convert(FILE* fIn, FILE* fOut)
{
ConvertState ret = SUCCESS;

char first, second;
State tag = C_END;
assert(fIn);
assert(fOut);
do
{
first = fgetc(fIn);
switch (first)
{
case '/':
second = fgetc(fIn);
if (second == '*')
{
if (tag == C_END)
{
fputc('/', fOut);
fputc('/', fOut);

tag = C_BEGIN;
}
else
{
fputc('/', fOut);
fputc('*', fOut);
}
}
else if (second == '/')
{
char next;
fputc('/', fOut);
fputc('/', fOut);

do
{
next = fgetc(fIn);
fputc(next, fOut);

if (next == EOF)
{
return ret;
}
} while (next != '\n');
}
else
{
fputc(first, fOut);
fputc(second, fOut);

}
break;
case '\n'://多行注释问题
fputc('\n', fOut);

if (tag == C_BEGIN)
{
fputc('/', fOut);
fputc('/', fOut);
}
break;
case '*':
second = fgetc(fIn);
if (second == '/')
{
//换行问题
char next = fgetc(fIn);

//连续注释问题
if (next == '/')
{
fputc('\n', fOut);
fseek(fIn, -1, SEEK_CUR);
}
else if (next != '\n' && next != EOF)
{
fputc('\n', fOut);
fputc(next, fOut);
}
else
{
fputc('\n', fOut);
}

tag = C_END;
}
else if (second == '*')
{
fputc(first, fOut);
fseek(fIn, -1, SEEK_CUR);
}
else
{
fputc(first, fOut);
fputc(second, fOut);
}
break;
default:
if (first != EOF)
fputc(first, fOut);

break;
}
} while (first != EOF);
if (tag != C_END)
{
ret = NO_MATCH;
}
return ret;
}

ConvertState AnnotationConvert(const char* inputFile, const char* outputFile)
{
ConvertState ret;
FILE* fIn, *fOut;
fIn = fopen(inputFile, 'r');
if (inputFile == NULL)
{
printf("打开文件%s失败, errno: %d\n", inputFile, errno);
return FILE_ERROR;
}

fOut = fopen(outputFile, 'w');
if (outputFile == NULL)
{
fclose(fIn);
printf("打开文件%s失败,errno: %d\n", outputFile, errno);
return FILE_ERROR;
}

ret = convert(fIn,fOut);
fclose(fIn);
fclose(fOut);

return ret;
}
int main()
{

}

注释转换--

C语言的注释 -> C++的注释
C语言的注释 /* xxxxx */
C++的注释 //xxxxx

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

// 8.C注释本身不匹配
/* int i = 0;

本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1743806
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐