免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1642 | 回复: 3
打印 上一主题 下一主题

[C] 函数指针,做题的时候遇到困惑了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-02-24 20:27 |只看该作者 |倒序浏览
本帖最后由 newonejoe 于 2016-02-24 20:30 编辑

具体代码如下
  1. /* 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 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>

  6. #define MAXLEN 100
  7. #define MAXWORD 1000

  8. struct tnode {
  9.     char *word;
  10.     int count;
  11.     struct tnode * left;
  12.     struct tnode * right;
  13. };

  14. struct key
  15. {
  16.     char *word;
  17.     int count;
  18. };

  19. int getword(char *, int);
  20. struct tnode * addtree(struct tnode *, char *);
  21. void treeprint(struct tnode *);
  22. void treeToKeyTab(struct tnode *);
  23. void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *));
  24. int keycomp(struct key * , struct key *);
  25. void swap(void *v[], int , int);

  26. int nodecount = 0;
  27. struct key wordtab[ MAXWORD];
  28. int wordcounter = 0;

  29. /* display word in count decreasing order */
  30. int main(void)
  31. {
  32.     int cond;
  33.     char word[MAXLEN];

  34.     struct tnode * root = NULL;

  35.     while ((cond = getword(word, MAXLEN)) != EOF)
  36.     {
  37.         if (isalpha(*word))
  38.             root = addtree(root, word);
  39.     }
  40.     treeprint(root);
  41.     printf("-----------\n");
  42.     treeToKeyTab(root);

  43.     for (int i = 0; i < nodecount; i++)
  44.         printf("%4d %s\n", (wordtab+i)->count, (wordtab+i)->word);

  45.     printf("-----------\n");

  46.     // sort the struct key array
  47.     //void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *));
  48.     //int keycomp(struct key*, struct key*);
  49.     word_qsort((void **)wordtab, 0, nodecount-1, (int (*)(void *, void *))keycomp);

  50.     for (int i = 0; i < nodecount; i++)
  51.         printf("%4d %s\n", (wordtab+i)->count, (wordtab+i)->word);
  52.     return 0;
  53. }


  54. /* getword: getword and return */
  55. int getword(char * word, int lim)
  56. {
  57.     int c, i = 0;
  58.     while (isspace(c = getc(stdin)))
  59.         ;
  60.     if (c == EOF)
  61.         return c;
  62.     if (!isalpha(c)) {
  63.         word[0] = c;
  64.         return word[0];
  65.     }
  66.     do {
  67.         word[i++] = c;
  68.     } while (isalpha(c = getc(stdin)) && i < lim);
  69.     word[i] = '\0';
  70.     ungetc(c, stdin);
  71.     return c;
  72. }

  73. /* addtree: add word into exist node or create new node */
  74. struct tnode * addtree(struct tnode *p, char *w)
  75. {
  76.     int cond;
  77.     if (p == NULL) {
  78.         p = (struct tnode *)malloc(sizeof(struct tnode));
  79.         p->word = (char *)malloc(sizeof(char) * MAXLEN);
  80.         strcpy(p->word, w);
  81.         p->count = 1;
  82.         p->left = p->right = NULL;
  83.         nodecount++;        //count the treenode numbers
  84.     } else {
  85.         if ((cond = strcmp(w, p->word)) == 0 ){
  86.             p->count++;   
  87.         } else if (cond < 0) {
  88.             p->left = addtree(p->left, w);
  89.         } else {            
  90.             p->right = addtree(p->right, w);
  91.         }
  92.     }   
  93.     return p;
  94. }

  95. /* treeprint:   print the word */
  96. void treeprint(struct tnode *p)
  97. {
  98.     if ( p ) {
  99.         treeprint(p->left);
  100.         printf("%4d %s\n", p->count, p->word);
  101.         treeprint(p->right);
  102.     }
  103. }

  104. /* treeToKeyTab */
  105. void treeToKeyTab(struct tnode *p)
  106. {
  107.     if (p != NULL)
  108.     {
  109.         treeToKeyTab(p->left);
  110.         (wordtab+wordcounter)->word = strdup(p->word);
  111.         (wordtab+wordcounter)->count = p->count;
  112.         wordcounter++;
  113.         treeToKeyTab(p->right);
  114.     }
  115. }
  116. /* qsort: sort v[left]...v[right] into increasing order */
  117. void word_qsort(void *v[], int left, int right, int (*comp)(void *, void *))
  118. {
  119.     int i, last;
  120.    
  121.     if (left >= right)
  122.         return;
  123.     // use the v[(left + right) / 2]as compare target
  124.     swap(v, left, (left + right)/2);
  125.     last = left;
  126.     for (i = left + 1; i <= right; i++)
  127.     {
  128.         printf("%ld", sizeof(v[i]));
  129.         if ((*comp)(v[i], v[left]) < 0)
  130.         {
  131.             printf("comp\n");
  132.             swap(v, ++last, i);
  133.         }
  134.     }
  135.     swap(v, left, last);
  136.     word_qsort(v, left, last-1, comp);
  137.     word_qsort(v, last+1, right, comp);
  138. }

  139. /* swap:    swap two */
  140. void swap(void *v[], int i, int j)
  141. {
  142.     void *temp;

  143.     temp = v[i];
  144.     v[i] = v[j];
  145.     v[j] = temp;
  146. }

  147. /* comp:    struct compare */
  148. int keycomp(struct key * v1, struct key *v2)
  149. {
  150.     int cond;
  151.     if (v1->count > v2->count)
  152.         return 1;
  153.     else if (v1->count < v2->count)
  154.         return -1;
  155.     else {
  156.         if ((cond = strcmp(v1->word, v2->word)) > 0)
  157.             return 1;
  158.         else if (cond < 0 )
  159.             return -1;
  160.         else
  161.             return 0;
  162.     }
  163. }
复制代码
快速排序程序的声明如下:
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函数是进行不下去,好像是指针转换过程中出了问题,具体的请帮忙分析。

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
2 [报告]
发表于 2016-02-25 11:13 |只看该作者
兄弟可以把指针的地址打出来看看,初始化把指针 设置为NULL 更容易找出问题。

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
3 [报告]
发表于 2016-02-25 11:21 |只看该作者
自己单步调试一下。

论坛徽章:
223
2022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32操作系统版块每日发帖之星
日期:2016-05-10 19:22:58操作系统版块每日发帖之星
日期:2016-02-18 06:20:00操作系统版块每日发帖之星
日期:2016-03-01 06:20:00操作系统版块每日发帖之星
日期:2016-03-02 06:20:0015-16赛季CBA联赛之上海
日期:2019-09-20 12:29:3219周年集字徽章-周
日期:2019-10-01 20:47:4815-16赛季CBA联赛之八一
日期:2020-10-23 18:30:5320周年集字徽章-20	
日期:2020-10-28 14:14:2615-16赛季CBA联赛之广夏
日期:2023-02-25 16:26:26CU十四周年纪念徽章
日期:2023-04-13 12:23:10操作系统版块每日发帖之星
日期:2016-05-10 19:22:58
4 [报告]
发表于 2016-02-25 13:34 |只看该作者
如果是学生没必要纠结这些东西,方向思想都太边缘了,
计算机工程还有更多的方向需要人们去探索,即使商业价值也没有多少,也就是技术中层管理干部不干活专牛角的功夫
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP