home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / smarts / cdll / cdll.c next >
Encoding:
Text File  |  1996-02-21  |  5.2 KB  |  172 lines

  1. --------------------------------------------------------------------------------
  2. --
  3. -- COPYRIGHT:
  4. --   IBM WorkFrame - Project Smarts
  5. --   (C) Copyright International Business Machines Corporation 1996
  6. --   Licensed Material - Program-Property of IBM - All Rights Reserved.
  7. --   US Government Users Restricted Rights - Use, duplication, or disclosure
  8. --   restricted by GSA ADP Schedule Contract with IBM Corp.
  9. --
  10. --------------------------------------------------------------------------------
  11.  
  12. <include prologc.tde>
  13.  
  14. /*----------------------------------------------
  15.  * $FILE_NAME$.c - Source file for a C DLL
  16.  *----------------------------------------------*/
  17.  
  18. #include "$FILE_NAME$.h"
  19.  
  20. #include <stdio.h>
  21.  
  22. /*----------------------------------------------------------------- */
  23. /* This file defines 4 exported functions, and 2 internal functions.*/
  24. <if ($CPPFILT$)>
  25. /* No symbol or function is marked with the _Export keyword or      */
  26. /* exported via the #pragma export directive.  Exporting is done    */
  27. /* using a .DEF file created with the help of the CPPFILT utility   */
  28. /* that creates an EXPORT entry for every public symbol.            */
  29. </if>
  30. <if ($EXPORT$)>
  31. /* The functions to be exported contain an _Export qualifier        */
  32. </if>
  33. <if ($PRAGMA$)>
  34. /* The functions to be exported are listed below.                   */
  35. /* The format of #pragma export is:                                 */
  36. /*     #pragma export (identifier,"exportName",ordinal)             */
  37. /*        or                                                        */
  38. /*     #pragma export (identifier,,ordinal)                         */
  39. /* The exportName is optional, and only required when exporting the */
  40. /* identifier as a different name than it is internally.            */
  41. #pragma export( bubble    ,, 1 )
  42. #pragma export( insertion ,, 2 )
  43. #pragma export( selection ,, 3 )
  44. #pragma export( list      ,, 4 )
  45. </if>
  46. /*----------------------------------------------------------------- */
  47.  
  48. size_t nSwaps;
  49. size_t nCompares;
  50.  
  51.  
  52. /*--------------------*/
  53. /* Swap two integers. */
  54. /*--------------------*/
  55. static void swap(int *x, int *y)
  56. {
  57.    int temp;
  58.    temp = *x;
  59.    *x = *y;
  60.    *y = temp;
  61.    ++nSwaps;
  62.    return ;
  63. }
  64.  
  65.  
  66. /*-----------------------*/
  67. /* Compare two integers. */
  68. /*-----------------------*/
  69. static int compare(int x, int y)
  70. {
  71.    ++nCompares;
  72.    return (x-y);
  73. }
  74.  
  75.  
  76. /*-------------------------------------------------------------------*/
  77. /* Use Bubble Sort to sort an array of integers in increasing order. */
  78. /*-------------------------------------------------------------------*/
  79. <if ($EXPORT$)>
  80. void _Export bubble(int array[], size_t nNumElements)
  81. <else>
  82. void bubble(int array[], size_t nNumElements)
  83. </if>
  84. {
  85.    size_t i,j;                         /* array indices  */
  86.    nSwaps = nCompares = 0;
  87.    for (i = 0; i < nNumElements-1; ++i) {
  88.       for (j = nNumElements-1; j > i; --j) {
  89.          if (compare(array[j], array[j-1]) < 0)
  90.             swap(&array[j], &array[j-1]);
  91.       }
  92.    }
  93.    return ;
  94. }
  95. <if ($STATIC_LINK$)>
  96. #pragma  handler( bubble )
  97. </if>
  98.  
  99.  
  100. /*--------------------------------------------------------------------------*/
  101. /* Use Insertion Sort to sort an array of integers in increasing order.     */
  102. /*--------------------------------------------------------------------------*/
  103. <if ($EXPORT$)>
  104. void _Export insertion(int array[], size_t nNumElements)
  105. <else>
  106. void insertion(int array[], size_t nNumElements)
  107. </if>
  108. {
  109.    size_t i,j;                         /* array indices  */
  110.    nSwaps = nCompares = 0;
  111.    for (i = 1; i < nNumElements; ++i) {
  112.       for (j = i; j && (compare(array[j], array[j-1]) < 0); --j)
  113.          swap(&array[j], &array[j-1]);
  114.    }
  115.    return ;
  116. }
  117. <if ($STATIC_LINK$)>
  118. #pragma  handler( insertion )
  119. </if>
  120.  
  121.  
  122. /*--------------------------------------------------------------------------*/
  123. /* Use Selection Sort to sort an array of integers in increasing order.     */
  124. /*--------------------------------------------------------------------------*/
  125. <if ($EXPORT$)>
  126. void _Export selection(int array[], size_t nNumElements)
  127. <else>
  128. void selection(int array[], size_t nNumElements)
  129. </if>
  130. {
  131.    size_t i,j,lowindex;                /* array indices                     */
  132.    int lowkey;                         /* temporary storage for lowest key  */
  133.                                        /* value                             */
  134.    nSwaps = nCompares = 0;
  135.    for (i = 0; i < nNumElements-1; ++i) {
  136.       lowindex = i;
  137.       lowkey = array[i];
  138.       for (j = i+1; j < nNumElements; ++j) {
  139.          if (compare(array[j], lowkey) < 0) {
  140.             lowkey = array[j];
  141.             lowindex = j;
  142.          }
  143.       }
  144.       if (lowindex != i)
  145.          swap(&array[i], &array[lowindex]);
  146.    }
  147.    return ;
  148. }
  149. <if ($STATIC_LINK$)>
  150. #pragma  handler( selection )
  151. </if>
  152.  
  153.  
  154. /*----------------------------------------*/
  155. /* List the integers in an array.         */
  156. /*----------------------------------------*/
  157. <if ($EXPORT$)>
  158. void _Export list(int array[], size_t nNumElements)
  159. <else>
  160. void list(int array[], size_t nNumElements)
  161. </if>
  162. {
  163.    size_t i;
  164.    for (i = 0; i < nNumElements; ++i)
  165.       printf("%i ", array[i]);
  166.    putchar('\n');
  167.    return ;
  168. }
  169. <if ($STATIC_LINK$)>
  170. #pragma  handler( list )
  171. </if>
  172.