View
256
Download
0
Embed Size (px)
Citation preview
1
生物計算期末作業
暨南大學資訊工程系
2003/05/20
2
如何從 C/C++ 讀入參數
int main(int argc, char* argv[]) argc: 參數的個數, argc>0 argv[0]: 目前被執行的檔案名稱,以字串表示 argv[1]: 第一個參數,以字串表示 argv[2]: 第二個參數 …依此類推
3
在老師給各位同學的檔案裡,包含了(1) *.dat
(2) *.cpp
(3) *.lex
(4) *.seq
4
punct.dat punctuation in C/C++ programming language
keywords.dat keywords in C/C++ programming language
corpus.dat identifiers that you should assume they all have t
he same meaning in different files
我們以換行( \n )作為分隔符號同學可以假設在這三個檔案中出現的字串在不同的 C/
C++ source code 中意義是相同的。
5
*.cpp
包含所有要給同學測試的 C/C++ source code 命名規則:
pnnn-v.cpp: nnn: 一組數字 v: 1 是原來的版本, 2 是被老師亂改過後的 nnn 相同則表示檔案來源相同
ACM INTERNATIONAL COLLEGIATE PROGRAMMING CONTEST 的解答 http://acm.uva.es/problemset/
6
*.lex
被切割成一堆 tokens 的檔案,其中 comments 已經被移除了。
比如說, p101-2.lex 是 p101-2.cpp 切割好的結果。
7
*.seq
*.lex 轉換成數字的序列,數字與數字之間用 /t ( tab )區隔
所有在 *.dat 中出現過的字串編號都相同,以負數表示,其餘字串以正整數表示
8
評分標準 每一位同學至少要能完成 *.seq 之間的比對。 最好是能完成 *.lex 之間的比對。 如果能完成 *.cpp 之間的比對,那就更好。
基本分數: 70 、 80 、 90 完整性:程式至少要能 run 正確性:降低 false positive 與 false negative 效率:不能太暴力去蠻做
9
C/C++ 的 comments
移除所有的 comments : /* ……… */ // ………
要注意 C++ 的 comment 是以 EOL 結尾// this is a comment
C 的 comment 可以換行,但是不可以 nested /* this is
a valid comment
//*/
10
/* this is another
* valid comment /*
*/ // this is a strange comment, /*
#include <stdio.h>
/* but still valid */
11
處理字串會遇到的問題
Escape code "say \"hello\"" '\'‘
與 comment 造成的問題 "3 /* ...... */ 4" "3 //4"
空白造成的問題 "const int a=10"
12
換行與空白
int a = 1; 與 int a=1; 是相同的。 if (a==1)
return 1; void f()
{
retrun;
}
13
用有系統的方式去切 tokens
將所有可能的情況先規劃清楚
14
o/w: otherwise
EOF: end-of-file
15
ctype.h
isalnum :數字與字母 iscntrl :控制字元 ispunct :標點符號 isalpha :字元 isdigit :十進位數字 isspace :空白(包含 /f /r /n /v /t )
16
建立 symbol tableunsigned int sum=0;
while (*symbol != '\0')
sum+=*symbol++;
return sum % TABLE_SIZE;
17
Separate chaining
18
Node* SymbolTable::search(char *symbol)
{
int posn=hash(symbol);
Node *temp;
for (temp=table[posn]; temp!=NULL; temp=temp->next) {
if (strcmp(symbol, temp->symbol)==0)
return temp;
}
return NULL;
}
19
Node* SymbolTable::insert(char *symbol){Node *temp=search(symbol);
if (temp!=NULL) // symbol is already in the tablereturn temp;
else {int pos=hash(symbol);temp=table[pos];table[pos]=new Node; // create a new nodestrcpy(table[pos]->symbol, symbol);table[pos]->sn=++counter; // unique idtable[pos]->next=temp;
}return temp;
}