分享:哈希表运用(宏定义)
功能:宏定义和宏定义删除终于把结构体这章弄完了,太耗时了。不过,这章和数组及指针那章学到的东西很多,不愧是金典!!!
Hashtab_Def.h:
程序代码:
#ifndef HASHTAB_DEFINE #define HASHTAB_DEFINE #define MAXWORD 100 #define HASHSIZE 101 typedef struct Hlist { char *name; char *def; struct Hlist *next; }HList; Hlist *install(char *, char *); Hlist *lookup(char *); void error(int, char *); int getch(void); void ungetch(int); int getword(char *, int); void skipblanks(void); void getdef(void); void undef(char *); void ungets(char *); unsigned hash(char *s); char *strdup(char *s); static Hlist *hashtab[HASHSIZE]; #endif
Hashtab_Def.cpp:
程序代码:
#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #include "Hashtab_Def.h" int main() { char w[MAXWORD]; int i; Hlist *p; while (getword(w, MAXWORD) != EOF) if (strcmp(w, "#") == 0) getdef(); else printf("%s :not is define name\n", w); printf("Please end EOF to printf result\n"); for (i = 0; i < HASHSIZE; i++) for (p = hashtab[i]; p != NULL; p = p->next) printf("%s %s\n", p->name, p->def); return 0; } void getdef(void) { int c, i; char name[MAXWORD], def[MAXWORD], dir[MAXWORD]; skipblanks(); if (!isalpha(getword(dir, MAXWORD))) error(dir[0], "getdef :non-alpha - name expected"); else if (strcmp(dir, "define") == 0) { skipblanks(); if (!isalpha(getword(name, MAXWORD))) error(name[0], "getdef: non -alpha -name expected"); else { skipblanks(); for (i = 0; i < MAXWORD; i++) if ((def[i] = getch()) == EOF || def[i] == '\n') break; c = def[i]; def[i] = '\0'; if (c == '\n' && i == 0) error('\n', "define cannot is line feed\n"); else install(name, def); } } else if (strcmp(dir, "undef") == 0) { skipblanks(); if (!isalpha(getword(name, MAXWORD))) error(name[0], "getdef :non-alpha in undef"); else undef(name); } else error(dir[0], "error : not is #define or #undef"); } void skipblanks() { int c; while ((c = getch()) == '\t' || c == ' ') ; ungetch(c); } void error(int c, char *s) { printf("error:%s\n", s); while (c != EOF && c != '\n') c = getchar(); } Hlist *install(char *name, char *def) { Hlist *p; unsigned hashval; if ((p = lookup(name)) == NULL) { p = (Hlist *)malloc(sizeof(Hlist)); if (p == NULL || (p->name = strdup(name)) == NULL) return NULL; hashval = hash(name); p->next = hashtab[hashval]; hashtab[hashval] = p; printf("define successed\n"); } else free(p->def); if ((p->def = strdup(def)) == NULL) { printf("redefine successed\n"); return NULL; } return p; } Hlist *lookup(char *s) { Hlist *p; for (p = hashtab[hash(s)]; p != NULL; p = p->next) if (!strcmp(s, p->name)) return p; return NULL; } unsigned hash(char *s) { unsigned hashval; for (hashval = 0; *s != '\0'; s++) hashval = *s + 31 * hashval; return hashval % HASHSIZE; } void undef(char *s) { int h; Hlist *prev, *p; prev = NULL; h = hash(s); for (p = hashtab[h]; p != NULL; p = p->next) { if (!strcmp(s, p->name)) break; prev = p; } if (p != NULL) { if (prev == NULL) hashtab[h] = p->next; else prev->next = p->next; free(p->name); free(p->def); free(p); printf("Undef successed\n"); } }
GetWord.cpp:
程序代码:
#include <ctype.h> #include <stdio.h> int getword(char *word, int lim) { int c, getch(); void ungetch(int); char *w = word; while (isspace(c = getch())) ; if (c != EOF) *w++ = c; if (!isalnum(c)) { *w = '\0'; return c; } for (; --lim; w++) if (!isalnum(*w = getch())) { ungetch(*w); break; } *w = '\0'; return word[0]; }
Getch.cpp:
程序代码:
#include<string.h> #include <stdio.h> #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { if (bufp >= BUFSIZE) printf("ungetch: too many charaters\n"); else buf[bufp++] = c; } void ungets(char *s) { int len; extern void ungetch(int); while ((len = strlen(s)) > 0) ungetch(s[--len]); }
Strdup.cpp:
程序代码:
#include <malloc.h> #include <string.h> char *strdup(char *s) { char *p; p = (char *)malloc(strlen(s) + 1); if (p != NULL) strcpy(p, s); return p; }