You are on page 1of 7

#include <stdio.

h>
00003
00004 #include "dbl_sort.h"
00005 #include "histogram.h"
00006
00007
00011 void histogram::init_bins( bin_vec &bins,
00012 const double min,
00013 const double max )
00014 {
00015 size_t num_bins = bins.length();
00016 double range = max - min;
00017 double bin_size = range / static_cast<double>( num_bins );
00018
00019 double start = min;
00020 double end = min + bin_size;
00021
00022 for (size_t i = 0; i < num_bins; i++) {
00023 bins[i] = 0; // initialize frequency to zero
00024 bins.start(i, start ); // initialize the i'th bin start value
00025 bins.end(i, end ); // initialize the i'th bin end value
00026 start = end;
00027 end = end + bin_size;
00028 }
00029 // The frequency in a bin is incremented if a value v is
00030 // in the range start <= v < end. This is fine until
00031 // we reach the last bin, which should also get values
00032 // which are in the range start <= v <= end. So add a
00033 // small amount to the end value to assure that the
00034 // end value of the last bin is beyond the value range.
00035 bins.end(num_bins-1, bins.end(num_bins-1) + (bin_size / 10.0) );
00036 } // init_bins
00037
00038
00039
00052 void histogram::calculate( const double *raw_data,
00053 const size_t N,
00054 bin_vec &bins )
00055 {
00056 double *sort_data = new double[ N ];
00057
00058 for (size_t i = 0; i < N; i++) {
00059 sort_data[i] = raw_data[i];
00060 }
00061
00062 dbl_sort s;
00063
00064 s.sort( sort_data, N );
00065 double min = sort_data[0];
00066 double max = sort_data[N-1];
00067
00068 size_t num_bins = bins.length();
00069
00070 init_bins( bins, min, max );
00071
00072 value_pool pool( sort_data, N );
00073
00074 size_t bin_ix = 0;
00075
00076 double val;
00077 bool more_values = pool.get_val( val );
00078 double end = bins.end(bin_ix);
00079
00080 while (bin_ix < num_bins && more_values) {
00081 if (val < end) {
00082 bins[bin_ix] = bins[bin_ix] + 1; // increment the frequency
00083 more_values = pool.get_val( val );
00084 }
00085 else {
00086 bin_ix++;
00087 end = bins.end(bin_ix);
00088 }
00089 } // while
00090
00091 delete [] sort_data;
00092 } // calculate
00093
1. // PROGRAMME EN C SOUS DOS TURBO C++2 BORLAND
2.
3. // ecrit le 30-06-2002 par cmarsc
4.
5. // GRAPHIQUE EN HISTOGRAMME MODE GRAPHIQUE
6.
7.
8.
9. #include <graphics.h>
10.
11. #include <stdlib.h>
12.
13. #include <stdio.h>
14.
15. #include <conio.h>
16.
17. #include <time.h>
18.
19. #include <string.h>
20.
21. #include <dos.h>
22.
23.
24.
25. #define COUL_CADRE CYAN
26.
27. #define MAXI 300
28.
29. #define N_BARRES 12
30.
31.
32.
33. enum reponse1 { NON,OUI };
34.
35.
36.
37. int main(void) {
38.
39.
40.
41. int gdriver = DETECT, gmode, errorcode;
42.
43.
44.
45. int i,x;
46.
47. int y1[N_BARRES], reponse[N_BARRES];
48.
49. int graph1[N_BARRES], graph1_precedent[N_BARRES];
50.
51. char texte[N_BARRES][5];
52.
53.
54.
55. initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
56.
57.
58.
59. errorcode = graphresult();
60.
61. if (errorcode != grOk)
62.
63. {
64.
65. printf("Graphics error: %s\n", grapherrormsg(errorcode));
66.
67. printf("Press any key to halt:");
68.
69. getch();
70.
71.
72.
73. exit(1);
74.
75. }
76.
77.
78.
79. srand( (unsigned) time(NULL) );
80.
81. memset(texte, '\0', N_BARRES);
82.
83.
84.
85. // initialiser a zero
86.
87. for (i = 0; i < N_BARRES; i++) {
88.
89. y1[i] = 0; reponse[i] = OUI;
90.
91. graph1[i] = 0; graph1_precedent[i] = 0;
92.
93. }
94.
95.
96.
97. // grand cadre
98.
99. setfillstyle(1,COUL_CADRE);
100.
101. bar (1, 00,getmaxx(), 355);
102.
103.
104.
105. setcolor(BLACK);
106.
107. rectangle(2 ,20 , getmaxx()-1,355 );
108.
109.
110.
111. setcolor(WHITE);
112.
113. rectangle(1 ,22 , getmaxx(),356+00 );
114.
115.
116.
117. settextstyle(SMALL_FONT, HORIZ_DIR, 5);
118.
119. sprintf(texte[0],"Projections des ventilations : 2003");
120.
121.
122.
123. // effet d'ombre
124.
125. setcolor(BLACK);
126.
127. outtextxy(180 ,1,texte[0]);
128.
129.
130.
131. // texte en rouge
132.
133. setcolor(RED);
134.
135. outtextxy(181 ,1,texte[0]);
136.
137.
138.
139. // elargir le texte en longueur * 3 et en hauteur * 10
140.
141. setusercharsize(3, 1,10, 1);
142.
143.
144.
145. setfillstyle(1,CYAN);
146.
147. bar (1,375,getmaxx() , getmaxy()-15);
148.
149. setcolor(YELLOW);
150.
151. rectangle (1,375,getmaxx() , getmaxy()-15);
152.
153.
154.
155. setcolor(LIGHTCYAN);
156.
157. sprintf(texte[0],"APPUYEZ SUR UNE TOUCHE POUR QUITTER");
158.
159. outtextxy(12 ,360,texte[0]);
160.
161.
162.
163. setcolor(BLACK);
164.
165. // faire un effet 3D
166.
167. for (i = 0; i < 2;i++) {
168.
169. outtextxy(13 +i,360+i,texte[0]);
170.
171. }
172.
173.
174.
175.
176.
177. while( !kbhit() ) {
178.
179. // x = 52 ecart entre les barres
180.
181. for (i = 0, x = 5 ; i < N_BARRES; i++, x +=52) {
182.
183.
184.
185. // determiner la hauteur de la barre
186.
187. if (reponse[i] == OUI) {
188.
189.
190.
191. // garder la valeur de graph1[i] precedente
192.
193. graph1_precedent[i] = graph1[i];
194.
195. graph1[i] = (rand() % MAXI) +1;
196.
197.
198.
199. // effacer le chiffre en blanc sous la barre
200.
201. setfillstyle(1, COUL_CADRE);
202.
203. bar (15 + x,333 , 50 + x,350 );
204.
205.
206.
207. // convertir le nombre tire
208.
209. sprintf(texte[i],"%3d",graph1[i]);
210.
211.
212.
213. // afficher ce nombre sous la barre
214.
215. settextstyle(SMALL_FONT, HORIZ_DIR, 6);
216.
217. setcolor(BLACK);
218.
219. outtextxy(15 + x,331,texte[i]);
220.
221.
222.
223. setcolor(WHITE);
224.
225. outtextxy(16 + x,332,texte[i]);
226.
227.
228.
229. }
230.
231.
232.
233. if ( graph1[i] > graph1_precedent[i]) {
234.
235.
236.
237. // faire monter la barre
238.
239. if (y1[i] < graph1[i]) {
240.
241.
242.
243. y1[i]++; reponse[i] = NON;
244.
245.
246.
247. } else if (y1[i] == graph1[i]) {
248.
249.
250.
251. // choisir un autre chiffre
252.
253. reponse[i] = OUI;
254.
255. }
256.
257.
258.
259. } else {
260.
261.
262.
263. // faire descendre la barre
264.
265. y1[i]--; reponse[i] = NON;
266.
267. if (y1[i] == graph1[i] || y1[i] <= 0 ) reponse[i] = OUI;
268.
269. }
270.
271.
272.
273. setcolor(COUL_CADRE);
274.
275. rectangle(8 + x ,326 - y1[i] , 52 + x,327- y1[i] );
276.
277.
278.
279. // dessiner les 2 traits sur les cotes
280.
281.
282.
283. // trait rouge vif a gauche
284.
285. setcolor(LIGHTRED);
286.
287. rectangle(8 + x,330 - y1[i] , 9 + x,330 );
288.
289.
290.
291. // barre au milieu en rouge
292.
293. setcolor(RED);
294.
295. rectangle(10 + x,330 - y1[i] , 50 + x,330 );
296.
297.
298.
299. // trait noir a droite
300.
301. setcolor(BLACK);
302.
303. rectangle(51 + x,330 - y1[i] ,52 + x,330 );
304.
305.
306.
307. } // fin de for (i)
308.
309.
310.
311. } // fin de while
312.
313.
314.
315.
316.
317. closegraph();
318.
319. return 0;
320.
321.
322.
323. } // fin de main
324.

You might also like