註釋轉換
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;
標頭檔案:
#ifndef __CONVERT__
#define __CONVERT__
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define INPUT_FILE "input.c"
#define OUTPUT_FILE "output.c"
typedef enum status{
NORMAL_STATUS,
C_Status,
CPP_Status,
END_Status,
}Status;
void convertBegin();
static void convertStatusMachine(FILE * in,FILE * out);
void DoNormal(FILE * in, FILE * out);
void DoCSatus(FILE * in, FILE* out);
void DoCppSatus(FILE * in, FILE* out);
#endif // !__CONVERT__
函式部分:
#include"convert.h"
Status gStatus=NORMAL_STATUS;
void DoNormal(FILE * in, FILE * out)//正常情況下注釋轉換
{
assert(in);
assert(out);
int first = fgetc(in);
int second = 0;
switch (first)
{
case '/':
second = fgetc(in);
if (second == '*')//如果第二個是*,那麼是C語言註釋,返回 / /
{
fputc('/', out);
fputc('/', out);
gStatus = C_Status;
}
else if (second == '/')//如果是/,那麼是cpp註釋,直接返回兩個變數的內容
{
fputc(first, out);
fputc(second, out);
gStatus = CPP_Status;
}
else//否則,不是註釋
{
fputc(first, out);
fputc(second, out);
}
break;
case EOF:
gStatus = END_Status;
default:
fputc(first, out);//
break;
}
}
/*
xxxxxxxxxxx
*/
void DoCSatus(FILE * in, FILE* out)//C語言型別的註釋轉換
{
assert(in);
assert(out);
int first = fgetc(in);
int second = 0;
switch (first)
{
case '*':
second = fgetc(in);
if (second == '/')//當第一個為*,第二個為/,第三個是‘\n’,那麼是C語言註釋
{
int third = fgetc(in);
if (third == '\n')
{
fputc(third, out);
}
else
{
ungetc(third, in);
fputc("\n", out);
}
gStatus = NORMAL_STATUS;
}
else
{
fputc(first, out);
ungetc(second, in);
}
break;
case '\n'://如果是‘\n’,那麼是cpp註釋,直接返回 / / 和第一個字元
fputc(first, out);
fputc('/', out);
fputc('/', out);
break;
case EOF://結束轉換
gStatus = END_Status;
break;
default:
fputc(first, out);
break;
}
}
//xxxxxxxx
void DoCppSatus(FILE * in, FILE* out)//p型別註釋轉換
{
assert(in);
assert(out);
int first = fgetc(in);
switch (first)
{
case '\n'://當第一個字元為'\n',那麼,為正常轉換
gStatus = NORMAL_STATUS;
fputc(first, out);
break;
case EOF://到文字結束符,結束轉換
gStatus = END_Status;
break;
default:
fputc(first, out);
break;
}
}
static void convertStatusMachine(FILE * in, FILE * out)
{
assert(in);
assert(out);
while (gStatus != END_Status)
{
switch (gStatus)
{
case NORMAL_STATUS:
DoNormal(in, out);
break;
case C_Status:
DoCSatus(in, out);
break;
case CPP_Status:
DoCppSatus(in, out);
break;
case END_Status:
break;
default:
break;
}
}
}
void convertBegin()
{
FILE * in = fopen(INPUT_FILE, "r");
if (NULL == in)
{
perror("fopen");
exit(1);
}
FILE * out = fopen(OUTPUT_FILE, "w");
if (NULL == out)
{
perror("fopen");
exit(2);
}
convertStatusMachine(in, out);
fclose(in);
fclose(out);
}
主函式部分:
#include"convert.h"
int main()
{
convertBegin();
printf("convert done……\n");
system("pause");
return 0;
}
相關文章
- Java註釋,識別符號,資料型別,型別轉換Java符號資料型別
- 在JBuilder5中增加註釋與反註釋工具 (轉)UI
- PDF閱讀器不只閱讀註釋,還有轉換與編輯
- 中文註釋的php.ini(轉)PHP
- 【轉載】Eclipse 的快捷鍵以及文件註釋、多行註釋的快捷鍵Eclipse
- JavaScript註釋:單行註釋和多行註釋詳解JavaScript
- 註釋與反註釋Comment/Uncomment selected code in Visual C++ (轉)C++
- Python註釋之TODO註釋Python
- 註釋
- MySQL追加註釋或者大量修改註釋MySql
- 「轉」Laravel 依賴注入原理(詳細註釋)Laravel依賴注入
- Bootstrap的Model原始碼詳細註釋 (轉)boot原始碼
- C++ 重解釋型別轉換C++型別
- SCSS 註釋CSS
- JavaScript 註釋JavaScript
- React註釋React
- Java註釋Java
- HTML 註釋HTML
- 符號(註釋符+轉義符+接續符)符號
- RFC1867(請求註釋)規範 (轉)
- 帶註釋手動注入指令碼命令整理(轉)指令碼
- 註解和註釋區別
- Python如何新增註釋?註釋有幾種?Python
- js基礎01--js的簡介、使用、語法、輸出、註釋、變數、型別轉換、表JS變數型別
- C# 註釋C#
- 字串及註釋字串
- mysql註釋符MySql
- oracle 新增註釋Oracle
- 3 JS註釋JS
- Python註釋Python
- IDEA自定義類註釋和方法註釋(自定義groovyScript方法實現多行引數註釋)Idea
- 建立更易理解的動態註釋使用者介面 (轉)
- iOS 註釋方法大全 程式碼塊加快捷鍵註釋iOS
- PHP 註釋標記,PHP
- golang 註釋規範Golang
- CSS 註釋規範CSS
- JavaScript 註釋規範JavaScript
- IDEA新增註釋Idea