Résultat du TP en C sur Forth
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.

calcop.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "calcop.h"
  2. #include "num.h"
  3. #include "pile.h"
  4. int addi(int i, int j) {
  5. return i + j;
  6. }
  7. float addf(float i, float j) {
  8. return i + j;
  9. }
  10. int subi(int i, int j) {
  11. return i - j;
  12. }
  13. float subf(float i, float j) {
  14. return i - j;
  15. }
  16. int multi(int i, int j) {
  17. return i * j;
  18. }
  19. float multf(float i, float j) {
  20. return i * j;
  21. }
  22. int divi(int i, int j) {
  23. return i/j;
  24. }
  25. float divf(float i, float j) {
  26. return i/j;
  27. }
  28. void calcop( int (*opint)(int,int), float (*opfloat)(float,float), struct Pile* pile ) {
  29. struct NumContainer res;
  30. struct NumContainer num1;
  31. struct NumContainer num2;
  32. getlastnums(&num1, &num2, pile);
  33. if ( num1.t == INT ) {
  34. if ( num2.t == INT ) {
  35. res.t = INT;
  36. res.n.vali = opint( num1.n.vali, num2.n.vali );
  37. } else {
  38. res.t = FLOAT;
  39. res.n.valf = opfloat((float)num1.n.vali, num2.n.valf);
  40. }
  41. } else {
  42. res.t = FLOAT;
  43. if ( num2.t == INT ) {
  44. res.n.valf = opfloat(num1.n.valf, (float)num2.n.vali);
  45. } else {
  46. res.n.valf = opfloat(num1.n.valf, num2.n.valf);
  47. }
  48. }
  49. push(pile, res);
  50. }
  51. void add(struct State* state) {
  52. if (state->mode == EXECUTE) {
  53. calcop(addi, addf, &(state->pile));
  54. }
  55. }
  56. void sub(struct State* state) {
  57. if (state->mode == EXECUTE) {
  58. calcop(subi, subf, &(state->pile));
  59. }
  60. }
  61. void mult(struct State* state) {
  62. if (state->mode == EXECUTE) {
  63. calcop(multi, multf, &(state->pile));
  64. }
  65. }
  66. void divide(struct State* state) {
  67. if (state->mode == EXECUTE) {
  68. calcop(divi, divf, &(state->pile));
  69. }
  70. }