Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99450
  • 博文数量: 34
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-27 11:21
文章分类
文章存档

2014年(1)

2013年(33)

我的朋友

分类: LINUX

2013-09-28 16:50:39

原文地址:How to use function pointer 作者:finger_lake

  1. 1 //This is a quicksort algorithm write by xiuxia,it's like C++ STL
  2.   2 //But can be used in C
  3.   3 #include <stdio.h>
  4.   4 #include <stdlib.h>
  5.   5 #include <string.h>
  6.   6 //int cmp(int *a, int *b);
  7.   7 typedef struct
  8.   8 {
  9.   9 int x;
  10.  10 int y;
  11.  11 }pointer;
  12.  12
  13.  13 int numcmp(char *s1,char *s2);
  14.  14 void qsort_1(void *v[], int left,int right,int (*comp)(void *,void *))
  15.  15 {
  16.  16 int i,last;
  17.  17 void swap(void *v[],int,int); //
  18.  18 if(left>=right)
  19.  19 return ;
  20.  20 swap(v,left,(left+right)/2); //why swap ??
  21.  21 last=left;
  22.  22 for(i=left+1;i<=right;i++)
  23.  23 {
  24.  24 if( (*comp)( v[i], v[left] )<0 )
  25.  25 {
  26.  26 swap(v,++last,i);
  27.  27 }
  28.  28 }
  29.  29 swap(v,left,last);
  30.  30 qsort_1(v,left,last-1,comp);
  31.  31 qsort_1(v,last+1,right,comp);
  32.  32 }
  33.  33
  34.  34 void swap(void *v[],int i,int j)
  35.  35 {
  36.  36 void *temp;
  37.  37 temp=v[i];
  38.  38 v[i]=v[j];
  39.  39 v[j]=temp;
  40.  40 }
  41.  41
  42.  42 int numcmp(char *s1,char *s2)
  43.  43 {
  44.  44 double v1,v2;
  45.  45 v1=atof(s1);
  46.  46 v2=atof(s2);
  47.  47 if(v1<v2)
  48.  46 v2=atof(s2);
  49.  47 if(v1<v2)
  50.  48 return -1;
  51.  49 else if(v1>v2)
  52.  50 return 1;
  53.  51 else
  54.  52 return 0;
  55.  53 }
  56.  54
  57.  55 int cmp_2(pointer *a, pointer *b)
  58.  56 {
  59.  57 int aa=a->x;
  60.  58 int bb=b->x;
  61.  59 if(aa<bb)
  62.  60 return -1;
  63.  61 else if(aa>bb)
  64.  62 return 1;
  65.  63 else
  66.  64 return 0;
  67.  65 }
  68.  66 int cmp(int *a, int *b)
  69.  67 {
  70.  68 int aa= (int)*a;
  71.  69 int bb=(int)*b;
  72.  70 if(aa<bb)
  73.  71 return -1;
  74.  72 else if(aa>bb)
  75.  73 return 1;
  76.  74 else
  77.  75 return 0;
  78.  76 }
  79.  77
  80.  78
  81.  79 int main()
  82.  80 {
  83.  81 //int A[10]={5,8,23,12,0,3,5};
  84.  82 int *A[10];
  85.  83 pointer *B[5];
  86.  84 int i;
  87.  85 int n=7;
  88.  86 for (i=0;i<10;i++)
  89.  87 A[i]=(int *)malloc(4);
  90.  88 *A[0]=9;
  91.  89 *A[1]=8;
  92.  90 *A[2]=23;
  93.  91 *A[3]=12;
  94.  92 for(i=0;i<4;i++)
  95.  91 *A[3]=12;
  96.  92 for(i=0;i<4;i++)
  97.  93 {
  98.  94 printf("%d\n",*A[i]);
  99.  95 }
  100.  96
  101.  97 qsort_1((void **)&A,0,4-1,(int (*)(void *,void *))cmp);
  102.  98 for(i=0;i<4;i++)
  103.  99 {
  104. 100 printf("%d\n",*A[i]);
  105. 101 }
  106. 102
  107. 103 //about B
  108. 104
  109. 105 for (i=0;i<5;i++)
  110. 106 B[i]=(pointer *)malloc(sizeof(pointer));
  111. 107
  112. 108 B[0]->x=7;
  113. 109 B[0]->y=9;
  114. 110
  115. 111 B[1]->x=0;
  116. 112 B[1]->y=6;
  117. 113
  118. 114 B[2]->x=4;
  119. 115 B[2]->y=2;
  120. 116
  121. 117 B[3]->x=3;
  122. 118 B[3]->y=5;
  123. 119 qsort_1((void **)B,0,3,(int (*)(void *, void *))cmp_2 );
  124. 120 printf("-------------------------------------\n");
  125. 121 for(i=0;i<3;i++)
  126. 122 {
  127. 123 printf("%d %d\n",B[i]->x,B[i]->y);
  128. 124 }
  129. 125 printf("-------------------------------------\n");
  130. 126 char *lineptr[4];
  131. 127
  132. 128 lineptr[0]=(char *)malloc(sizeof(char)*10);
  133. 129 strcpy(lineptr[0],"zhang");
  134. 130 lineptr[1]=(char *)malloc(sizeof(char)*10);
  135. 131 strcpy(lineptr[1],"xiu");
  136. 132 lineptr[2]=(char *)malloc(sizeof(char)*10);
  137. 133 strcpy(lineptr[2],"xia");
  138. 134
  139. 135 lineptr[3]=(char *)malloc(sizeof(char)*10);
  140. 136 strcpy(lineptr[3],"love");
  141. 137
  142. 136 strcpy(lineptr[3],"love");
  143. 137
  144. 138 lineptr[4]=(char *)malloc(sizeof(char)*10);
  145. 139 strcpy(lineptr[4],"life");
  146. 140
  147. 141 qsort_1((void **)lineptr,0,4,(int (*)(void *, void *))strcmp);
  148. 142
  149. 143 for(i=0;i<5;i++)
  150. 144 printf("%s\n",lineptr[i]);
  151. 145 return 0;
  152. 146 }

~                                               
阅读(1581) | 评论(0) | 转发(0) |
0

上一篇:struct module

下一篇:kill_fasync及fasync_helper

给主人留下些什么吧!~~