Mercurial > hg > Members > kono > nitros9-code
comparison docs/ccguide/basic09.appendix @ 565:0e84dcd81835
Several functions ready.
author | roug |
---|---|
date | Thu, 31 Oct 2002 22:05:32 +0000 |
parents | 7d803625ead8 |
children | 594a34d027f8 |
comparison
equal
deleted
inserted
replaced
564:1f4a17c1c56d | 565:0e84dcd81835 |
---|---|
108 5 136 -1029 | 108 5 136 -1029 |
109 </screen> | 109 </screen> |
110 indicating that our C function worked! | 110 indicating that our C function worked! |
111 </para> | 111 </para> |
112 </section> | 112 </section> |
113 | |
114 <section> | |
115 <title>Example 2 - More Complex Integer Aritmetic Case</title> | |
116 <para> | |
117 </para> | |
118 </section> | |
119 | |
120 <section> | |
121 <title>Example 3 - Simple String Manipulation</title> | |
122 <para> | |
123 </para> | |
124 </section> | |
125 | |
126 <section> | |
127 <title>Example 4 - Quicksort</title> | |
128 <para> | |
129 The next example programs demonstrate how one might implement a | |
130 quicksort written in C to sort some BASIC09 data. | |
131 </para> | |
132 <para> | |
133 C integer quicksort program: | |
134 </para> | |
135 <programlisting> | |
136 #define swap(a,b) { int t; t=a; a=b; b=t; } | |
137 | |
138 /* qsort to be called by BASIC09: | |
139 dim d(100):INTEGER any size INTEGER array | |
140 run cqsort(d,100) calling qsort. | |
141 */ | |
142 | |
143 qsort(argcnt,iarray,iasize,icount,icsiz) | |
144 int argcnt, /* BASIC09 argument count */ | |
145 iarrary[], /* Pointer to BASIC09 integer array */ | |
146 iasize, /* and it's size */ | |
147 *icount, /* Pointer to BASIC09 (sort count) */ | |
148 icsiz; /* Size of integer */ | |
149 { | |
150 sort(iarray,0,*icount); /* initial qsort partition */ | |
151 } | |
152 | |
153 /* standard quicksort algorithm from Horowitz-Sahni */ | |
154 static sort(a,m,n) | |
155 register int *a,m,n; | |
156 { | |
157 register i,j,x; | |
158 | |
159 if(m < n) { | |
160 i = m; | |
161 j = n + 1; | |
162 x = a[m]; | |
163 for(;;) { | |
164 do i += 1; while(a[i] < x); /* left partition */ | |
165 do j -= 1; while(a[j] > x); /* right partition */ | |
166 if(i < j) | |
167 swap(a[i],a[j]) /* swap */ | |
168 else break; | |
169 } | |
170 swap(a[m],a[j]); | |
171 sort(a,m,j-1); /* sort left */ | |
172 sort(a,j+1,n); /* sort right */ | |
173 } | |
174 } | |
175 </programlisting> | |
176 <para> | |
177 The BASIC09 program is: | |
178 </para> | |
179 <programlisting> | |
180 PROCEDURE sorter | |
181 DIM i,n,d(1000):INTEGER | |
182 n=1000 | |
183 i=RND(-(PI)) | |
184 FOR i=1 to n | |
185 d(i):=INT(RND(1000)) | |
186 NEXT i | |
187 PRINT "Before:" | |
188 RUN prin(1,n,d) | |
189 RUN qsortb(d,n) | |
190 PRINT "After:" | |
191 RUN prin(1,n,d) | |
192 END | |
193 | |
194 PROCEDURE prin | |
195 PARAM n,m,d(1000):INTEGER | |
196 DIM i:INTEGER | |
197 FOR i=n TO m | |
198 PRINT d(i); " "; | |
199 NEXT i | |
200 PRINT | |
201 END | |
202 </programlisting> | |
203 </section> | |
113 </appendix> | 204 </appendix> |