注釋轉換器

2021-07-11 12:53:56 字數 3917 閱讀 9589

讓我們來實現乙個注釋轉換器。它可以將擁有c風格(/* code */)或c++風格(// code)的注釋的**轉換為c++風格。

剛開始看到這個題目是有點頭暈的,因為各種注釋的混合將使情況變得非常複雜,比如:

// 1.一般情況

/* 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++注釋問題

// /*******************xx*/

這裡只列舉了幾種情況,當然還有更多。所以想要單純的通過if-else語句判斷是很麻煩很麻煩的。由此,我們就想通過某種方法將這麼多的複雜情況簡化為幾種狀態,在這幾種為數不多的狀態裡區分邏輯那就一目了然了。所以有限狀態機來了。

有限狀態機,(英語:finite-state machine, fsm),又稱有限狀態自動機,簡稱狀態機,是表示有限個狀態以及在這些狀態之間的轉移和動作等行為的數學模型。

由這個思路,我們可以畫出如下模型(當然只是隨便畫畫,並不嚴謹):

按注釋風格將**分為幾種狀態:

非注釋**稱為無狀態,c風格注釋稱為c轉態,c++風格注釋稱為cpp狀態,檔案結束稱為end狀態。

箭頭旁邊的標註代表狀態轉換的條件,例如無狀態指向c轉態的箭頭,意義為當在無狀態下,遇到 '/*'則切換為c狀態。

理清了這些邏輯,我們便可以很好的在各個轉態之間切換了。下面貼上我寫的**(input.c作為測試**源,output.c作為輸出檔案,都放在當前目錄下):

標頭檔案annotation_converter.h

#ifndef __annotation_concerter__

#define __annotation_concerter__

#include #include #define input_file_name "input.c"

#define output_file_name "output.c"

typedef enum state

state_type;

void converter(void);

//核心轉換函式

void converter_core(file *read, file *write);

//無狀態處理

void converter_null(file *read, file *write, state_type *pstate);

//c狀態處理

void converter_c(file *read, file *write, state_type *pstate);

//cpp狀態處理

void converter_cpp(file *read, file *write, state_type *pstate);

#endif

具體實現檔案annotation_converter.c

#define _crt_secure_no_warnings

#include "annotation_converter.h"

#include void converter_null(file *read, file *write, state_type *pstate)

} break;

case eof:

*pstate = end_state;

break;

default:

fputc(first, write);

break;

} }}void converter_c(file *read, file *write, state_type *pstate)

else if ('*' == second)

else

}break;

case '\n':

fputc(first, write);

fputc('/', write);

fputc('/', write);

break;

case eof:

*pstate = end_state;

break;

default:

fputc(first, write);

break;

} }}void converter_cpp(file *read, file *write, state_type *pstate) }}

void converter_core(file *read, file *write) }}

void converter()

write = fopen(output_file_name, "w");

if (null == write)

converter_core(read, write);

fclose(read);

fclose(write);

}

測試檔案test.c

#include "annotation_converter.h"

int main()

input.c

// 1.一般情況

/* 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++注釋問題

// /*******************xx*/

output.c初始為空檔案,執行程式後:

// 1.一般情況

// 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++注釋問題

// /*******************xx*/

當然你也可以改用其它的input.c來測試。這裡就不在試了。

進製轉換器

專案分析 1.列印主選單。輸出主選單選項 小寫轉化為大寫 輸入的驗證 2.大小寫的轉化。輸入需要轉化的數 必須要小於基數 需驗證為字串型 引數 基數 返回值 乙個通過驗證過的數 3.進製的轉化 轉化的方式分兩種 一種是十進位制轉化為其他進製。另一種是其他進製轉化為十進位制 public classc...

進製轉換器

include include include define max 10 define add 10 typedef int elemtype typedef struct sqstacksqstack 初始化 intinsistack sqstack s s.top s.base s.maxsi...

進製轉換器

int tennum char a,int b 將輸入的數字轉換為10進製數 else if a i a a i f sum sum b num return sum 功能為將輸入的數字通過字串來儲存,利用字串的形式將不同的進製數轉換為10進製數,並以整型將10進製數返回。void numchang...