1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2...

23
1 生生生生生生生生 生生生生生生生生生 2003/05/13

Post on 18-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

1

生物計算期末作業

暨南大學資訊工程系

2003/05/13

2

compare f1 f2 只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。

compare directory 以兩兩比對的方式,比對一個目錄下所有檔案的相似程度。

將相似度很高的檔案做成報表輸出,報表中至少要包含 被判定相似程度很高的檔名清單。 被認定相似的總比數。 將所有檔名排序,並列出與哪些檔案相似。

3

假設我們有 f1 f2 f3 f4 f5 五個檔案: 相似清單:

f1 f4 f5 相似總比數:

3 相似關係

f1: f4 f5 f2 f3 f4: f1 f5 f5: f1 f4

4

移除

移除所有的 comments : /* ……… */ // ………

5

Keywords in ANSI C++

6

Keywords in ANSI C

Keywords 是重要的比對要素。

7

Operators and Punctuation

Operators 也是重要的比對要素。

8

White Space

Newline \n Horizontal tab \t Vertical tab \v Carriage return \r Form feed \f Blank

9

將 input file 切割成 tokens

運用 operators 、 punctuations 、與 white spaces ,將 input file 切成 tokens : x1 = wide * sin(angle);x1

=wide*sin(angle);

10

if (1 != scanf("%c", &ch)) return 0;if

(

1

!=

scanf

(

"%c"

)

)

return

0

;

注意,字串要特別處理。

11

建立 symbol table

我們將切出來的 tokens 全部建立在一個 symbol table 中,然後將 table 中的 tokens 編號。

在建 table 時,有幾點要注意: 先在 table 中內建 keywords 、 operators 、 punct

uations ,這樣可使得被內建的這些 tokens 有固定的 ID 。

所有 C/C++ 標準內建函式庫需要先建在 table 裡。 Scope 的問題先不處理,但如果同學要處理也是做得到的。

12

處理 identifiers 會遇到的問題

同一檔案中名字相同,但卻表示不同的變數: scoping 、 operator overloading 、 function name

overload 兩個要比較的檔案中,名稱不同但實際上卻相同的兩個 identifier 。 User defined functions

兩個要比較的檔案中,名稱一樣,而實際上也是相同的兩個名稱: C/C++ library

13

Scope 的問題

void f() { int a; ...}

void g() { int a; ...}

兩個 a 不同,但卻被我們當成一樣。

14

Alias 的問題

void f() { printf( ……); }

void g() { ... }

void g() { printf(……); }

void f() { ... }

15

Macro 造成的問題

#define A printf #define A (B)

先不處理

16

所以我們處理的方式是採取先內建一些標準 C/C++ 的identifiers 、 keywords 、 operators 、 punctuation 在 symbol table 中。

對於 user defined 的 identifiers ,降低 match/mismatch 的分數。

17

Keywords 的分類 Data type :

整數: int 、 short 、 signed 、 unsigned 、 long 浮點: float 、 double 、 long double 字串: char 、 char*

Flow control : for 、 while 、 do

幾乎是可以忽略的 keywords : auto 、 const 、 extern 、 register 、 static 、 volatil

e

同類的 keywords 比對時給分要比較高。

18

可以考慮用 hash table 建 symbol table

當 hash 發生 collision 時,用 separate chaining 的方式處理。

19

Loop normalization (不一定要做) For (s1; s2; s3) { …}

s1 { If (s2) break; … s3 Goto }

20

測試資料

大部分都是 C ,只有很少的會用到一部分 C++ 的功能,但不會用到 Class 。

比賽時會有另一組測試資料。

21

False positive: 被誤判成有密切關連

False negative: 原來有密切關連,但卻被誤判成沒關連

同學的程式要降低這兩項發生的比率。

22

如何找 disjoint highest similar regions

23