No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pile.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "pile.h"
  6. void Init_Pile(struct Pile *new)
  7. {
  8. //struct Pile new;
  9. new->Pil=NULL;
  10. new->nbElements=0;
  11. //return new;
  12. }
  13. struct Nombre Typenum(char * C)
  14. {
  15. struct Nombre k;
  16. //enum Numtype type;
  17. //union Test i;
  18. k.typ = Entier;
  19. for(int i=0; i<strlen(C); i++)
  20. {
  21. if( C[i] == '.')
  22. {
  23. k.typ=Reel;
  24. }
  25. }
  26. if(k.typ == Entier)
  27. {
  28. k.nombr.ValEntier = atoi(C);
  29. }
  30. else if(k.typ == Reel)
  31. {
  32. k.nombr.ValReel = atof(C);
  33. }
  34. return k;
  35. }
  36. struct Nombre Addition(struct Nombre a, struct Nombre b)
  37. {
  38. struct Nombre e;
  39. if(a.typ == Entier && b.typ == Entier)
  40. {
  41. e.typ = Entier;
  42. e.nombr.ValEntier = a.nombr.ValEntier + b.nombr.ValEntier;
  43. }
  44. else if(a.typ == Entier && b.typ == Reel)
  45. {
  46. e.typ = Reel;
  47. e.nombr.ValReel = a.nombr.ValEntier + b.nombr.ValReel;
  48. }
  49. else if(a.typ == Reel && b.typ == Entier)
  50. {
  51. e.typ = Reel;
  52. e.nombr.ValReel = a.nombr.ValReel + b.nombr.ValEntier;
  53. }
  54. else if(a.typ == Reel && b.typ == Reel)
  55. {
  56. e.typ = Reel;
  57. e.nombr.ValReel = a.nombr.ValReel + b.nombr.ValReel;
  58. }
  59. return e;
  60. }
  61. struct Nombre Soustraire(struct Nombre a, struct Nombre b)
  62. {
  63. struct Nombre e;
  64. if(a.typ == Entier && b.typ == Entier)
  65. {
  66. e.typ = Entier;
  67. e.nombr.ValEntier = a.nombr.ValEntier - b.nombr.ValEntier;
  68. }
  69. else if(a.typ == Entier && b.typ == Reel)
  70. {
  71. e.typ = Reel;
  72. e.nombr.ValReel = a.nombr.ValEntier - b.nombr.ValReel;
  73. }
  74. else if(a.typ == Reel && b.typ == Entier)
  75. {
  76. e.typ = Reel;
  77. e.nombr.ValReel = a.nombr.ValReel - b.nombr.ValEntier;
  78. }
  79. else if(a.typ == Reel && b.typ == Reel)
  80. {
  81. e.typ = Reel;
  82. e.nombr.ValReel = a.nombr.ValReel - b.nombr.ValReel;
  83. }
  84. return e;
  85. }
  86. struct Nombre Multiplier(struct Nombre a, struct Nombre b)
  87. {
  88. struct Nombre e;
  89. if(a.typ == Entier && b.typ == Entier)
  90. {
  91. e.typ = Entier;
  92. e.nombr.ValEntier = a.nombr.ValEntier * b.nombr.ValEntier;
  93. }
  94. else if(a.typ == Entier && b.typ == Reel)
  95. {
  96. e.typ = Reel;
  97. e.nombr.ValReel = a.nombr.ValEntier * b.nombr.ValReel;
  98. }
  99. else if(a.typ == Reel && b.typ == Entier)
  100. {
  101. e.typ = Reel;
  102. e.nombr.ValReel = a.nombr.ValReel * b.nombr.ValEntier;
  103. }
  104. else if(a.typ == Reel && b.typ == Reel)
  105. {
  106. e.typ = Reel;
  107. e.nombr.ValReel = a.nombr.ValReel * b.nombr.ValReel;
  108. }
  109. return e;
  110. }
  111. struct Nombre Diviser(struct Nombre a, struct Nombre b)
  112. {
  113. struct Nombre e;
  114. if(a.typ == Entier && b.typ == Entier)
  115. {
  116. e.typ = Entier;
  117. e.nombr.ValEntier = a.nombr.ValEntier / b.nombr.ValEntier;
  118. }
  119. else if(a.typ == Entier && b.typ == Reel)
  120. {
  121. e.typ = Reel;
  122. e.nombr.ValReel = a.nombr.ValEntier / b.nombr.ValReel;
  123. }
  124. else if(a.typ == Reel && b.typ == Entier)
  125. {
  126. e.typ = Reel;
  127. e.nombr.ValReel = a.nombr.ValReel / b.nombr.ValEntier;
  128. }
  129. else if(a.typ == Reel && b.typ == Reel)
  130. {
  131. e.typ = Reel;
  132. e.nombr.ValReel = a.nombr.ValReel / b.nombr.ValReel;
  133. }
  134. return e;
  135. }
  136. void Empiler(struct Pile * P, char * nb)
  137. {
  138. struct Cell * cel= malloc(sizeof(struct Cell));
  139. struct Nombre i = Typenum(nb);
  140. if (i.typ == Entier)
  141. {
  142. //cel->Ent.nombr.ValEntier = i.nombr.ValEntier;
  143. //cel->Ent.typ = Entier;
  144. cel->Ent = i;
  145. }
  146. else if (i.typ == Reel)
  147. {
  148. //cel->Ent.nombr.ValReel = i.nombr.ValReel;
  149. //cel->Ent.typ = Reel;
  150. cel->Ent = i;
  151. }
  152. cel->Suiv = P->Pil;
  153. P->Pil = cel;
  154. P->nbElements +=1;
  155. }
  156. void EmpilerR(struct Pile * P, struct Nombre i)
  157. {
  158. struct Cell * cel= malloc(sizeof(struct Cell));
  159. cel->Ent = i;
  160. cel->Suiv = P->Pil;
  161. P->Pil = cel;
  162. P->nbElements +=1;
  163. }
  164. struct Cell * Depiler(struct Pile * P)
  165. {
  166. struct Cell * res= NULL;
  167. if (P->Pil != NULL)
  168. {
  169. res = P->Pil;
  170. P->Pil= P->Pil->Suiv;
  171. }
  172. return res;
  173. }
  174. void Supprimer(struct Pile *P){
  175. struct Cell * tmp;
  176. if(P->nbElements != 0)
  177. {
  178. tmp = P->Pil;
  179. P->Pil = P->Pil->Suiv;
  180. free(tmp);
  181. }
  182. }
  183. void Affiche_Nombre(struct Nombre nb)
  184. {
  185. if(nb.typ == Entier)
  186. {
  187. printf("%d \n", nb.nombr.ValEntier);
  188. }
  189. else if(nb.typ == Reel)
  190. {
  191. printf("%f \n", nb.nombr.ValReel);
  192. }
  193. }
  194. void Affichage(struct Cell * C)
  195. {
  196. if(C == NULL)
  197. {
  198. //printf("Rien a afficher");
  199. }
  200. else
  201. {
  202. Affiche_Nombre(C->Ent);
  203. Affichage(C->Suiv);
  204. }
  205. }
  206. int numberOfDelimiters(char* string) {
  207. int count = 0;
  208. for (int i=0;i<strlen(string);i++) {
  209. if (string[i] == ' ')
  210. {count++;}
  211. }
  212. return count;
  213. }
  214. Programme* lexer(char* chaine) {
  215. char *token,*str;
  216. str = strdup(chaine);
  217. int i=0;
  218. int arraysize = (numberOfDelimiters(str)+1);
  219. char** programme = (char**)malloc(sizeof(char*)*arraysize);
  220. while ( (token = strsep(&str, " ")) ) {
  221. programme[i] = token;
  222. i++;
  223. }
  224. Programme *retour = (Programme*)malloc(sizeof(Programme));
  225. retour->tokens = programme;
  226. retour->taille = i;
  227. return retour;
  228. }
  229. void Executer(Etat * etat){
  230. //struct Pile P = etat->Donnee;
  231. //Programme Program = etat->Prog;
  232. struct Cell *C1, *C2;
  233. int tail = etat->Prog->taille;
  234. struct Nombre r;
  235. char * element;
  236. for(int i = 0;i <tail; i++)
  237. {
  238. element = (etat->Prog->tokens[i]);
  239. //printf("%s \n", element);
  240. if ( *element == '+')
  241. {
  242. C2 = Depiler(etat->Donnee);
  243. C1 = Depiler(etat->Donnee);
  244. if(C1 != NULL && C2 != NULL)
  245. {
  246. //r= C1->Ent.ValEntier + C2->Ent.ValEntier;
  247. r = Addition(C1->Ent, C2->Ent);
  248. EmpilerR(etat->Donnee, r );
  249. }
  250. else
  251. {
  252. printf("ERROR \n");
  253. }
  254. }
  255. else if( *element == '/')
  256. {
  257. C2 = Depiler(etat->Donnee);
  258. C1 = Depiler(etat->Donnee);
  259. if(C1 != NULL && C2 != NULL)
  260. {
  261. //r= C1->Ent.ValEntier / C2->Ent.ValEntier;
  262. r=Diviser(C1->Ent,C2->Ent);
  263. EmpilerR(etat->Donnee, r );
  264. }
  265. else
  266. {
  267. printf("ERROR \n");
  268. }
  269. }
  270. else if( *element == '-')
  271. {
  272. C2 = Depiler(etat->Donnee);
  273. C1 = Depiler(etat->Donnee);
  274. if(C1 != NULL && C2 != NULL)
  275. {
  276. //r= C1->Ent.ValEntier - C2->Ent.ValEntier;
  277. r = Soustraire(C1->Ent ,C2->Ent);
  278. EmpilerR(etat->Donnee, r );
  279. }
  280. else
  281. {
  282. printf("ERROR \n");
  283. }
  284. }
  285. else if(*element == '*')
  286. {
  287. C2 = Depiler(etat->Donnee);
  288. C1 = Depiler(etat->Donnee);
  289. if(C1 != NULL && C2 != NULL)
  290. {
  291. //r= C1->Ent.ValEntier * C2->Ent.ValEntier;
  292. r = Multiplier(C1->Ent,C2->Ent);
  293. EmpilerR(etat->Donnee, r );
  294. }
  295. else
  296. {
  297. printf("ERROR \n");
  298. }
  299. }
  300. else
  301. {
  302. Empiler(etat->Donnee, element);
  303. }
  304. }
  305. //printf("SORTIE : %d \n", etat->Donnee->Pil->Ent.ValEntier);
  306. printf("SORTIE :");
  307. Affiche_Nombre(etat->Donnee->Pil->Ent);
  308. }