Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8.  
  9.  
  10. char
  11. *getline2(FILE *f)
  12.  
  13.  
  14. int
  15. main(void)
  16. {
  17. char *s = NULL;
  18. int line_counter = 1;
  19. int number_counter = 0;
  20. long curr;
  21. long sum = 0;
  22. int stop_flag = 0;
  23. char *end;
  24. char *buf;
  25. while ((s = getline2(stdin)) != NULL)
  26. {
  27. buf = s;
  28. stop_flag = 0;
  29. while (isspace(*s))
  30. {
  31. s++;
  32. }
  33. if (*s == '\0')
  34. {
  35. printf("%d\n", 0xbad1dea + line_counter);
  36. line_counter++;
  37. free(buf);
  38. continue;
  39. }
  40. if (!isdigit(*s) && *s != '-')
  41. {
  42. printf("%d\n", 0xbedabeda + line_counter);
  43. free(buf);
  44. line_counter++;
  45. continue;
  46. }
  47.  
  48.  
  49. sum = 0;
  50. number_counter = 0;
  51. while (s)
  52. {
  53.  
  54. number_counter++;
  55. errno = 0;
  56. curr = strtol(s, &end, 10);
  57. if (errno == ERANGE && curr == LONG_MAX)
  58. {
  59.  
  60. sum += number_counter;
  61. errno = 0;
  62. }
  63. else if (errno == ERANGE && curr == LONG_MIN)
  64. {
  65.  
  66. sum -= number_counter;
  67.  
  68. errno = 0;
  69. }
  70. else
  71. {
  72.  
  73. sum += curr;
  74. }
  75. if (!curr) break;
  76. s = end;
  77.  
  78. while (isspace(*s))
  79. {
  80. s++;
  81. }
  82. if (*s != '\0' && !isdigit(*s) && *s != '-')
  83. {
  84. printf("%d\n", 0xbedabeda + line_counter);
  85. line_counter++;
  86. stop_flag = 1;
  87. break;
  88. }
  89. }
  90. if (!stop_flag) printf("%ld\n", sum);
  91. line_counter++;
  92.  
  93.  
  94. free(buf);
  95. }
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement