注釋轉化(C注釋轉為C 注釋)

2021-08-22 10:11:59 字數 2904 閱讀 8360

假如現在有乙個.c的檔案,它裡面有c語言的注釋(/…/),c++的注釋(//),當然也有正常的**了,現在我們想要把裡面的注釋統一一下,將c語言注釋全部轉換為c++的注釋,該怎麼做呢?

思路:

我們可以這麼想,它裡面無非有四種型別:

1.正常**

2.c注釋

3.c++注釋

4.檔案結束標誌eof

我們要向把c注釋全部轉化成c++注釋,當然要分析一下這四種型別之間的過渡關係了,**如下:

經過我們分析,如上圖,

1.正常**遇到/*之後會變成c注釋,遇到//之後會變成c++**,而遇到eof會結束;

2.c**遇到**/會變成正常**,不可能會遇到eof;

3.c++**遇到『\n』會變成正常**,遇到eof會就會結束

例如:

測試用例如下:

// 1.一般情況

int num = 0;

/* int i = 0; */

// 2.換行問題

/* int i = 0; */

int j = 0;

/* int i = 0; */

int j = 0;

// 3.匹配問題

/*int i = 0;/****xx*/

// 4.多行注釋問題

/*int i=0;

int j = 0;

int k = 0;

*/int k = 0;

// 5.連續注釋問題

/**//**/

// 6.連續的**/問題

/***/

// 7.c++注釋問題

// /**************/

#define _crt_secure_no_warnings 1

#include"commentconvert.h"

void test()

pfwrite = fopen("output.c", "w");

if (pfwrite == null)

commentcovert(pfread, pfwrite);//注釋轉化

fclose(pfread);

pfread = null;

fclose(pfwrite);

pfwrite = null;

}int main()

#define _crt_secure_no_warnings 1

#include"commentconvert.h"

void donulstate(file *pfread, file *pfwrite, state *ps)

break;

case

'/':

break;

case eof:

default:

fputc(first, pfwrite);//其他情況照常輸出

fputc(second, pfwrite);

break;}}

break;

case eof:

*ps = end_state;

break;

default:

fputc(first, pfwrite);

*ps = nul_state;

break;

}}void docstate(file *pfread, file *pfwrite, state *ps)

else

*ps = nul_state;//狀態置為正常**

}break;

case

'*':

break;

default:

break;}}

break;

case

'\n':

break;

default:

fputc(first, pfwrite);

break;

}}void docppstate(file *pfread, file *pfwrite, state *ps)

}void commentcovert(file *pfread, file *pfwrite)

break;

case c_state:

break;

case cpp_state:

break;

case end_state:

break;}}

}

#ifndef __commentconvert_h__

#define __commentconvert_h__

#include

#include

typedef enum state

state;

void donulstate(file *pfread, file *pfwrite, state *ps);

void docstate(file *pfread, file *pfwrite, state *ps);

void docppstate(file *pfread, file *pfwrite, state *ps);

void commentcovert(file *pfread, file *pfwrite);

#endif // __commentconvert_h__

結果如下:

注釋轉換 c注釋 c 注釋

本專案基於乙個狀態機的思想,每次處理完成之後通過狀態的裝換繼續處理後邊的內容。另外,利用各類的檔案操作函式,實現將將c風格的注釋裝換成c 風格的注釋的乙個簡單功能。大概如下圖 什麼是狀態機?在本專案中,會用到四種狀態,既空狀態 正常 區 c注釋狀態 c風格注釋區 c 注釋狀態 c 風格注釋區 檔案結...

注釋轉換C 注釋》

注意 中所用到的fopen,fwrite,fread等檔案操作函式在通訊錄檔案流部落格中做了介 紹 ungetc 函式是將從檔案中讀取的乙個字元還回到緩衝區,即檔案中去 分析思路圖 分析思路圖 自定義標頭檔案部分 ifndef comment convert h define comment con...

注釋轉換(C注釋轉換為c 注釋)

對於注釋轉換首先給出我的測試圖 由圖可以看出將左邊的c語言注釋轉換為右邊c 注釋就是注釋轉換 首先說明一下轉換思想方法 1.建立兩個檔案input.c和output.c,input.c裡面用來讀取c語言的注釋,output.c裡面儲存轉換成為c 的注釋,中間的轉換過程就是 完成,當然檔案名字和作用自...