Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
- #include <ctype.h>
- #include <limits.h>
- #include <errno.h>
- char
- *getline2(FILE *f)
- int
- main(void)
- {
- char *s = NULL;
- int line_counter = 1;
- int number_counter = 0;
- long curr;
- long sum = 0;
- int stop_flag = 0;
- char *end;
- char *buf;
- while ((s = getline2(stdin)) != NULL)
- {
- buf = s;
- stop_flag = 0;
- while (isspace(*s))
- {
- s++;
- }
- if (*s == '\0')
- {
- printf("%d\n", 0xbad1dea + line_counter);
- line_counter++;
- free(buf);
- continue;
- }
- if (!isdigit(*s) && *s != '-')
- {
- printf("%d\n", 0xbedabeda + line_counter);
- free(buf);
- line_counter++;
- continue;
- }
- sum = 0;
- number_counter = 0;
- while (s)
- {
- number_counter++;
- errno = 0;
- curr = strtol(s, &end, 10);
- if (errno == ERANGE && curr == LONG_MAX)
- {
- sum += number_counter;
- errno = 0;
- }
- else if (errno == ERANGE && curr == LONG_MIN)
- {
- sum -= number_counter;
- errno = 0;
- }
- else
- {
- sum += curr;
- }
- if (!curr) break;
- s = end;
- while (isspace(*s))
- {
- s++;
- }
- if (*s != '\0' && !isdigit(*s) && *s != '-')
- {
- printf("%d\n", 0xbedabeda + line_counter);
- line_counter++;
- stop_flag = 1;
- break;
- }
- }
- if (!stop_flag) printf("%ld\n", sum);
- line_counter++;
- free(buf);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement