|
|||
|
As I've written before, it would be good if we could harvest
some of the eg. zillion linux C utilities. We know that p2c exists because Pascal is clean, and can therefore easily be translated to a clean/constrained subset of C. Only now did I stumble across this ctop12b.zip 110532 Apr 26 1989 Convert C code to Turbo Pascal, Knowledge Software ftp://garbo.uwasa.fi/pc/turbopas/ctop12b.zip [110KB] I was a heavy user of TurboPascal5.5 about 20 years ago, and when I now ran TP7 which I've got under DOS, I 'almost recognised the menus'. Since I've taken the trouble to fetch the CtoP and 'translate' the 1 of 2 provided C-sources. And I'm including the C-source & 2 resulting Pascal-translated 'units', perhaps some readers can test the TP sources, and post their results here for others to see I haven't yet looked at why there's an extra unit; i.e. 2 TP units for the single C-sources, since I'd need some effort to re-familiarise myself with TP. Here are the sources:------------------------ /* Sieve of Erastothenes */ #define UPTO 8192 #define ITERATIONS 10 #define YES 1 #define NO 0 int isprime[UPTO]; main() { int i, j, iterate; int count; printf("Start...\7\n"); for (iterate = 0; iterate < ITERATIONS; iterate++) { count = 0; /* initialise array to 'all prime' */ for (i = 0; i < UPTO; isprime[i++] = YES); for (i = 2; i < UPTO; i++) if (isprime[i]) { count++; /* 'cross out' all multiples of i: */ for (j = 2*i; j < UPTO; j += i) isprime[j] = NO; } } printf("Finish...\7 Found %d primes.\n", count); } ----------- { Translated by: CtoP version 1.2b From: Knowledge Software Ltd 32 Cove Rd, Farnborough, Hants, GU14 0EN, England Specialists in language translators and code generators sieveu.pas 18:25:04 6 Nov 2010 } Unit sieveu; {$N+ switch on 80x87 floating point types} Interface Const UPTO = 8192; YES = 1; ITERATIONS = 10; NO = 0; Type P_AChar0 = ^ A_Char_0; A_Char_0 = Packed Array [-2..2] Of Char; Function printf : Integer{?}; Implementation Function printf; Begin { dummy body } End; End. ----------- { Translated by: CtoP version 1.2b From: Knowledge Software Ltd 32 Cove Rd, Farnborough, Hants, GU14 0EN, England Specialists in language translators and code generators sieve.pas 18:25:04 6 Nov 2010 } Program sieve; {$N+ switch on 80x87 floating point types} Uses Ctopunit, sieveu; Var isprime : Array [0..UPTO-1] Of Integer { Used In Boolean Context }; { } { Sieve of Erastothenes } { } Procedure main; Var i : Integer; j : Integer; iterate : Integer; count : Integer; Begin { printf(StrAddr('Start...'^G''^J'')) } Writeln('Start...'^G''); iterate:=0; While (iterate<ITERATIONS) Do Begin count:=0; { initialise array to 'all prime' } i:=0; While (i<UPTO) Do Begin isprime[PoI_SInteger(i)]:=YES End { For }; i:=2; While (i<UPTO) Do Begin If (isprime[i]<>0) Then Begin count:=Succ(count); { 'cross out' all multiples of i: } j:=2*i; While (j<UPTO) Do Begin isprime[j]:=NO; j:=j+i End { For }; ; End { If }; i:=Succ(i) End { For }; ; iterate:=Succ(iterate) End { For }; { printf(StrAddr('Finish...'^G' Found %d primes.'^J''), count) } Writeln('Finish...'^G' Found ', count, ' primes.'); End; { main } Begin { sieve } Main; End. |
|
|
||||
|
||||
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|