- 浏览: 444047 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (158)
- J2SE (15)
- c/c++ (17)
- linux & ubuntu (20)
- js (18)
- algorithm (21)
- android (1)
- software (3)
- svn (1)
- db (6)
- other (19)
- css (5)
- go (1)
- html 5 (3)
- computer science (1)
- php (3)
- 创业 (8)
- EJB & jboss (1)
- TDD (1)
- jsp & servlet (2)
- http, tcp & ip (2)
- hibernate (1)
- json (1)
- 乐 (2)
- ps (2)
- netbeans (1)
- extjs (2)
- eclipse (4)
- 项目管理 (1)
- varnish (2)
- study abroad (1)
- python (1)
- erlang (1)
- math (1)
- shell (1)
- assembly (4)
- lucene (1)
- web (1)
- http (1)
- tcp & ip (1)
最新评论
-
yiguxianyun:
...
css li 不换行 -
stdayong:
...
netbeans 中使用 maven -
程序猿_星:
为啥会中文乱码啊
servlet 以 gzip 格式返回数据 -
huanhuan519:
感谢分享~
gdb 调试工具 -
heyl1234:
写过些js,对css还不熟。谢谢~
css li 不换行
c - word counter (binary-tree)
count ascii word in a file, written by c, using binary-tree,
code:
word_counter.c:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define WORD_MAX_LEN 50 #define LAST_LETTER 1 #define LAST_OTHER 0 /** * count ascii words * @author kuchaguangjie * @date 2012-05-09 14:05:21 * @mail kuchaguangjie@163.com */ struct word_count { char *word; int count; struct word_count *left; struct word_count *right; }; /** * find word from FILE * * state machine of word found: |---------------------------------------| |last_char new_char word_found| |---------------------------------------| |letter other Y | |letter letter N | |other other N | |other letter N | |---------------------------------------| letter, include: a-z A-Z, other, any other ascii char, * * @param fp * pointer to FILE * @param word * the char array to store word * @param limit * max char count in the word * * @return * 1 -> not reach EOF, 0 -> EOF, * if reach EOF, it will put into word the string found, or an empty string, */ int getword(FILE *fp, char *word, int limit) { int i = 0, last = LAST_OTHER; char c; while ((c = fgetc(fp)) != EOF) { if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) { // a - z, A - Z, last = LAST_LETTER; if (i < WORD_MAX_LEN) word[i++] = c; } else { if (last == LAST_LETTER) { word[i] = '\0'; return 1; } last = LAST_OTHER; } } // reach EOF word[i] = '\0'; return 0; } /** * alloc memory for a struct word_count */ struct word_count *word_alloc() { return (struct word_count *) malloc(sizeof(struct word_count)); } /** * binary-tree add * * @param p * pointer to the node on/under which to add/count the word * @param word * the word to add * * @return * the pointer on which the word is add/count++ */ struct word_count *btree_add(struct word_count *p, char *word) { int cmp; if (p == NULL) { p = word_alloc(); p->word = strdup(word); // tip: here must duplicate the string, because the original string will change later, p->count = 1; p->left = p->right = NULL; } else if ((cmp = strcmp(word, p->word)) == 0) p->count++; else if (cmp < 0) p->left = btree_add(p->left, word); else p->right = btree_add(p->right, word); return p; // tip: must return the pointer, in case when original pointer is NULL, need to update it, } /* print all nodes in order */ void treeprint(struct word_count *p) { if (p != NULL) { treeprint(p->left); printf("%6d, %s\n", p->count, p->word); treeprint(p->right); } } /** * do count words from a file * * @param fp * FILE pointer * @param counts * word_count array, which is ordered, * @param n * word counts, equals to word_count array size, * * @return * total different word */ void docount(FILE *fp) { struct word_count *root = NULL; char word[WORD_MAX_LEN + 1]; int end; while ((end = getword(fp, word, WORD_MAX_LEN)) == 1) { root = btree_add(root, word); } // last word if (word[0] != '\0') { root = btree_add(root, word); } treeprint(root); } int main() { char *fpath = "/home/eric/workspace/c_workplace/practise/word_counter.c"; FILE *fp = fopen(fpath, "r"); docount(fp); return 1; }
发表评论
-
c - linkedlist
2012-05-10 14:52 1093c - linkedlist store ordere ... -
c - pointer is also pass by value
2012-05-09 14:13 972c - pointer is also pass by ... -
find palindromic-prime in pi
2012-04-26 18:32 1857find palindromic-prime in pi ... -
c #define
2012-04-08 13:29 2110c #define macro substitu ... -
c static
2012-04-04 21:59 1243c static static external ... -
c extern
2012-04-04 21:53 1160c extern extern, used to de ... -
int to string by specified base
2012-04-03 22:15 1087int to string by specified base ... -
random select
2011-08-28 01:00 1210random select problem: ... -
sparse data structure - matrix
2011-08-18 20:03 1090sparse data structure sp ... -
max sub_sequence - c
2011-08-10 01:02 1080max sub_sequence - c /* ... -
binary search - c
2011-08-06 12:07 1098binary search - c (simple) ... -
bit_array - simple use
2011-05-28 23:47 1010bit array,use less memory to de ... -
linux c udp
2011-04-01 18:02 2100linux 下可用 c 进行 udp 通信,使用 server ... -
linux c tcp
2011-04-01 18:00 3066linux 下可用 c 进行 tcp 通信,使用 server ... -
gdb 调试工具
2011-02-21 17:20 3324gdb 调试工具 gdb 概 ... -
linkedlist - java 简单实现
2011-02-11 21:29 1603linked list 链表, - ... -
queue (用 java 简单实现)
2011-02-03 01:45 4056queue ------ 结构 线性存 ... -
Medians and Order Statistics (次序统计)
2011-01-03 14:36 2836Medians and Order Statistics - ... -
counting sort
2011-01-02 20:36 1571counting sort ------ counting ... -
quick sort
2011-01-01 20:26 1196quicksort ------ quicksort ove ...
相关推荐
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
c语言入门 c语言_leetcode题解543-diameter-of-binary-tree.c
标题中的"amoeba-mysql-binary-2.2.0.tar" 指的是一种名为Amoeba MySQL二进制分发版的软件包,版本号为2.2.0,其存储格式为tar档案。Amoeba是一个分布式数据库中间件,它能够将一个MySQL实例透明地扩展到多个节点,...
LOIC-1.0.8-binary.zip
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
java java_leetcode-114-flatten-binary-tree-to-linked-list
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
js js_leetcode题解之105-construct-binary-tree-from-preorder
js js_leetcode题解之106-construct-binary-tree-from-inorder
javascript js_leetcode题解之114-flatten-binary-tree-to-linked-list.js
c c语言_leetcode题解之0226_invert_binary_tree
c语言入门 c语言_leetcode题解之0543_diameter_of_binary_tree
java java_leetcode-110-balanced-binary-tree
c c语言_leetcode题解之0297_serialize_and_deserialize_binary_tree
java java_leetcode-maximum-depth-of-binary-tree
c语言入门 c语言_leetcode题解之1373_maximum_sum_bst_in_binary_tree
java java_leetcode-111-minimum-depth-of-binary-tree
c c语言_leetcode题解之0236_lowest_common_ancestor_of_a_binary_tree
java java_leetcode-99-recover-binary-search-tree