home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / smrtsort / sample3.c < prev    next >
Text File  |  1995-07-18  |  3KB  |  98 lines

  1. /*********************************************************/
  2. /*                                                        */ 
  3. /*  Licensed Material -- Property of IBM                  */    
  4. /*  5765-349                                              */
  5. /*  (c) Copyright IBM Corporation 1995                    */
  6. /*  All rights reserved                                   */
  7. /*                                                        */
  8. /**********************************************************/
  9.  
  10. /**********************************************************/
  11. /*                                                        */
  12. /* SMARTsort -- sample3.c                                 */
  13. /*                                                        */
  14. /* This program shows how to use SMARTsort to sort an     */
  15. /* array of structures defined in memory.                 */
  16. /*                                                        */
  17. /**********************************************************/
  18.  
  19. #pragma linkage (SMARTsort,system)
  20. #include <string.h>
  21. #include "smrtsort.h"
  22.  
  23. typedef struct INVENTORY {
  24.                           int id;
  25.                           int stockonhand;
  26.                           float price;
  27.                           char itemname[12];
  28.                          } INVENTORY;
  29.  
  30. void main(void)
  31. {
  32.  INVENTORY *health_foods;
  33.  INVENTORY *by_quantity;
  34.  SMARTSORT_BUFFER source[2], target[2];
  35.  char sortcommand[100] = "-s sort ";
  36.  int rc;
  37.  int numberofitems;
  38.   
  39.  /* Create inventory list */
  40.  health_foods=(INVENTORY *) malloc( sizeof(INVENTORY)*10 );
  41.  health_foods[0].id=1;
  42.  health_foods[0].stockonhand=12;
  43.  health_foods[0].price=8.75;
  44.  strcpy(health_foods[0].itemname,"Oranges");
  45.  health_foods[1].id=2;
  46.  health_foods[1].stockonhand=45;
  47.  health_foods[1].price=15.00;
  48.  strcpy(health_foods[1].itemname,"Raspberries");
  49.  health_foods[2].id=3;
  50.  health_foods[2].stockonhand=10;
  51.  health_foods[2].price=0.99;
  52.  strcpy(health_foods[2].itemname,"Pears");
  53.  health_foods[3].id=4;
  54.  health_foods[3].stockonhand=16;
  55.  health_foods[3].price=4.50;
  56.  strcpy(health_foods[3].itemname,"Tangerines");
  57.  health_foods[4].id=5;
  58.  health_foods[4].stockonhand=36;
  59.  health_foods[4].price=2.99;
  60.  strcpy(health_foods[4].itemname,"Grapes");
  61.  health_foods[5].id=6;
  62.  health_foods[5].stockonhand=12;
  63.  health_foods[5].price=1.00;
  64.  strcpy(health_foods[5].itemname,"Apples");
  65.  numberofitems=6;
  66.  
  67.  /* Define structure of data for SMARTsort */
  68.  strcat(sortcommand,"-x 'format int int float char 12'");
  69.  
  70.  /* Indicate to sort by stock on hand */
  71.  strcat(sortcommand," -k 2,2");
  72.  
  73.  /* Allocate space for results of sort */
  74.  by_quantity=(INVENTORY *) malloc( sizeof(INVENTORY)*10 );
  75.  
  76.  /* Initialize SMARTsort source and target structures */
  77.  source[0].buffer=(char *) health_foods;
  78.  source[0].buffer_size=sizeof(INVENTORY)*numberofitems;
  79.  source[0].nbytes_used=NULL;
  80.  source[1].buffer=NULL;
  81.  source[1].buffer_size=0;
  82.  source[1].nbytes_used=NULL;
  83.  target[0].buffer=(char *) by_quantity;
  84.  target[0].buffer_size=sizeof(INVENTORY)*10;
  85.  target[0].nbytes_used=NULL;
  86.  target[1].buffer=NULL;
  87.  target[1].buffer_size=0;
  88.  target[1].nbytes_used=NULL;
  89.  
  90.  /* Call SMARTsort to sort the structures */
  91.  rc=SMARTsort(sortcommand,source,target,NULL,NULL,NULL,NULL);
  92.  
  93.  /* Clean up and get out */
  94.  free(health_foods);
  95.  free(by_quantity);
  96.  
  97. }
  98.