?? keyword.c
字號:
//---------------------------------------------------------------------------
/* 統計 C 語言源程序里標識符的個數及相應標識符出現次數的程序 */
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
#include <string.h>
#define KEYWORDMAXLENGTH 32
#define KEYWORDTABLELENGTH 32
#undef getchar
#undef isalnum
#undef isalpha
typedef struct _KeywordTable {
char *key;
int count;
}KeywordTable;
KeywordTable keywordTable[KEYWORDTABLELENGTH] = {
"auto" , 0, "break" , 0, "case" , 0, "char" , 0, "const" , 0, "continue" ,0 ,
"default" , 0, "do" , 0, "double" , 0, "else" , 0, "enum" , 0, "extern" ,0 ,
"float" , 0, "for" , 0, "goto " , 0, "if" , 0, "int" , 0, "long" ,0 ,
"register" , 0, "return" , 0, "short" , 0, "signed" , 0, "sizeof" , 0, "static" ,0 ,
"struct" , 0, "switch" , 0, "typedef" , 0, "union" , 0, "unsigned" , 0, "void" ,0 ,
"volatile" , 0, "while" , 0};
int getword(char *word,int limit,int handle);
int _isalnum(char ch);
int _isalpha(char ch);
int _getchar(int handle);
KeywordTable *binsearch(KeywordTable *pkeywordTable, char *keyWord, int keywordTableLength);
void format(int space);
int main(int argc, char *argv[])
{
char keyWord[KEYWORDMAXLENGTH + 1];
KeywordTable *found;
int handle;
if(argc != 2) {
fprintf(stderr,"請在命令行中指定一個 C 語言源程序文件。\n");
exit(1);
}
else if ((handle = open(*(++argv), O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) {
fprintf(stderr,"打開指定的文件 %s 時出現錯誤。\n",*argv);
exit(2);
}
while(getword(keyWord, KEYWORDMAXLENGTH + 1, handle) > 0) /* 只有這樣改一下,在getword函數中的循環while(len<limit)因為len==limit結束時,word[len] = '\0'才不會產生越界防問的嫌疑. */
if((found = binsearch(keywordTable, keyWord, KEYWORDTABLELENGTH)) != NULL)
found->count++;
close(handle);
for(found = keywordTable; found < keywordTable + KEYWORDTABLELENGTH; found++) {
printf("%10s: %5d\n",found->key,found->count);
//format(0);
}
getch();
return 0;
}
int getword(char *word,int limit,int handle) {
/* 這個 getword 函數沒有考慮預處理宏的影響;考慮了注釋和字符串常量。
* 在處理過程中,函數會把預處理宏當作正常的程序代碼對待,同時,函數
* 也會掠過空白類字符(比如空格、換行符、橫向制表符等)、注釋和字符
* 串常量
* */
static int priorch,ch = ' ';
int len = 0;
while(ch != EOF && !_isalpha(ch)) {
if(isspace(ch)){ /* 掠過空白類字符 */
while(isspace(ch))
ch = _getchar(handle);
continue;
}
else if(ch == '/') { /* 掠過注釋 */
if((ch = _getchar(handle)) == '*') {
while(priorch = ch,(ch = _getchar(handle)) != EOF && (priorch != '*' || ch != '/'));
ch = _getchar(handle);
continue;
}
continue;
}
else if(ch == '\"') { /* 掠過字符串常量 */
while((ch = _getchar(handle)) != EOF) {
if(ch == '\\') {
ch = _getchar(handle);
continue;
}
else if(ch == '\"')
break;
}
ch = _getchar(handle);
continue;
}
ch = _getchar(handle);
}
if(ch == EOF)
return len;
word[len++] = ch; /* 標識符的第一個字符是字母 */
while(len < limit - 1 && _isalnum(ch = _getchar(handle)))/* word里面最多放limit個字符,也就是(KEYWORDMAXLENGTH + 1)個字符. */
word[len++] = ch;
word[len] = '\0';
//printf("%s ",word);
return len;
}/* iint getword(char *word,int limit,int *handle) */
int _isalpha(char ch) {
/* 如果一個字符是字母返回 1 否則返回 0 */
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_';
}/* int _isalpha(char ch) */
int _isalnum(char ch) {
/* 如果一個字符是數字字符或者字母返回 1 否則返回 0 */
return _isalpha(ch) || ch >= '0' && ch <= '9';
}/* int _isalnum(char ch) */
int _getchar(int handle) {
/* 帶有預讀緩沖區的 _getchar */
static char buffer[1024];
static char *bufp;
static int n;
if(n == 0) { /* 預讀緩沖區是空的 */
//n = read(handle,buffer,sizeof(buffer));
n = read(handle,buffer,1024);
bufp = buffer;
}
return (--n >= 0) ? (unsigned char) *bufp++ : (n = 0,EOF);
}/* int _getchar(int *handle) */
KeywordTable *binsearch(KeywordTable *pkeywordTable, char *keyWord, int keywordTableLength) {
/* 使用二分查找法查找一個單詞是不是出現在指定的關鍵詞表中 */
KeywordTable *low = pkeywordTable;
KeywordTable *high = pkeywordTable + keywordTableLength;
KeywordTable *middle;
int cond;
while(low < high) {
middle = low + (high - low) / 2;
if((cond = strcmp(keyWord, middle->key)) < 0)
high = middle;
else if(cond > 0)
low = middle + 1;
else
return middle;
}
return NULL;
}/* KeywordTable *binsearch(KeywordTable *pkeywordTable, char *keyWord, int keywordTableLength) */
void format(int space) {
/* 這個函數以每 (space + 1)次調用形成一個周期,其中的前 space 次每次調用輸出一個空格,第
* (space + 1)次調用輸出一個換行符
* */
static char count;
++count <= space ? putchar(' ') : (count = 0, putchar('\n'));
}/* void format(void) */
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -