Guest User

Untitled

a guest
Jan 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* count characters in input; 1st version */
  4. main()
  5. {
  6. long nc;
  7.  
  8. nc = 0;
  9. while (getchar() != EOF)
  10. ++nc;
  11. printf("%ldn", nc);
  12. }
  13.  
  14. #include <stdio.h>
  15.  
  16. /* count characters in input; 2nd version */
  17. main()
  18. {
  19. double nc;
  20.  
  21. for (nc = 0; getchar() != EOF; ++nc)
  22. ;
  23. printf("%.0fn", nc);
  24. }
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <float.h>
  29.  
  30. /* count characters in input; SO version */
  31. int main(void)
  32. {
  33. double total;
  34. float batch; /* change me to double */
  35.  
  36. for (total = 0.0, batch = 0.0; getchar() != EOF; ) {
  37. float batch_new = batch + 1.0; /* me too */
  38. if (batch_new - batch != 1.0) {
  39. total += batch;
  40. batch = 1.0;
  41. } else {
  42. batch = batch_new;
  43. }
  44. }
  45.  
  46. if (ferror(stdin)) {
  47. printf("I/O error on standard inputn");
  48. return EXIT_FAILURE;
  49. }
  50.  
  51. if (total == 0.0)
  52. printf("%.0f (exact)n", batch);
  53. else
  54. printf("%.*g (approx)n", DBL_DIG, total + batch);
  55.  
  56. return 0;
  57. }
  58.  
  59. double
Add Comment
Please, Sign In to add comment