C注释 转换为 C++注释
?//?1.一般情況
/*?int?i?=?0;?*/
//?2.換行問(wèn)題
/*?int?i?=?0;?*/int m =?0;
/*?int?i?=?0;?*/
int?j?=?0;
//?3.匹配問(wèn)題
/*int?i?=?0;/*xxxxx*/
//?4.多行注釋問(wèn)題
/*
int?i=0; ?
int?j?=?0;
int?k?=?0;
*/int?k?=?0;
//?5.連續(xù)注釋問(wèn)題
/**//**/
//?6.連續(xù)的**/問(wèn)題
/***/
//?7.C++注釋問(wèn)題
//?/*xxxxxxxxxxxx*/
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
typedef enum STATE
{
C_BEGIN, ? ? ?//在c注釋段內(nèi)
C_END, ? ? ? //注釋結(jié)束
C_NO_MATCH, //注釋不匹配
C_SUCCESS ?//注釋轉(zhuǎn)換成功
}STATE;
int flag = 0;//解決C++注釋問(wèn)題 ?當(dāng) flag == 1 時(shí)表示出現(xiàn)C++問(wèn)題
STATE AnnotationCovert(FILE* fIn, FILE* fOut)
{
assert(fIn);
assert(fOut);
char first, second;
STATE ?sta= C_END;
do
{
first = fgetc(fIn);
switch (first)
{
case '/':// /*xxxxxxxxxxxx*/
{
second = fgetc(fIn);
if (second == '*'&& sta == C_END)//>>>>>解決一般問(wèn)題
{
fputc('/',fOut);
fputc('/', fOut);
sta = C_BEGIN;
}
else if (second == '/')//解決C++注釋問(wèn)題
{
fputc('/', fOut);
fputc('/', fOut);
sta = C_BEGIN;
flag = 1;
}
else//>>>>>>>>解決匹配問(wèn)題
{
fputc(first, fOut);
fputc(second, fOut);
}
break;
case '*':
{
second = fgetc(fIn);
if (second == '/' && sta == C_BEGIN && flag == 0)
{
fputc('\n',fOut);//>>>>>解決換行問(wèn)題
sta = C_END;
}
else if (second == '/' && sta == C_BEGIN && flag == 1)//解決C++注釋問(wèn)題
{
fputc(first, fOut);
fputc(second, fOut);
sta = C_END;
flag = 0;
}
else
{
fputc(first, fOut);
fseek(fIn, -1, SEEK_CUR);//>>>>>>解決連續(xù)的**/問(wèn)題
}
break;
}
case '\n': ? ? //>>>>>>>>>>>>>解決多行注釋問(wèn)題
{
?fputc(first, fOut);
?if (sta == C_BEGIN)
?{
?fputc('/', fOut);
?fputc('/', fOut);
?}
?break;
}
default:
{
?? fputc(first, fOut);
?? break;
}
? ? ? ? }
}
} while (first != EOF);
if (sta == C_END)
{
return C_SUCCESS;
}
else
{
return C_NO_MATCH;
}
}
void TestConvert()
{
FILE* fIn = fopen("input.c", "r");
if (fIn == NULL)
{
printf("file open fail. ? errno:%d\n", errno);
return;
}
FILE* fOut = fopen("output.c", "w");
if (fOut == NULL)
{
fclose(fIn);
printf("file open fail. ? errno:%d\n", errno);
return;
}
STATE ret = AnnotationCovert(fIn, fOut);
if (ret == C_SUCCESS)
{
cout << "注釋轉(zhuǎn)換成功!" << endl;
}
else if (ret == C_NO_MATCH)
{
cout << "注釋轉(zhuǎn)換失敗!" << endl;
}
else
{
cout << "未知錯(cuò)誤!請(qǐng)仔細(xì)核查!" << endl;
}
}
int main()
{
TestConvert();
system("pause");
return 0;
}
原注釋:
轉(zhuǎn)換后的注釋:
上一封?下一封
?返回
轉(zhuǎn)載于:https://blog.51cto.com/10704527/1719496
總結(jié)
以上是生活随笔為你收集整理的C注释 转换为 C++注释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ECC 算法
- 下一篇: windows进入mysql