《C语言接口与实现》实验——表(WF)
来源:互联网 发布:大数据解决方案供应商 编辑:程序博客网 时间:2024/06/08 04:28
该程序是《C语言接口与实现》书中P87页中wf示例源码:在下载:
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <ctype.h>#include "include/atom.h"#include "include/table.h"#include "include/mem.h"#include <string.h>#include <assert.h>void wf(char *, FILE *);int first(int c);int rest (int c);int compare(const void *x, const void *y);void vfree(const void *, void **, void *);#pragma comment(lib, "libcii.lib")int getword(FILE *fp, char *buf, int size,int first(int c), int rest(int c)) {int i = 0, c;assert(fp && buf && size > 1 && first && rest);c = getc(fp);for ( ; c != EOF; c = getc(fp))if (first(c)) {{if (i < size - 1)buf[i++] = c;}c = getc(fp);break;}for ( ; c != EOF && rest(c); c = getc(fp)){if (i < size - 1)buf[i++] = c;}if (i < size)buf[i] = '\0';elsebuf[size-1] = '\0';if (c != EOF)ungetc(c, fp);return i > 0;}int main(int argc, char *argv[]) {int i;for (i = 1; i < argc; i++) {FILE *fp = fopen(argv[i], "r");if (fp == NULL) {fprintf(stderr, "%s: can't open '%s' (%s)\n",argv[0], argv[i], strerror(errno));return EXIT_FAILURE;} else {wf(argv[i], fp);fclose(fp);}}if (argc == 1) wf(NULL, stdin);return EXIT_SUCCESS;}void wf(char *name, FILE *fp) {Table_T table = Table_new(0, NULL, NULL);char buf[128];while (getword(fp, buf, sizeof buf, first, rest)){const char *word;int i, *count;for (i = 0; buf[i] != '\0'; i++)buf[i] = tolower(buf[i]);word = Atom_string(buf);count = Table_get(table, word);if (count)(*count)++;else {NEW(count);*count = 1;Table_put(table, word, count);}}if (name)printf("%s:\n", name);{ int i;void **array = Table_toArray(table, NULL);qsort(array, Table_length(table), 2*sizeof (*array),compare);for (i = 0; array[i]; i += 2)printf("%d\t%s\n", *(int *)array[i+1],(char *)array[i]);FREE(array); }Table_map(table, vfree, NULL);Table_free(&table);}int first(int c) {return isalpha(c);}int rest(int c) {return isalpha(c) || c == '_';}int compare(const void *x, const void *y) {return strcmp(*(char **)x, *(char **)y);}void vfree(const void *key, void **count, void *cl) {FREE(*count);}
- 《C语言接口与实现》实验——表(WF)
- 《C语言接口与实现》实验——表(Table_T)
- 《C语言接口与实现》实验——链表(List_T)
- 《C语言接口与实现》实验——原子(Atom)
- 《C语言接口与实现》实验——集合(Set_T)
- 《C语言接口与实现》实验——动态数组(Array_T)
- 《C语言接口与实现》实验——位向量(Bit_T)
- 《C语言接口与实现》实验——格式化(Fmt_T)
- 《C语言接口与实现》实验——低级字符串(Str_T)
- 《C语言接口与实现》实验——任意精度算术(AP_T)
- 《C语言接口与实现》实验——扩展精度算术(XP_T)
- 《C语言接口与实现》实验——高级字符串(Text_T)
- 《C语言接口与实现》实验——多精度算数(MPCalc)
- 《C语言接口与实现》实验——环(Ring_T)
- 《C语言接口与实现》实验——集合(xref)
- 《C语言接口与实现》实验——任意精度算数(Calc)
- 《C语言接口与实现》实验——序列(Seq_T)
- 《C语言接口与实现》实验——可变参数表的使用(va_list, va_start, va_arg, va_end)
- 简单使用JDOM解析XML
- 为DataTable设置主键及利用主键查找行数据
- fopen w 和wb
- PHP导出excel(*.xls)文件的简单方法
- 展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
- 《C语言接口与实现》实验——表(WF)
- 数据结构(Java ArrayList模拟)本代码重在学习数据结构思路,代码完整性欠缺,请见谅
- 我作SE的那点事
- 从一个实例看jaxb的强大
- EXCEL 函数
- JiBX初步
- JSF入门
- 黑马程序员—两种设计模式及其代码的实现—第11篇
- 用分治法输出一个一维数组中的n个不同实数间距离最小的两个实数