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.

num.c 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "num.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. void printLnNum( struct NumContainer num ) {
  6. if ( num.t == INT ) {
  7. printf("%d\n", num.n.vali);
  8. } else {
  9. printf("%f\n", num.n.valf);
  10. }
  11. }
  12. void printNum( struct NumContainer num ) {
  13. if ( num.t == INT ) {
  14. printf("%d ", num.n.vali);
  15. } else {
  16. printf("%f ", num.n.valf);
  17. }
  18. }
  19. char evalasBool( struct NumContainer num ) {
  20. if (num.t == INT) {
  21. if (num.n.vali == 0) {
  22. return 0;
  23. } else {
  24. return 1;
  25. }
  26. } else {
  27. if (num.n.valf == 0.0) {
  28. return 0;
  29. } else {
  30. return 1;
  31. }
  32. }
  33. }
  34. struct NumContainer readNum(char* s) {
  35. char* original = s;
  36. int is_int = 1;
  37. struct NumContainer res;
  38. while (*s != '\0') {
  39. if ( !isdigit(*s) ) {
  40. if ( *s == '.' && is_int ) {
  41. is_int = 0;
  42. } else {
  43. fprintf(stderr, "'%s' is not a number", original);
  44. exit(1);
  45. }
  46. }
  47. ++s;
  48. }
  49. if (is_int) {
  50. res.t = INT;
  51. res.n.vali = atoi(original);
  52. } else {
  53. res.t = FLOAT;
  54. res.n.valf = atof(original);
  55. }
  56. return res;
  57. }