jazykc  2013.3
upoljazykc
 All Classes Files Functions Variables Typedefs Macros Pages
stdlib-qsort.c
Go to the documentation of this file.
1 
3 #include <stdlib.h>
4 #include <stdio.h>
5 
6 #include <string.h>
7 
8 
9 #ifndef NELEM
10 #define NELEM(pole) (sizeof pole/sizeof *pole)
11 #endif
12 
14 char *jmena[] = {"Myspulin","Fifinka","Pinda","Bobik"};
15 
16 int inty[] = { 2, 6, 1, 4};
17 
18 double dbly[] = { 2.0, 6.1, 1.2, 4.5};
19 
20 /* TODO: udelat compare funkce verejne! vyuzije je i bst_tree, bsearch ... */
21 
23 static int str_asc (const void *a, const void *b) { return strcmp(*(char **)a, *(char **)b); }
24 
27 static int int_asc (const void *a, const void *b) { return *(int *)a - *(int *)b; }
28 static int int_desc(const void *a, const void *b) { return *(int *)b - *(int *)a; }//sestupnÄ›
29 
31 static int dbl_asc(const void *a, const void *b) {
32  return *(double *)a > *(double *)b ? 1 : -1;// equal: vracet 0 neresime?
33 }
34 
35 /* deklarace ze stdlib.h:
36 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
37 */
38 
39 int main(void) {
40  int i;
41  for(i=0; i<NELEM(jmena); i++){ printf("%c\"%s\"", i?',':'{', jmena[i]);} ; printf("}\n");
42  qsort(jmena, NELEM(jmena), sizeof *jmena, str_asc);
43  for(i=0; i<NELEM(jmena); i++){ printf("%c\"%s\"", i?',':'{', jmena[i]);} ; printf("}\n");
44 
45  qsort(inty, NELEM(inty), sizeof *inty, int_desc);
46  for(i=0; i<NELEM(inty); i++){ printf("%c%6d", i?',':'{', inty[i]);} ; printf("}\n");
47 
48  qsort(dbly, NELEM(dbly), sizeof *dbly, dbl_asc);
49  for(i=0; i<NELEM(dbly); i++){ printf("%c%6g", i?',':'{', dbly[i]);} ; printf("}\n");
50 
51  return 0;
52 }
char * jmena[]
Definition: stdlib-qsort.c:14
#define NELEM(pole)