- 论坛徽章:
- 0
|
本帖最后由 newonejoe 于 2016-02-24 20:30 编辑
具体代码如下- /* Exercise 6-4. Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define MAXLEN 100
- #define MAXWORD 1000
- struct tnode {
- char *word;
- int count;
- struct tnode * left;
- struct tnode * right;
- };
- struct key
- {
- char *word;
- int count;
- };
- int getword(char *, int);
- struct tnode * addtree(struct tnode *, char *);
- void treeprint(struct tnode *);
- void treeToKeyTab(struct tnode *);
- void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *));
- int keycomp(struct key * , struct key *);
- void swap(void *v[], int , int);
- int nodecount = 0;
- struct key wordtab[ MAXWORD];
- int wordcounter = 0;
- /* display word in count decreasing order */
- int main(void)
- {
- int cond;
- char word[MAXLEN];
- struct tnode * root = NULL;
- while ((cond = getword(word, MAXLEN)) != EOF)
- {
- if (isalpha(*word))
- root = addtree(root, word);
- }
- treeprint(root);
- printf("-----------\n");
- treeToKeyTab(root);
- for (int i = 0; i < nodecount; i++)
- printf("%4d %s\n", (wordtab+i)->count, (wordtab+i)->word);
- printf("-----------\n");
- // sort the struct key array
- //void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *));
- //int keycomp(struct key*, struct key*);
- word_qsort((void **)wordtab, 0, nodecount-1, (int (*)(void *, void *))keycomp);
- for (int i = 0; i < nodecount; i++)
- printf("%4d %s\n", (wordtab+i)->count, (wordtab+i)->word);
- return 0;
- }
- /* getword: getword and return */
- int getword(char * word, int lim)
- {
- int c, i = 0;
- while (isspace(c = getc(stdin)))
- ;
- if (c == EOF)
- return c;
- if (!isalpha(c)) {
- word[0] = c;
- return word[0];
- }
- do {
- word[i++] = c;
- } while (isalpha(c = getc(stdin)) && i < lim);
- word[i] = '\0';
- ungetc(c, stdin);
- return c;
- }
- /* addtree: add word into exist node or create new node */
- struct tnode * addtree(struct tnode *p, char *w)
- {
- int cond;
- if (p == NULL) {
- p = (struct tnode *)malloc(sizeof(struct tnode));
- p->word = (char *)malloc(sizeof(char) * MAXLEN);
- strcpy(p->word, w);
- p->count = 1;
- p->left = p->right = NULL;
- nodecount++; //count the treenode numbers
- } else {
- if ((cond = strcmp(w, p->word)) == 0 ){
- p->count++;
- } else if (cond < 0) {
- p->left = addtree(p->left, w);
- } else {
- p->right = addtree(p->right, w);
- }
- }
- return p;
- }
- /* treeprint: print the word */
- void treeprint(struct tnode *p)
- {
- if ( p ) {
- treeprint(p->left);
- printf("%4d %s\n", p->count, p->word);
- treeprint(p->right);
- }
- }
- /* treeToKeyTab */
- void treeToKeyTab(struct tnode *p)
- {
- if (p != NULL)
- {
- treeToKeyTab(p->left);
- (wordtab+wordcounter)->word = strdup(p->word);
- (wordtab+wordcounter)->count = p->count;
- wordcounter++;
- treeToKeyTab(p->right);
- }
- }
- /* qsort: sort v[left]...v[right] into increasing order */
- void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *))
- {
- int i, last;
-
- if (left >= right)
- return;
- // use the v[(left + right) / 2]as compare target
- swap(v, left, (left + right)/2);
- last = left;
- for (i = left + 1; i <= right; i++)
- {
- printf("%ld", sizeof(v[i]));
- if ((*comp)(v[i], v[left]) < 0)
- {
- printf("comp\n");
- swap(v, ++last, i);
- }
- }
- swap(v, left, last);
- word_qsort(v, left, last-1, comp);
- word_qsort(v, last+1, right, comp);
- }
- /* swap: swap two */
- void swap(void *v[], int i, int j)
- {
- void *temp;
- temp = v[i];
- v[i] = v[j];
- v[j] = temp;
- }
- /* comp: struct compare */
- int keycomp(struct key * v1, struct key *v2)
- {
- int cond;
- if (v1->count > v2->count)
- return 1;
- else if (v1->count < v2->count)
- return -1;
- else {
- if ((cond = strcmp(v1->word, v2->word)) > 0)
- return 1;
- else if (cond < 0 )
- return -1;
- else
- return 0;
- }
- }
复制代码 快速排序程序的声明如下:
void word_qsort(void *v[], int left, int right, int(*comp)(void *, void *));
其中int(*comp)(void *, void *)是一个函数指针,意思是:
该函数指针指向一个函数,该函数的返回值是int类型
然后我在main函数中调用该函数的语句是:
word_sort((void **)wordtab, 0, node count-1, (int (*)(void *, void *))keycomp);
其中wordtab是结构体数组,keycomp是一个比较函数
wordtab的结构体是
struct key {
char *word;
int count;
};
keycamp函数的声明如下
int keycomp(struct key * v1, struct key *v2);
我运行的时候每次在调用keycomp函数是进行不下去,好像是指针转换过程中出了问题,具体的请帮忙分析。 |
|