campo-sirio/gfm/dsort.c
luca ac8ecb0b9f Patch level :2.0 aga 370
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :sistemate le librerie gfm (allineate a partners version)


git-svn-id: svn://10.65.10.50/trunk@10750 c028cbd2-c16b-5b4b-a496-9718f37d4682
2003-01-17 12:20:25 +00:00

115 lines
2.0 KiB
C
Executable File

/* DEC **SortDecimal(dst, src, n, opt);
*
* ARGUMENT
* DEC **dst;
* DEC **src;
* int n;
* int opt;
*
* DESCRIPTION
* Sorts an array in increasing or decreasing order. The n elements
* of src are sorted and stored in dst. opt is GM_INC if the elements
* should be sorted in increasing order, and opt is GM_DEC is the elements
* should be sorted in decreasing order.
* A temporary copy of the array is allocated by this procedure.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* dst if successful, otherwise GM_NULL
*
* POSSIBLE ERRORS
* GM_NULLPOINTER
* GM_INIT
* GM_NOMEMORY
*
* AUTHOR
* Jared Levy
* Copyright (C) 1988-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*
*/
#include <stdio.h>
#include <malloc.h>
#include "gmsystem.h"
DEC **SortDecimalArray(dst, src, n, opt)
DEC **dst, **src;
int n, opt;
{
DEC *tmpd, **tmpa, *p;
int i;
extern void qsort(char *, unsigned, unsigned, int (*)());
_MacStart(GM_DSORT);
if (n<0|| (opt!=GM_INC && opt!=GM_DEC)) {
_MacErr(GM_ARGVAL);
_MacRet(GM_NULLARR);
}
if (!src||!dst) {
_MacErr(GM_NULLPOINTER);
_MacRet(GM_NULLARR);
}
if (n==0)
_MacRet(dst);
for (i=0;i<n;i++) {
_MacInVar(src[i], GM_NULLARR);
_MacOutVar(dst[i], GM_NULLARR);
}
tmpa = (DEC **) calloc(n, sizeof(DEC *));
if (!tmpa) {
_MacErr(GM_NOMEMORY);
_MacRet(GM_NULLARR);
}
tmpd = (DEC *) calloc(n, sizeof(DEC));
if (!tmpd) {
free ((char *) tmpa);
_MacErr(GM_NOMEMORY);
_MacRet(GM_NULLARR);
}
for (i=0;i<n;i++) {
p = &(tmpd[i]);
tmpa[i]=p;
_MacDCopy(p,(src[i]));
}
if (opt==GM_INC) /* increasing order */
qsort((void *)tmpa,n,sizeof(DEC *), _SortInc);
else /* decreasing order */
qsort((void *)tmpa,n,sizeof(DEC *), _SortDec);
for (i=0;i<n;i++)
_MacDCopy((dst[i]),(tmpa[i]));
free((char *) tmpd);
free((char *) tmpa);
_MacRet(dst);
}
int _SortInc(e1, e2)
DEC **e1;
DEC **e2;
{
return(CompareDecimal((DEC *)*e1, (DEC *)*e2));
}
int _SortDec(e1, e2)
DEC **e1;
DEC **e2;
{
return(CompareDecimal((DEC *)*e2, (DEC *)*e1));
}