《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);}