The C code looks garbled in my news reader, so I'll post it one more time.
--
//---------------------------START OF C
CODE---------------------------------------
#include <string.h>
#include <memory.h>
#pragma hdrstop
//------------------------------------------------------------------------------------
typedef struct {
int len;
int node[];
}heap;
//----------------------------------------------------------------------------------------int
fLeft(int i){return (i << 1) + 1;}
//----------------------------------------------------------------------------------------
void Swap(int *i, int *j) { int k = *j; *j = *i; *i =
k;}//----------------------------------------------------------------------------------------
void Fix_heap(heap *hp, int i) { int m = fLeft(i); if (m < hp->len){
int h = m + 1; if (h < hp->len && hp->node[h] > hp->node[m]) m =
h; if (hp->node[m] > hp->node[i]) { Swap(hp->node+i,
hp->node+m); Fix_heap(hp, m); } } }
//---------------------------------------------------------------------------------------
int main(int argc, char *argv[]) { int i; int ary[]={9, 2, 10, 12,
7, 5, 8, 1, 3, 5}; heap *hp = (heap *) malloc(sizeof(int)*10); //9
elem. + len memcpy(hp, ary, sizeof(ary)); Fix_heap(hp, 0); for
(i=0; i<hp->len; i++) printf(" %i", hp->node[i]); getch(); return 0;
}//---------------------------------------------------------------------------"Anonymous"
<r1a@[EMAIL PROTECTED]
> skrev i
meldingnews:gYfUi.1519$Qr.763@[EMAIL PROTECTED]
>
Hello,>> I'm trying to convert some piece of C code to PB, but have some
trouble toconvert C memory manipulation code to its equivalent in PB,
which results isincorrect output; from the PB code.See below.>> Hopefully
someone could have some advice to share.> Thanks in advance!> -->>
#COMPILER PBCC 4.04> #COMPILE EXE> #DIM
ALL>'------------------------------------------------------------------------------------------>
TYPE HeapType> length AS LONG> node AS DWORD PTR> END
TYPE>'------------------------------------------------------------------------------------------>
FUNCTION fLeft(BYVAL i AS LONG)AS LONG> ****FT LEFT i,1> i = i + 1> fLeft
= i> END
FUNCTION>'------------------------------------------------------------------------------------------>
SUB hpSwap(BYVAL i AS DWORD PTR,BYVAL j AS DWORD PTR)> LOCAL k AS DWORD>
k = j> k= j> j = i> i = k> END
SUB>'------------------------------------------------------------------------------------------>
SUB Fix_Heap(BYVAL hp AS HeapType PTR,BYVAL i AS LONG)> REGISTER m AS
LONG,h AS LONG> m = fLeft(i)> IF m < @[EMAIL PROTECTED]
THEN> h = m + 1>
IF h < @[EMAIL PROTECTED]
AND @[EMAIL PROTECTED]
> @[EMAIL PROTECTED]
THEN m = h> IF
@[EMAIL PROTECTED]
> @[EMAIL PROTECTED]
> hpSwap @[EMAIL PROTECTED]
>
Fix_Heap hp,m> END IF> END IF> END
SUB>'-------------------------------------------------------------------------------------------->
SUB Fix_Heap_Test()> DIM hp AS LOCAL HeapType> LOCAL i AS LONG> DIM
ary(9) AS LOCAL LONG> ARRAY ASSIGN ary() = 9,2,10,12,7,5,8,1,3,5>
hp.node = VARPTR(ary(1))> hp.length = UBOUND(ary)-1> Fix_heap
VARPTR(hp),0> FOR i = 0 TO hp.length> ? hp.@[EMAIL PROTECTED]
'Output
:2,10,12,7,5,8,1,3,5> NEXT 'Should be:12,10,8, 7,5,2,1,3,5 as
in the C code.> END
SUB>'------------------------------------------------------------------------------------------>
FUNCTION PBMAIN () AS LONG>> Fix_Heap_Test()> WAITKEY$>> END FUNCTION>
'---------------------------END OF
PBCODE--------------------------------------->>
//---------------------------START OF
CCODE---------------------------------------> #include <string.h>>
#include <memory.h>> #pragma
hdrstop>//------------------------------------------------------------------------------------>
typedef struct {> int len;> int node[];>
}heap;>//----------------------------------------------------------------------------------------int
fLeft(int i){return (i << 1) +
1;}//----------------------------------------------------------------------------------------
void Swap(int *i, int *j) { int k = *j; *j = *i; *i
=k;}//----------------------------------------------------------------------------------------
void Fix_heap(heap *hp, int i) { int m = fLeft(i);if (m < hp->len){
int h = m + 1; if (h < hp->len && hp->node[h] >hp->node[m]) m = h;
if (hp->node[m] > hp->node[i]) {Swap(hp->node+i, hp->node+m);
Fix_heap(hp, m)
; } }
}//---------------------------------------------------------------------------------------
int main(int argc, char *argv[]) { int i; int ary[]={9,2, 10, 12, 7,
5, 8, 1, 3, 5}; heap *hp = (heap *) malloc(sizeof(int)*10);//9 elem. +
len memcpy(hp, ary, sizeof(ary)); Fix_heap(hp, 0); for(i=0;
i<hp->len; i++) printf(" %i", hp->node[i]); getch(); return;
}//--------------------------------------------------------------------------->


|