-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtp3.c
More file actions
251 lines (217 loc) · 7.04 KB
/
tp3.c
File metadata and controls
251 lines (217 loc) · 7.04 KB
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
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "tp3.h"
// fonctions requises
T_Transaction* ajouterTransaction(int idEtu, float montant, char *descr, time_t date, BlockChain bc){//il faut donner le pointeur sur le bon block pour inserer la transaction
//allocation de l'espace mémoire correspondant à une transaction
T_Transaction* newTransaction = (T_Transaction*)malloc(sizeof(T_Transaction));
//initialisation de la transaction
newTransaction->idEtu=idEtu;
newTransaction->montant=montant;
newTransaction->descr= (char*)malloc(sizeof(MAX_DESCR+1));
strcpy(newTransaction->descr,descr);
//T_Transaction* listeTransaction = searchTransactionToInsert(date,bc);
if(bc->listeTransaction){
newTransaction->suivant=bc->listeTransaction;
}
else
newTransaction->suivant=NULL;
printTransaction(newTransaction);
return newTransaction;
}
BlockChain ajouterBlock(BlockChain bc, time_t t){//#DONE
T_Block* newBlock = creeBlock(t);
if(bc){
bc = insertBlockbyDate(bc, newBlock);
bc = uptadeIDBlock(bc);
}
//printf("bc pointe sur le %deme block\n",bc->idBlock);
return bc;
}
BlockChain insertBlockbyDate(BlockChain bc, BlockChain newBlock){
if(bc){
//printf("bc nest pas null\n");
if (bc->date < newBlock->date){ //insertion au debut de la blockchain
newBlock->suivant=bc;
bc=newBlock;
//printf("bc pointe sur le %deme block\n",bc->idBlock);
}else{
BlockChain tmp = bc;
if (tmp->suivant){
while(tmp->suivant->date > newBlock->date){
tmp=tmp->suivant;
if (!tmp->suivant){//nous sommes à la fin de la chaine
tmp->suivant = newBlock;
return bc;
}
}//nous sommes au milieu de la liste
newBlock->suivant=tmp->suivant;
tmp->suivant = newBlock;
return bc;
}else{//on insert à la fin d'une liste de longueur 1
tmp->suivant = newBlock;
return bc;
}
}
}
else{
return newBlock;
}
return bc;
}
BlockChain creeBlock(time_t t){
//allocation de l'espace mémoire correspondant à un Block
T_Block* newBlock = malloc(sizeof(T_Block));
//initialisation du Block
newBlock->listeTransaction = NULL;//pas de malloc car liste vide
newBlock->date=t;
newBlock->idBlock=0;
newBlock->suivant=NULL;
return newBlock;
}
float totalTransactionEtudiantBlock(int idEtu, BlockChain Block){//envoie la variation du solde pour un block
float soldeJour=0.0;
if(Block){
T_Transaction* temp;
temp=Block->listeTransaction;
if(temp){
//printf("Ce Block contient une liste de transaction\n");
while(temp){
if(temp->idEtu==idEtu)
soldeJour+=temp->montant;
temp=temp->suivant;
}
//printf("temp de la fonction print va etre freed\n");
}else{
//printf("Ce Block contient une liste de transaction vide\n");
}
}else{printf("ce block nexiste pas");
}
return soldeJour;
}
float soldeEtudiant(int idEtu, BlockChain bc){//4 requis //retourne le montant disponible sur le compte de l'étudiant
float solde=0.0;
if(bc){
T_Block* temp;
temp = bc;
while(temp){
solde+=totalTransactionEtudiantBlock(idEtu,temp);
temp=temp->suivant;
}
}
return solde;
}
void crediter(int idEtu, float montant, char *descr,time_t date, BlockChain bc){//5 #DONE
bc->listeTransaction = ajouterTransaction(idEtu,montant,descr,date,bc);
}
int payer(int idEtu, float montant, char *descr,time_t date, BlockChain bc){//6 #DONE
if(montant>soldeEtudiant(idEtu,bc)){
printf("lEtudiant %d ne dispose que de %f, transaction annulee\n",idEtu,soldeEtudiant(idEtu,bc));
return 0;
}
bc->listeTransaction=ajouterTransaction(idEtu,-(montant),descr,date,bc);
return 1;
}
int transfert(int idSource, int idDestination, float montant, char *descr,time_t date, BlockChain bc){//7 du menu
if((idDestination!=idSource)&&payer(idSource,montant,descr,date,bc)==1){
//printf("lEtudiant a ete paye\n");
crediter(idDestination,montant,descr,date,bc);
//printf("et on a crédité l'autre\n");
}
else
return 0;
return 1;
}
//fonctions intervenantes dans le menu
//fonctions optionelles
T_Block* searchBlockbyId(int idBlock, BlockChain bc){//renvoi le block de la chaine ayant l'id indiqué
T_Block* temp;
if(!bc)return NULL;
if(idBlock>bc->idBlock){
printf("le block %d n'existe pas car la liste est trop courte\n",idBlock);
return NULL;
}
temp=bc;
while(temp&&(temp->idBlock>=0)){
if(temp->idBlock==idBlock)
return temp;
temp=temp->suivant;
}
return NULL;
}
T_Block* searchBlockbyDate(time_t date, BlockChain bc){//#DONE
T_Block* temp;
if(!bc)return NULL;
temp=bc;
while(temp){
if(temp->date==date)
return temp;
temp=temp->suivant;
}
return NULL;
}
BlockChain* searchTransactionToInsert(time_t date,BlockChain* bc){//#DONE
BlockChain* bonB = malloc(sizeof(BlockChain));
if(bc && date != (-1)){
*bonB = searchBlockbyDate(date,*bc);
if(!*bonB){
*bc = ajouterBlock(*bc,date);
*bonB= searchBlockbyDate(date,*bc);
}
return bonB;
}
return NULL;
}
int nbBlockinChain(BlockChain bc){
int nbBlock =0;
if (bc){
BlockChain tmp = bc;
while(tmp){
tmp= tmp->suivant;
nbBlock++;
}
}
return nbBlock;
}
BlockChain uptadeIDBlock(BlockChain bc){//change le num des blocks tant que ca ne match pas mais ne vérifie pas toute la liste
if (bc){
int nbBlock = (nbBlockinChain(bc)-1);
BlockChain tmp = bc;
while(tmp ){//&& tmp->idBlock != nbBlock){
tmp->idBlock = nbBlock--;
tmp=tmp->suivant;
}
}
return bc;
}
BlockChain insertionSortbyDate(BlockChain bc){
if(bc){
int taille = (nbBlockinChain(bc)-2);
int i;
for (i=taille;i>0;--i){
bc = uptadeIDBlock(bc);
BlockChain tmp = selectBlock(searchBlockbyId(i,bc),&bc);
bc = insertBlockbyDate(bc,tmp);
printf("i est %d\n",i);
}
}
return bc;
}
BlockChain selectBlock(BlockChain Block,BlockChain* bc){//attention, ne fonctionne que si les blocks ont des ID décroissants de 1 en 1.
if (Block){
if(Block->idBlock == nbBlockinChain(*bc)){//il faut supprimer le premier block de la chaine
(*bc)->suivant=Block->suivant;
}else{
BlockChain tmp = searchBlockbyId((Block->idBlock+1),*bc);
(tmp)->suivant=Block->suivant;
}
}
return Block;
}