Bonjour

J'ai quelques petits problèmes avec mon programme de gestion bancaire:
pas de problème quand je regarde la liste des comptes de suite après les avoir encodés mais si je ferme le programme et le re-ouvre on dirait qu'il lit hors tableau et je ne comprends pas pourquoi.
Il n'y a pas moyen non plus de voir mes virements quand je fais lister_virements on dirait également qu'il lit des valeurs un peu n'importe où en mémoire.
Est-ce quelqu'un peut m'aider? Je comprendrais que non vu la longueur du code mais je sèche depuis quelques jours et à force de le relire je suis sûr que j'ai le nez dessus et que je ne le vois plus.
En espérant que quelqu'un aie assez de courage pour se lancer dans mes bêtises je vous remercie d'avance.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
 
#include <assert.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <alloc.h>
#include <time.h>
#define MAX_ITEM_SIZE 70
 
struct numero
{
	int	bank_id;
	long num;
	int	check_digit;
};
 
struct compte
{
	int index;
	struct numero num_de_cpt;
	double solde;
	char type; //epargne ou courant
	struct numero num_cpt_ass;
	double minimum;
	char flag_effacement;
	int efface;
	char flag_bloque;
};
 
struct virement
{
	struct	numero	num_compte_don;
	struct	numero	num_compte_bene;
	double			montant;
	struct	tm		*date_creation;
	struct	tm		*date_execution;
	char			commu[50];
	char			flag_execution;
};
 
struct compte_index
{
	struct numero num_de_cpt;
	int position;
	char type;
};
 
struct op
{
	struct	numero	num_compte_don;
	struct	numero	num_compte_bene;
	double			montant;
	struct tm		*date_paye;
	struct tm		*date_fin;
	char			commu[50];
	char			flag_effacement;
};
 
struct historique
{
	struct	numero	num_compte_bene;
	struct	tm		*date;
	char			signe;
	double			montant;
	char			mouvement[16];
	struct	numero	num_compte_don;
	char			commu[50];
};
 
void aff_compte(struct compte);
void aff_form_compte(char[]);
void aff_form_op(char []);
void aff_form_virement(char titre[]);
void aff_histo (long);
void aff_message(char []);
void aff_op (struct	op);
void aff_virements (struct virement);
int  cherche_compte(long);
void cree_op();
void cree_virement();
void debloquer_compte(int);
long demander_num_cpt();
void depot();
int encodage_check_digit(struct numero, int, int);
struct numero encodage_cpt_ass();
struct numero encodage_cpte_bene(struct compte);
struct numero encodage_cpte_don();
struct tm encodage_date(int, int);
long encodage_num_cpt(int, int);
char exec_choix(char [], char []);
void exec_cree_compte();
char exec_menu(char [][MAX_ITEM_SIZE], int, char []);
int fct_check_digit(int, long);
//void historique(struct compte, struct compte, double, char []);
void info_cpt(int);
void lister_comptes (int);
void lister_virements();
void menu_consultations(int, long);
void menu_comptes();
void menu_main();
void menu_op();
void menu_ouverture();
void menu_virements();
void ouverture_fic_op();
void ouverture_fic_virement();
void ouverture_historique();
void sauvegarde(struct compte, struct compte_index);
void sauvegarde_op(struct op);
void sauvegarde_vir(struct virement);
void suppression_compte();
void suppression_op();
void trie_index();
void virements_en_attente(long);
 
/*====================================================================*
* Déclaration de variables globales                                   *
*====================================================================*/
 
 
int nbre_enreg; /* sans ceux supprimés */
int nbre_vrais_enreg; /* avec ceux supprimés */
int nbre_virements;
int nbre_op;
int nbre_historiques;
FILE * ptr_flux;
FILE * ptr_flux_histo;
FILE * ptr_flux_index;
FILE * ptr_flux_error;
FILE * ptr_flux_op;
FILE * ptr_flux_virements;
struct compte_index *ptr_tab;
 
void main ()
{
	int indice;
	menu_ouverture();
	fclose(ptr_flux);
	if(nbre_enreg>0)
	{
		ptr_flux_index = fopen("index.dat", "w+b");
 
		fseek(ptr_flux_index, 0, SEEK_SET);
		for(indice=0;indice<nbre_enreg;indice++)
			fwrite(&ptr_tab[indice], sizeof(struct compte_index), 1, ptr_flux_index);
		fclose(ptr_flux_index);
	}
	free(ptr_tab);
}
 
 
/*====================================================================*
* aff_compte  : Affiche les données d'un compte                       *
*====================================================================*/
 
void aff_compte(struct compte cpte)
{
	aff_form_compte("Données du compte");
	gotoxy(29, 5); printf("%d", cpte.num_de_cpt.bank_id);
	gotoxy(33, 5); printf("%ld", cpte.num_de_cpt.num);
	gotoxy(41, 5); printf("%d", cpte.num_de_cpt.check_digit);
	gotoxy(18, 7); printf("%f", cpte.solde);
	gotoxy(27, 9);
	switch(cpte.type)
	{
	case 'C': 	puts("Courant");
		break;
	case 'E': 	puts("Epargne");
		gotoxy(27, 11);	printf("%d", cpte.num_cpt_ass.bank_id);
		gotoxy(31, 11); printf("%ld", cpte.num_cpt_ass.num);
		gotoxy(39, 11); printf("%d", cpte.num_cpt_ass.check_digit);
		break;
	}
	gotoxy(30, 13);	printf("%f", cpte.minimum);
 
}
 
/*====================================================================*
* aff_form_compte  : Affiche le formulaire pour un compte             *
*====================================================================*/
 
void aff_form_compte(char titre[])
{
	int long_titre,
	cptr;
	clrscr();
	long_titre = strlen(titre);
	gotoxy(40 - long_titre / 2, 1); puts(titre);
	gotoxy(1, 2);
	for (cptr = 0 ; cptr < 80 ; cptr++)
	printf("=");
	gotoxy(10, 5); printf("Numéro de compte : ...-.......-.. ");
	gotoxy(10, 7); printf("Solde : ");
	gotoxy(10, 9); printf("Type de compte : ");
	gotoxy(10, 11); printf("Compte associé : ...-.......-..");
	gotoxy(10, 13); printf("Minimum : ");
 
}
 
/*====================================================================*
* aff_form_op  : Affiche le formulaire pour un ordre permanent        *
*====================================================================*/
 
void aff_form_op(char titre[])
{
	int long_titre, cptr;
 
	clrscr();
	long_titre = strlen(titre);
	gotoxy(40 - long_titre / 2, 1); puts(titre);
	gotoxy(1, 2);
	for (cptr = 0 ; cptr < 80 ; cptr++)	printf("=");
	gotoxy(10, 5);	printf("Numéro du donneur d'ordre : ...-.......-..");
	gotoxy(10, 7);	printf("Montant à verser : ..............................");
	gotoxy(10, 9);	printf("Numéro du bénéficiaire : ...-.......-..");
	gotoxy(10, 11);	printf("Date de début : ../../....");
	gotoxy(10, 13); printf("Date de fin :   ../../....");
	gotoxy(10, 15);	printf("Communication : .................................");
}
 
/*====================================================================*
* aff_form_virement  : Affiche le formulaire pour un virement         *
*====================================================================*/
 
void aff_form_virement(char titre[])
{
	int long_titre, cptr;
 
	clrscr();
	long_titre = strlen(titre);
	gotoxy(40 - long_titre / 2, 1); puts(titre);
	gotoxy(1, 2);
	for (cptr = 0 ; cptr < 80 ; cptr++)	printf("=");
	gotoxy(10, 5);	printf("Numéro du donneur d'ordre : ...-.......-..");
	gotoxy(10, 7);	printf("Montant à verser : ");
	gotoxy(10, 9);	printf("Numéro du bénéficiaire : ...-.......-..");
	gotoxy(10, 11);	printf("Date de création : ../../....");
	gotoxy(10, 13); printf("Date d'exécution : ../../....");
	gotoxy(10, 15);	printf("Communication : ");
}
 
/*====================================================================*
* aff_histo  : Affiche l'historique des mouvements pour un compte     *
*====================================================================*/
 
void aff_histo(long num)
{
	struct	historique histo;
	int	cpt, long_titre;
	char titre[]="HISTORIQUE DU COMPTE";
 
	clrscr();
	ouverture_historique();
 
	long_titre = strlen(titre);
	gotoxy(40 - long_titre / 2, 1); puts(titre);
	gotoxy(1, 2);
	for (cpt = 0 ; cpt < 80 ; cpt++)	printf("=");
 
	fseek(ptr_flux_histo, 0, SEEK_SET);
	fread(&histo, sizeof(struct historique), 1, ptr_flux_histo);
	while(!feof(ptr_flux_histo))
	{
		if (num==histo.num_compte_don.num)
		{
			//asctime(hist.date_mouv);	printf("\n");
			printf("%c%.2f -- %s -- %d-%ld-%d\n", histo.signe, histo.montant,
											histo.mouvement,
											histo.num_compte_don.bank_id,
											histo.num_compte_don.num,
											histo.num_compte_don.check_digit);
			puts(histo.commu);
		}
		fread(&histo, sizeof(struct historique), 1, ptr_flux_histo);
	}
	aff_message(" ");
	fclose(ptr_flux_histo);
}
 
/*====================================================================*
* aff_op  : Affiche les informations d'un ordre permanement           *
*====================================================================*/
 
void aff_op (struct	op ord_p)
{
	gotoxy(38, 5);	puts("667");
	gotoxy(42, 5);	printf("%ld", ord_p.num_compte_don.num);
	gotoxy(50, 5);	printf("%d", ord_p.num_compte_don.check_digit);
	gotoxy(29, 7);	printf("%f", ord_p.montant);
	gotoxy(35, 9);	printf("%d", ord_p.num_compte_bene.bank_id);
	gotoxy(39, 9);	printf("%ld", ord_p.num_compte_bene.num);
	gotoxy(47, 9);	printf("%d", ord_p.num_compte_bene.check_digit);
	gotoxy(26, 15);	printf("%s", ord_p.commu);
 
}
 
/*====================================================================*
* aff_virements  : Affiche les informations d'un virement             *
*====================================================================*/
 
void aff_virements(struct	virement vir)
{
	gotoxy(38, 5);	puts("667");
	gotoxy(42, 5);	printf("%ld", vir.num_compte_don.num);
	gotoxy(50, 5);	printf("%d", vir.num_compte_don.check_digit);
	gotoxy(29, 7);	printf("%f", vir.montant);
	gotoxy(35, 9);	printf("%d", vir.num_compte_bene.bank_id);
	gotoxy(39, 9);	printf("%ld", vir.num_compte_bene.num);
	gotoxy(47, 9);	printf("%d", vir.num_compte_bene.check_digit);
	/*gotoxy(29,11);	printf("%s", asctime(vir.date_cree));
	gotoxy(29,13);	printf("%s", asctime(vir.date_exec));   */
	gotoxy(26,15);	printf("%s", vir.commu);
}
 
/*====================================================================*
* aff_message  : Affiche un message passé en paramètre                *
* et attend la pression d'une touche                                  *
*====================================================================*/
 
void aff_message(char message[])
{
	int cptr;
	gotoxy(1,23); for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
	gotoxy(1,24); for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
	gotoxy(40 - strlen(message) / 2 , 23); puts(message);
	gotoxy(20, 24); puts("-- Presser une touche pour continuer --"); getch();
	gotoxy(1,23); for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
	gotoxy(1,24); for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
}
 
/*====================================================================*
* cherche_compte  : Renvoit la position du compte dans le fichier     *
* ou -1 si celui ci ne s'y trouve pas                                 *
*====================================================================*/
 
int  cherche_compte(long numero)
{
	int result_final =-1;
	 int milieu;
	int trouve=0;
	int debut=0;
	int fin=nbre_enreg-1;
	 while (debut <= fin && trouve==0)
	 {
		milieu = (debut + fin) / 2;
			if (ptr_tab[milieu].num_de_cpt.num > numero)
			{
				fin = milieu - 1;
			} else {
 
				if(ptr_tab[milieu].num_de_cpt.num == numero)
				{
					result_final=ptr_tab[milieu].position;
					trouve=1;
				}
				else
					debut = milieu + 1;
			}
	}
	return result_final;
}
 
/*====================================================================*
* cree_op: Crée et enregistre un ordre permanent                      *
*====================================================================*/
 
void cree_op()
{
	struct op ord_p;
	struct tm time_check;
	char buffer[8];
	struct compte cpte;
	int pos=0;
 
	ouverture_fic_op();
	aff_form_op("Création d'un ordre permanant");
	ord_p.num_compte_don=encodage_cpte_don();
 
	gotoxy(50,5); printf("%d", ord_p.num_compte_don.check_digit);
	gotoxy(29,7); ord_p.montant=atof(gets(buffer));
	pos=cherche_compte(ord_p.num_compte_don.num);
 
	fseek(ptr_flux, pos*sizeof(struct compte), SEEK_SET);
	fread(&cpte, sizeof(struct compte), 1, ptr_flux);
	encodage_cpte_bene(cpte);
 
	time_check=encodage_date(26, 11);
 
	if (mktime(&time_check)==-1) time_check.tm_wday=7;
	ord_p.date_paye=&time_check;
	gotoxy(26,11);	printf("%s", asctime(ord_p.date_paye));
 
	time_check=encodage_date(26, 13);
 
	if (mktime(&time_check)==-1) time_check.tm_wday=7;
	ord_p.date_fin=&time_check;
	gotoxy(26,13);	printf("%s", asctime(ord_p.date_fin));
	gotoxy(26,15);	gets(ord_p.commu);
	ord_p.flag_effacement='N';
	sauvegarde_op(ord_p);
}
 
/*====================================================================*
* cree_virement: Crée et enregistre un virement                       *
*====================================================================*/
 
void cree_virement()
{
	struct	virement	vir;
	struct	compte		cpte;
	struct	tm			time_check;
	char				buffer[8];
	int					pos;
	time_t 				timer;
 
	ouverture_fic_virement();
	aff_form_virement("Création d'un virement");
	vir.num_compte_don=encodage_cpte_don();
	gotoxy(29,7); vir.montant=atof(gets(buffer));
	pos=cherche_compte(vir.num_compte_don.num);
	fseek(ptr_flux, pos*sizeof(struct compte), SEEK_SET);
	fread(&cpte, sizeof(struct compte), 1, ptr_flux);
	vir.num_compte_bene=encodage_cpte_bene(cpte);
 
	timer=time(NULL); vir.date_creation=localtime(&timer);
	gotoxy(29,11);	printf("%s", asctime(vir.date_creation));
	time_check=encodage_date(29, 13);
	if (mktime(&time_check)==-1) time_check.tm_wday=7;
	vir.date_execution=&time_check;
	gotoxy(29,13);	printf("%s", asctime(vir.date_execution));
	gotoxy(26,15);	gets(vir.commu);
	vir.flag_execution='N';
	sauvegarde_vir(vir);
}
 
/*====================================================================*
* debloquer_compte: Permet de débloquer le compte se trouvant en pos  *
*====================================================================*/
 
void debloquer_compte(int pos)
{
	char reponse;
	struct compte cpte;
	clrscr();
	aff_form_compte("DEBLOQUER UN COMPTE");
	fseek(ptr_flux, pos*(sizeof(struct compte)), SEEK_SET);
	fread(&cpte, sizeof(struct compte), 1, ptr_flux);
 
		if (cpte.flag_bloque=='O')
		{
			aff_compte(cpte);
			reponse=exec_choix("<D>ébloquer - <A>bandonner", "DA");
			if (reponse == 'D')
			{
				cpte.flag_bloque = 'N';
				fseek(ptr_flux, pos*(sizeof(struct compte)), SEEK_SET);
				fwrite(&cpte, sizeof(struct compte), 1, ptr_flux);
			}
		}
		else
			aff_message("Compte non bloqué");
 
}
 
/*====================================================================*
* demander_num_cpt: Demande l'entrée d'un numéro de compte qui existe *
*====================================================================*/
 
long demander_num_cpt()
{
	long num;
	char buffer[15];
	do
	{
	clrscr();
	gotoxy(5, 10);
	printf("Entrez le numéro de compte sans les 2 derniers chiffres:\n");
	gotoxy(5, 14);
	printf("667-");
	num = atol(gets(buffer));
	if ((num < 0)||(num >= 10000000))
			aff_message("Numero incorrect");
	if(cherche_compte(num)==-1)
			aff_message("Compte n'existe pas");
 
	}while((num < 0)||(num >= 10000000)
			||(cherche_compte(num)==-1));
 
	return num;
 
}
 
/*====================================================================*
* depot: permet d'effectuer un dépôt                                  *
*====================================================================*/
 
 
void depot()
{
	char			buffer[15];
	int			pos;
	long 			numero;
	struct	compte	cpte;
	char 			reponse;
	double			montant=0.;
 
	clrscr();
 
	aff_form_compte("Vers quel compte effectuer le dépôt ?");
	gotoxy(29,5);	puts("667");
	gotoxy(33, 5);	numero = atol(gets(buffer));
	if((numero<=0 || numero>=10000000)||((pos=cherche_compte(numero))==-1))
		aff_message("Ce compte n'existe pas");
	else
	{
		fseek(ptr_flux, pos*(sizeof(struct compte)), SEEK_SET);
		fread(&cpte, sizeof(struct compte), 1, ptr_flux);
		aff_compte(cpte);
		while(montant<=0)
		{
			printf("\n\nMontant du dépôt : "); 
			montant=atof(gets(buffer));
		}
		printf("\n\nAncien solde : %f ", cpte.solde);
		cpte.solde+=montant;
		printf("\n\nNouveau solde : %f ", cpte.solde);
		reponse = exec_choix("<S>auver - <A>bandonner","SA");
		if (reponse == 'S')
		{
			fseek(ptr_flux, pos*(sizeof(struct compte)), SEEK_SET);
			fwrite(&cpte, sizeof(struct compte), 1, ptr_flux);
			//historique(cpte, cpte, montant, "dépot");
		}
	}
 
}
 
/*====================================================================*
* encodage_check_digit: permet d'encoder un check digit correctement  *
*====================================================================*/
 
int encodage_check_digit(struct numero num_cpt, int x, int y)
{
	char buffer[15];
	gotoxy(x, y);	puts("-..");
	gotoxy(x+1, y);
	num_cpt.check_digit = atoi(gets(buffer));
	while((num_cpt.check_digit < 0)
	||(num_cpt.check_digit >= 100)
	||num_cpt.check_digit!=fct_check_digit(num_cpt.bank_id, num_cpt.num) )
	{
		if ((num_cpt.check_digit < 0)||(num_cpt.check_digit >= 100))
		aff_message("Check Digit trop long ou négatif" );
		else
		aff_message("Check Digit incorrect");
		gotoxy(x, y);	puts("-..");
		gotoxy(x+1, y);	num_cpt.check_digit = atoi(gets(buffer));
	}
	return  num_cpt.check_digit;
 
}
 
/*====================================================================*
* encodage_cpt_ass: permet l'encodage d'un compte associé             *
*====================================================================*/
 
struct numero encodage_cpt_ass()
{
	struct compte cpte2;
	struct numero num_cpt_ass;
	int pos;
 
	do
		{
			gotoxy(27, 11); puts("667"); num_cpt_ass.bank_id = 667;
			num_cpt_ass.num=encodage_num_cpt(31, 11);
 
			pos=cherche_compte(num_cpt_ass.num);
			num_cpt_ass.check_digit=encodage_check_digit(num_cpt_ass, 38, 11);
			if(pos!=-1)
			{
				fseek(ptr_flux, pos*sizeof(struct compte), SEEK_SET);
				fread(&cpte2, sizeof(struct compte), 1, ptr_flux);
			}
 
		}while(pos==-1 || cpte2.type != 'C') ;
 
	return num_cpt_ass;
 
}
 
/*====================================================================*
* encodage_cpte_bene: permet l'encodage d'un compte bénéficiaire      *
*====================================================================*/
 
struct numero encodage_cpte_bene(struct compte cpte)
{
		struct numero num_cpte_bene;
		char buffer[15];
	if (cpte.type=='C')
	{
			gotoxy(35,9); puts("...-.......-..");
			gotoxy(35,9); puts("667"); num_cpte_bene.bank_id=667;
			gotoxy(39,9); num_cpte_bene.num=atol(gets(buffer));
			num_cpte_bene.check_digit=encodage_check_digit(num_cpte_bene, 46, 9);
	}
	else
	{
		gotoxy(35,9); num_cpte_bene=cpte.num_cpt_ass;
		printf("%d-%ld-%d", cpte.num_cpt_ass.bank_id, cpte.num_cpt_ass.num,
							cpte.num_cpt_ass.check_digit);
	}
	return num_cpte_bene;
}
 
/*====================================================================*
* encodage_cpte_don: permet l'encodage d'un compte donneur d'ordre    *
*====================================================================*/
 
struct numero encodage_cpte_don()
{
		struct numero num_cpte;
		char buffer[15];
		gotoxy(38,5); puts("667"); num_cpte.bank_id=667;
		gotoxy(42,5); num_cpte.num=atol(gets(buffer));
		while((num_cpte.num < 0)||(num_cpte.num >= 10000000)
			||(cherche_compte(num_cpte.num))==-1)
		{
			aff_message("Ce compte n'existe pas");
			gotoxy(42, 5);	puts(".......-..");
			gotoxy(42, 5);	num_cpte.num=atol(gets(buffer));
		}
 
		num_cpte.check_digit=encodage_check_digit(num_cpte, 49, 5);
		return num_cpte;
}
 
/*====================================================================*
* encodage_date: permet l'encodage d'une date                         *
*====================================================================*/
 
struct tm encodage_date(int x, int y)
{
		char buffer[10];
		struct tm time_check;
 
		int	jour, mois, annee;
		gotoxy(x,y);	jour=(atoi(gets(buffer)));
		gotoxy(x+3,y);	mois=(atoi(gets(buffer))-1);
		gotoxy(x+6,y);	annee=(atoi(gets(buffer))-1900);
		time_check.tm_year = annee;			 time_check.tm_mon  = mois;
		time_check.tm_mday = jour;			    time_check.tm_hour = 0;
		time_check.tm_min  = 0;				    time_check.tm_sec  = 1;
		time_check.tm_isdst = -1;
		return time_check;
}
 
 
/*====================================================================*
* encodage_num_cpt: permet l'encodage d'un numero de compte           *
*====================================================================*/
 
long encodage_num_cpt(int x, int y)
{
	char buffer[15];
	struct numero num_cpt;
 
	gotoxy(x, y);
	num_cpt.num = atol(gets(buffer));
	while((num_cpt.num < 0)||(num_cpt.num >= 10000000)
			||(cherche_compte(num_cpt.num)!=-1 && x!=31))
	{
		if ((num_cpt.num < 0)||(num_cpt.num >= 10000000))
			aff_message("Numero incorrect");
		else
			aff_message("Compte existe déjà");
		gotoxy(x, y);	puts(".......-..");
		gotoxy(x, y);	num_cpt.num=atol(gets(buffer));
	}
 
	return num_cpt.num;
}
 
 
/*====================================================================*
* exec_choix : Pose une question et attend une réponse(char)          *
*====================================================================*/
 
char exec_choix(char message[], char reponse_possibles[])
{
	char 	buffer[10],
	reponse;
	int		cptr;
 
	for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
	gotoxy(1,24);
	for (cptr = 0 ; cptr < 80 ; cptr++) printf(" ");
	gotoxy(40 - strlen(message) / 2 , 23); puts(message);
	gotoxy(32, 24); puts("Votre choix : ");
	do
	{
		gotoxy(48, 24); puts(" ");
		gotoxy(48, 24); gets(buffer);
		reponse = toupper(buffer[0]);
	} while (!strchr(reponse_possibles, reponse));
	return reponse;
}
 
/*====================================================================*
* exec_cree_compte : Permet la création d'un compte                   *
*====================================================================*/
 
void exec_cree_compte()
{
	struct compte cpte;
 
	struct compte_index pos_cpte;
	char buffer[15],
	reponse;
	aff_form_compte("Créer un compte");
	gotoxy(29, 5); puts("667"); cpte.num_de_cpt.bank_id = 667;
 
	cpte.num_de_cpt.num=encodage_num_cpt(33, 5);
 
	cpte.num_de_cpt.check_digit=encodage_check_digit(cpte.num_de_cpt, 40, 5);
 
	pos_cpte.num_de_cpt.bank_id=cpte.num_de_cpt.bank_id;
	pos_cpte.num_de_cpt.num=cpte.num_de_cpt.num;
	pos_cpte.num_de_cpt.check_digit=cpte.num_de_cpt.check_digit;
	pos_cpte.position=nbre_vrais_enreg;
 
	gotoxy(18, 7); gets(buffer);
	cpte.solde=atof(buffer);
	do
	{
		gotoxy(27, 9); gets(buffer);
		cpte.type = toupper(buffer[0]);
	}
	while ((cpte.type != 'E') && (cpte.type != 'C'));
	pos_cpte.type=cpte.type;
	if(cpte.type == 'E')
		cpte.num_cpt_ass=encodage_cpt_ass();
 
	gotoxy(20, 13); gets(buffer);
	cpte.minimum=atof(buffer);
 
	cpte.flag_effacement = 'N';
	reponse = exec_choix("<S>auver - <A>bandonner","SA");
	if (reponse == 'S')
	{
		sauvegarde(cpte, pos_cpte);
		if(nbre_enreg>1)
			trie_index();
	}
}
 
/*====================================================================*
* exec_menu : Affiche un menu et attend le choix d'option             *
* de l'utilisateur                                                    *
*====================================================================*/
 
char exec_menu(char liste_lignes [][MAX_ITEM_SIZE],
int nbre_lignes,
char liste_choix[])
{
	int		pos_col,
	pos_lig,
	long_courant,
	cptr;
	char	reponse,
	buffer[10];
 
	clrscr(); gotoxy(1, 1);
	for (cptr = 0 ; cptr < nbre_lignes ; cptr++)
	long_courant = strlen(liste_lignes[cptr]);
 
	pos_col = 40 - long_courant / 2;
	pos_lig = 12 - nbre_lignes / 2;
	for (cptr = 0 ; cptr < nbre_lignes ; cptr++)
	{
		gotoxy(pos_col, pos_lig+cptr);
		puts(liste_lignes[cptr]);
	}
	gotoxy(pos_col, pos_lig+cptr+2);
	puts("Votre choix : ");
	do
	{
		gotoxy(pos_col + 15 , pos_lig+cptr+2); puts(" ");
		gotoxy(pos_col + 15 , pos_lig+cptr+2); gets(buffer);
	} while (!strchr(liste_choix, (reponse=toupper(buffer[0]))));
	return reponse;
}
 
/*====================================================================*	
* fct_check_digit : Calcule le check digit d'un compte                *
*====================================================================*/
 
int fct_check_digit(int b_id, long num)
{
	char buffer[12];
	double num_complet;
	double check_di;
 
	sprintf (buffer, "%d%ld",b_id,num);
	num_complet = atof(buffer);
	check_di = fmod(num_complet,97);
	return (int)check_di;
 
}
 
 
/*void historique(struct compte_index cpte_bene, struct compte_index cpte_don, double montant, char mouvement[])
{
	struct historique histo;
	time_t timer;
	ouverture_historique();
	histo.num_compte_bene=cpte_bene.num_de_cpt;
	timer=time(NULL);	histo.date=localtime(&timer);
	histo.signe='+';
	histo.montant=montant;
	strcpy(histo.mouvement, mouvement);
	histo.num_compte_don=cpte_don.num_de_cpt;
	strcpy(histo.commu, " ");
	fseek(ptr_flux_histo, 0, SEEK_END);
	fwrite(&histo, sizeof(struct historique), 1, ptr_flux_histo);
	fclose(ptr_flux_histo);
}*/
 
/*====================================================================*	
* info_cpt : Affiche les infos sur le compte se trouvant en pos       *
*====================================================================*/
void info_cpt(int pos)
{
	struct compte cpte;
 
	aff_form_compte("DONNEES GENERALES DU COMPTE");
	fseek(ptr_flux, pos*sizeof(struct compte), SEEK_SET);
	fread(&cpte, sizeof(struct compte), 1,ptr_flux);
	aff_compte(cpte);
	aff_message(" ");
}
 
 
/*====================================================================*
* lister_compte: Affiche les comptes 10 par 10                        *
*====================================================================*/
 
void lister_comptes(int position)
{
	int		cpt, long_titre;
	char	titre[]="Liste des comptes";
	char type[10], reponse;
 
	int stop;
	if(nbre_enreg>0)
	{
	do
	{
	clrscr();
	long_titre = strlen(titre);
	gotoxy(40 - long_titre / 2, 1); puts(titre);
	gotoxy(1, 2);
	for (cpt = 0 ; cpt < 80 ; cpt++)	printf("=");
 
	if(position>nbre_enreg/10)
		position=0;
	if(position < 0)
		position=nbre_enreg/10;
 
	stop=0;
	for(cpt=0;cpt<10 && stop==0;cpt++)
	{
		if(position==nbre_enreg/10 && (cpt==(nbre_enreg%10)-1))
			stop=1;
		switch(toupper(ptr_tab[cpt].type))
		{
		case 'E': 	strcpy(type,"Epargne");
			break;
		case 'C': 	strcpy(type,"Courant");
			break;
		}
		printf(" %d-%ld-%d	| ", ptr_tab[cpt].num_de_cpt.bank_id,
									ptr_tab[cpt].num_de_cpt.num,
									ptr_tab[cpt].num_de_cpt.check_digit);
		puts(type);
	}
		reponse=exec_choix("10 <P>récédents        <R>etour       10 <S>uivants", "PRS");
		switch (reponse)
		{
			case'P' : position-1;break;
			case'S' : position+1;break;
			case'R' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='R');
	}
}
 
 
/*====================================================================*	
* lister_virements : Affiche tous les virements 1 à 1                 *
*====================================================================*/
 
void lister_virements()
{
 
	struct virement vir;
	char reponse='S';
 
 
	ouverture_fic_virement();
	if(nbre_virements>0)
	{
	fseek(ptr_flux_virements, 0, SEEK_SET);
	while(!feof(ptr_flux_virements)&& reponse == 'S')
	{
	fread(&vir, sizeof(struct virement), 1, ptr_flux);
		clrscr();
		aff_form_virement("Liste des virements");
		aff_virements(vir);
	reponse = exec_choix("<R>etour - <S>uivant","RS");
	}
	}
	fclose(ptr_flux_virements);
 
}
 
/*====================================================================*
* menu_consultations : Gère le menu de consultations d'un compte      *
* avec ses options                                                    *
*====================================================================*/
 
void menu_consultations(int pos, long num)
{
	char menu[][MAX_ITEM_SIZE]=
	{   "*******************************************",
		"*           MENU CONSULTATIONS            *",
		"*******************************************",
		"*   <D>onnées du compte                   *",
		"*   <H>istorique des mouvements           *",
		"*   <V>irements en attente                *",
		"*   <O>rdres permanents                   *",
		"*.........................................*",
		"*   <R>etour                              *",
		"*******************************************"},
	reponse;
 
	do
	{
		reponse=exec_menu(menu, 10, "DHVOR");
 
		switch(reponse)
		{
			case'D' : info_cpt(pos); break;
			case'H' : aff_histo(num); break;
			case'V' : virements_en_attente(num); break;
			//case'O' : list_ordre();       break; // att du compte
			case'R' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='R');
}
 
 
/*====================================================================*	
* menu_compte: gère le menu comptes et les options s'y rapportant     *
*====================================================================*/
void menu_comptes()
{
	char menu[][MAX_ITEM_SIZE]=
	{"*******************************************",
		"*             MENU COMPTES                 ",
		"*******************************************",
		"*   <C>réer un compte                      *",
		"*   <L>ister les comptes                  *",
		"*    C<O>nsulter un compte                *",
		"*   <S>upprimer un compte                 *",
		"*   <B>loquer les comptes à bloquer       *",   //todo
		"*   <D>ébloquer un compte                 *",
		"*.........................................*",
		"*   <R>etour                              *",
		"*******************************************"},
	reponse;
	long num;
	int position;
 
 
	do
	{
		reponse=exec_menu(menu, 12, "CLOSBDR");
		switch(reponse)
		{
			case'C' : exec_cree_compte();   	break;
			case'L' : lister_comptes(0);  break;
			case'O' : 	num=demander_num_cpt();printf(" %ld ", num);
						position=cherche_compte(num);
						menu_consultations(position, num);
 
						break;
			case'S' : suppression_compte();  	break;
			//case'B' : bloc_compte();  break;
			case'D' : num=demander_num_cpt();
						position=cherche_compte(num);
						debloquer_compte(position);  break;
			case'R' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='R');
}
 
/*====================================================================*
* menu_main: gère le menu principal et les options s'y rapportant     *
*====================================================================*/
 
void menu_main()
{
	char menu[][MAX_ITEM_SIZE]=
	{"**************************",
		"*     MENU PRINCIPAL     *",
		"**************************",
		"*   <C>omptes            *",
		"*   <D>épôts             *",
		"*   <V>irements          *",
		"*   Ordres <P>ermanants  *",
		"*   Liste des <E>rreurs  *",
		"*........................*",
		"*   <Q>uitter            *",
		"**************************"},
	reponse;
 
	do
	{
		reponse=exec_menu(menu, 11, "CDVPEQ");
		switch (reponse)
		{
			case'C' : menu_comptes();	break;
			case'D' : depot();              break;
			case'V' : menu_virements();		break;
			case'P' : menu_op();			break;
			//case'E' : aff_liste_erreur();                    break;
			case'Q' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='Q');
}
 
 
/*====================================================================*
* menu_op: gère le menu ordre permanent et les options s'y rapportant *
*====================================================================*/
void menu_op()
{
	char	menu [][MAX_ITEM_SIZE]=
	{"*******************************************",
		"*          MENU ORDRES PERMANENTS         *",
		"*******************************************",
		"*   <Créer> un ordre permanent             *",
		"*   <S>upprimer un ordre permanent        *",
		"*   <E>xécuter les ordres permanents      *",
		"*.........................................*",
		"*   <R>etour                              *",
		"*******************************************"},
	reponse;
 
	do
	{
		reponse=exec_menu(menu, 9, "CSER");
		switch(reponse)
		{
			case'C' : cree_op();   break;
			case'S' : suppression_op();   break;
			//case'E' : exec_ordre();   break;   */
			case'R' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='R');
}
 
/*====================================================================*
* menu_ouverture: gère le menu ouverture et les options s'y rapportant*
* Il execute également l'ouverture du fichier compte ainsi que la mise*
* en tableau du fichier index                                         *
*====================================================================*/
 
void menu_ouverture()
{
	int indice;
 
	char menu[][MAX_ITEM_SIZE]=
	{"**************************",
		"*     MENU OUVERTURE     *",
		"**************************",
		"*   <O>uvrir             *",
		"*   <Q>uitter            *",
		"**************************"},
	reponse;
 
	do
	{
		reponse=exec_menu(menu, 6, "OQ");
		switch (reponse)
		{
			case'O' : {
				struct compte cpte;
				clrscr();
				nbre_enreg = 0;
				nbre_vrais_enreg = 0;
 
				ptr_flux = fopen("comptes.dat", "r+b");
				if (ptr_flux == NULL)
				ptr_flux = fopen("comptes.dat", "w+b");
				if (ptr_flux != NULL)
				{
					fseek(ptr_flux, 0, SEEK_SET);
					fread(&cpte, sizeof(struct compte), 1, ptr_flux);
					while (!feof(ptr_flux))
					{
						nbre_vrais_enreg++;
						if (cpte.flag_effacement == 'N')
						nbre_enreg++;
						fread(&cpte, sizeof(struct compte), 1, ptr_flux);
					}
				}
 
 
 
				ptr_flux_index = fopen("index.dat", "r+b");
				if (ptr_flux_index == NULL)
					ptr_flux_index = fopen("index.dat", "w+b");
				ptr_tab = (struct compte_index*) malloc(nbre_enreg* sizeof(ptr_tab));
				fseek(ptr_flux_index, 0, SEEK_SET);
 
				for(indice=0;indice<nbre_enreg;indice++)
					fread(&ptr_tab[indice], sizeof(struct compte_index), 1, ptr_flux_index);
				fclose(ptr_flux_index);}
				menu_main();
				break;
 
			case'Q' : break;
			default : aff_message("Option non reconnue");
		}
 
	} while(reponse!='Q');
}
 
 
/*====================================================================*		
* menu_virements: gère le menu virements et les options s'y rapportant*
*====================================================================*/
 
void menu_virements()
{
	char menu[][MAX_ITEM_SIZE]=
	{"*******************************************",
		"*              MENU VIREMENTS             *",
		"*******************************************",
		"*   <C>réé virement                       *",
		"*   <L>iste des virements en attente      *",
		"*   <E>xécution des virements en attente  *",
		"*.........................................*",
		"*   <R>etour                              *",
		"*******************************************"},
	reponse;
 
	do
	{
		reponse=exec_menu(menu, 9, "CLER");
		switch(reponse)
		{
			case'C' : cree_virement();   break;
			case'L' : lister_virements();   break;
			//case'E' : exec_virement();   break;
			case'R' : break;
			default : aff_message("Option non reconnue");
		}
	} while(reponse!='R');
}
 
/*====================================================================*
* ouverture_fic_op: ouvre le fichier ordre permanent dans le bon mode *
*====================================================================*/
 
void ouverture_fic_op()
{
	struct op cpt_op;
	ptr_flux_op = fopen("ordres.dat", "r+b");
	if (ptr_flux_op != NULL)
	{
		fseek(ptr_flux_op, 0, SEEK_SET);
		fread(&cpt_op, sizeof(struct op), 1, ptr_flux_op);
		nbre_op = 0;
		while (!feof(ptr_flux_op))
		{
			nbre_op++;
			fread(&cpt_op, sizeof(struct op), 1, ptr_flux_op);
		}
	}
	else
		ptr_flux_op = fopen("ordres.dat", "w+b");
}
 
/*====================================================================*		
* ouverture_fic_virement: ouvre le fichier virement dans le bon mode  *
*====================================================================*/
 
void ouverture_fic_virement()
{	
	struct virement cpt_vir;
	ptr_flux_virements = fopen("virements.dat", "r+b");
	if (ptr_flux_virements != NULL)
	{
		fseek(ptr_flux_virements, 0, SEEK_SET);
		fread(&cpt_vir, sizeof(struct virement), 1, ptr_flux_virements);
		nbre_virements = 0;
		while (!feof(ptr_flux_virements))
		{
			nbre_virements++;
			fread(&cpt_vir, sizeof(struct virement), 1, ptr_flux_virements);
		}
	}
	else
		ptr_flux_op = fopen("virements.dat", "w+b");
}
 
/*====================================================================*		
* ouverture_historique: ouvre le fichier historique dans le bon mode  *
*====================================================================*/
 
void ouverture_historique()
{
	struct historique histo;
 
	ptr_flux_histo = fopen("historique.dat", "r+b");
	if (ptr_flux_histo != NULL)
	{
		fseek(ptr_flux_histo, 0, SEEK_SET);
		fread(&histo, sizeof(struct historique), 1, ptr_flux_histo);
		nbre_historiques = 0;
		while (!feof(ptr_flux_histo))
		{
			nbre_historiques++;
			fread(&histo, sizeof(struct historique), 1, ptr_flux_histo);
		}
	}
	else
		ptr_flux_histo = fopen("historique.dat", "w+b");
}
 
/*====================================================================*
* sauvegarde: enregistre les données du nouveau compte dans le        *
* fichier compte et dans l'index                                      * 
*====================================================================*/
 
void sauvegarde(struct compte cpte, struct compte_index pos_cpte)
{
		int indice;
		struct compte_index *ptr_tab_tmp;
		ptr_tab_tmp = (struct compte_index *)realloc (ptr_tab, (nbre_enreg+1)* sizeof(ptr_tab_tmp));
 
		if (ptr_tab_tmp != NULL)
		{
			for(indice=0;indice<nbre_enreg;indice++)
				ptr_tab_tmp[indice]=ptr_tab[indice];
			ptr_tab = ptr_tab_tmp;
		}
		ptr_tab[nbre_enreg]=pos_cpte;
 
 
		fseek(ptr_flux, 0, SEEK_END);
		fwrite(&cpte, sizeof(struct compte), 1, ptr_flux);
		free(ptr_tab_tmp);
		nbre_enreg++;
		nbre_vrais_enreg++;
}
 
/*====================================================================*
* sauvegarde_op: enregistre les données du nouvel ordre permanent     *
*====================================================================*/
 
void sauvegarde_op(struct op ordre)
{
	char reponse;
	reponse = exec_choix("<S>auver - <A>bandonner","SA");
	if (reponse == 'S')
	{
		fseek(ptr_flux_virements, 0, SEEK_END);
		fwrite(&ordre, sizeof(struct virement), 1, ptr_flux_op);
		nbre_op++;
	}
	fclose(ptr_flux_op); 
}
 
/*====================================================================*		
* sauvegarde_vir: enregistre les données du nouveau virement          *
*====================================================================*/
 
void sauvegarde_vir(struct virement vrmt){
	char reponse;
	reponse = exec_choix("<S>auver - <A>bandonner","SA");
	if (reponse == 'S')
	{
		fseek(ptr_flux_virements, 0, SEEK_END);
		fwrite(&vrmt, sizeof(struct virement), 1, ptr_flux_virements);
		nbre_virements++;
	}
	fclose(ptr_flux_virements);
}
 
 
/*====================================================================*		
* suppression_compte: supprime un compte                              *
*====================================================================*/
 
void suppression_compte()
{
	char buffer[15];
	int pos,indice, pos_index, trouve;
	long num;
	struct compte cpte;
 
	clrscr();
	aff_form_compte("SUPPRESSION D'UN COMPTE");
	gotoxy(29,5); puts("667");
	gotoxy(33, 5);	num = atol(gets(buffer));
	pos=cherche_compte(num);
	if((num <= 0)||(pos==-1)|| num>=10000000)
		aff_message("Ce compte n'existe pas");
	else
	{
		fseek(ptr_flux, sizeof(struct compte)*pos, SEEK_SET);
		fread(&cpte, sizeof(struct compte), 1, ptr_flux);
		aff_compte(cpte);
		if (cpte.solde!=0)
			aff_message("Impossible de supprimer un compte non soldé");
		else
			if (exec_choix("<C>onfirmer suppression - <A>nnuler", "CA")== 'C')
			{
				cpte.flag_effacement = 'O';
				trouve=0;
				for(indice=0;indice<nbre_enreg && trouve ==0;indice++)
					if(ptr_tab[indice].position==pos)
					{
						pos_index=indice;
						trouve=1;
					}
 
					for(indice=pos_index;indice<nbre_enreg-1;indice++)
						ptr_tab[indice]=ptr_tab[indice+1];
 
				nbre_enreg--;
				fseek(ptr_flux, sizeof(struct compte)*pos, SEEK_SET);
				fwrite(&cpte, sizeof(struct compte), 1, ptr_flux);
			}
	}
}
 
/*====================================================================*		
* suppression_op: supprime un ordre permanent                         *
*====================================================================*/
 
void suppression_op()
{
	char reponse='S';
	struct op 	ord_p;
	int		pos=0;
 
 
	clrscr();
	ouverture_fic_op();
	aff_form_op("SUPPRESSION D'UN ORDRE PERMANANT");
	fseek(ptr_flux_op, 0, SEEK_SET);
	fread(&ord_p, sizeof(struct op), 1, ptr_flux_op);
	while (!feof(ptr_flux_op)&&reponse=='S')
	{
		if (ord_p.flag_effacement=='N')
		{
			aff_op(ord_p);
			reponse=exec_choix("<E>ffacer - <S>uivant", "ES");
			if (reponse == 'E')
			{
				ord_p.flag_effacement = 'O';
				nbre_op--;
				fseek(ptr_flux_op, pos, SEEK_SET);
				fwrite(&ord_p, sizeof(struct op), 1, ptr_flux_op);
				reponse='S';
			}
		}
		fread(&ord_p, sizeof(struct op), 1, ptr_flux_op);
		pos++;
	}
	fclose(ptr_flux_op);
}
 
/*=====================================================================*
* trie_index: Trie le tableau de structures index                       *
*=====================================================================*/
 
void trie_index()
{
	struct compte_index temp;
	int idx,
	debut,
	fin,
	milieu,
	pos=nbre_enreg-1;
 
	debut=0;
	fin=nbre_enreg-2;
 
	 while (debut <= fin)
	 {
		milieu = (debut + fin) / 2;
		if (debut == fin)
		{
			if (ptr_tab[milieu].num_de_cpt.num > ptr_tab[nbre_enreg - 1].num_de_cpt.num)
			{
				pos = milieu ;
			} else {
				pos = milieu +1;
			}
		}
		if (ptr_tab[milieu].num_de_cpt.num > ptr_tab[nbre_enreg - 1].num_de_cpt.num)
			fin = milieu - 1;
		else
			debut = milieu + 1;
	}
	temp=ptr_tab[nbre_enreg-1];
 
	for(idx=nbre_enreg-2;idx>=pos;idx--)
		ptr_tab[idx+1]=ptr_tab[idx];
 
	ptr_tab[pos]=temp;
 
}
 
/*=====================================================================*
* virements_en_attente: Affiche les virements en attente               *
*=====================================================================*/
 
void virements_en_attente(long num_de_cpt)
{
	struct virement vir;
	char reponse='S';
	ouverture_fic_virement();
	if(nbre_virements>0)
	{
	fseek(ptr_flux_virements, 0, SEEK_SET);
	while(!feof(ptr_flux_virements)&& reponse == 'S')
	{
	fread(&vir, sizeof(struct virement), 1, ptr_flux_virements);
 
	if(vir.num_compte_don.num == num_de_cpt || vir.num_compte_bene.num == num_de_cpt)
	{
		clrscr();
		aff_form_virement("Virements en attente");
		aff_virements(vir);
	}
	reponse = exec_choix("<R>etour - <S>uivant","RS");
	}
	}
	fclose(ptr_flux_virements);
 
 
}